in reply to Iterating over an array of objects, within an object?

To say it a bit shorter than GrandFather, $self->{objs} is a reference to your array. A reference is a scalar, so all you're iterating over is that one reference.

What you want instead of course is to iterate over the elements of your array. So to get from your array reference to your array, you need to dereference it. So you need @{$self->{objs}}, which turns your code into:

package Foo; ... sub doStuff { my $self = shift; foreach my $obj (@{$self->{objs}}) { $obj->doSomethingElse(); } }

Replies are listed 'Best First'.
Re^2: Iterating over an array of objects, within an object?
by GrandFather (Saint) on Dec 19, 2006 at 02:07 UTC

    Actually I said it in one line, although I should have put that line at the top of my reply rather than at the bottom.

    The code not only illustrates the fix, but in a subtle fashion shows that you can have objects without packages in seperate files, or even requiring full blown constructors - and realising that can be a real Ahh moment.


    DWIM is Perl's answer to Gödel