NewBQ: Accessing list instance variable? 
Author Message
 NewBQ: Accessing list instance variable?

Hi All!

I've got a problem:
My object is a reference to a hash containing  a list of references to
objects. Now I whant to add to the list, but I cannot extract the list
as a list from the object. All I ever get is the name of the first
instance variable in the first object.

The essence of my code is:

package Obj;
sub new{
  my $invocer =shift;
  my $class = ref($invocer) || $invocer;
  my $self = {xmin => 0,
              xmax => 0,
              name => "",
              lst => (),

  return $self;


sub addData{
  my $self = shift;
  my $inObj = shift;
  unshift $self->{lst},$inObj;

  return $self->{lst};


With this code, the unshift statement returns the message:

"Type of arg 1 to unshift must be array (not hash element) at.."

Howcome? What do I do about it?


Tue, 21 Sep 2004 13:01:58 GMT  
 NewBQ: Accessing list instance variable?

>            my $self = {xmin => 0,
>                xmax => 0,
>                name => "",
>                lst => (),

You want a reference to an (empty) array here, not an empty list:

                         lst => [],                          

>   unshift $self->{lst},$inObj;

You have to dereference the reference to get at the array:


Tue, 21 Sep 2004 15:13:12 GMT  
 [ 2 post ] 

 Relevant Pages 

1. OO Perl instance variables and lists

2. Instance Data / Instance Variables

3. Perl 5 hack: readable instance variables.

4. Questions on instance variable inheritance in perl5

5. Creating instance variables in Perl5

6. Private instance variables in Objects ??

7. question about objects (modules) - instance variable

8. Array/hash instance variable in hash

9. Problem with anonymous array as instance variable?

10. Filehandles as instance variables

11. Filehandle as object instance variable?

12. Implementing instance variables for composite widgets


Powered by phpBB® Forum Software