[Wellington-pm] Referencing arrays in an object

Cliff Pratt enkidu at cliffp.com
Sun May 28 01:39:13 PDT 2006


Grant McLean wrote:
> On Sun, 2006-05-28 at 17:39 +1200, Cliff Pratt wrote:
> 
>>One of the methods is called 'grid' initialised so:
>>
>>$self->grid([]) ;
>>
>>I can access the elements of the array referred to by $self->grid by the 
>>following:
>>
>>$xxxx = $self->grid->[$i] ; # Within a method of the class
>>
>>I'm not using any accessor method here, am I? 
> 
> Yes, you are.  $self->grid is calling the 'grid' accessor which is
> returning an array reference.  You are then indexing into the referenced
> array by appending ->[$i]
>
Ah, thanks for clarifying that.
 >
> If you wanted to foreach through all the elements in the array you could
> do it like this:
> 
>   foreach my $cell ( @{ $self->grid } ) {
>     # do something with $cell
>   }
> 
> 
>>I theenk that the accessor 
>>method will be something like the following:
>>
>>$xxxx = $self->grid([$i]) ; # Will this work?
> 
> No, in this case you're calling the grid method and passing it a
> reference to a newly created array with one element, which contains the
> current value of $i.  From what you said earlier, the effect will be
> overwriting the whole 'grid' array.
>
Ugh! That's not what I want!
 >
> You could conceivably write your own accessor method that took the grid
> index as an argument:
> 
>   $self->grid_cell($i)
> 
> It's hard to say whether that would offer any advantage in your case.
>
None really. That Class::Accessor::Fast module is pretty cool!

Cheers,

Cliff

-- 

http://barzoomian.blogspot.com


More information about the Wellington-pm mailing list