in reply to looping over class instances

There's nothing built in. If you want to do this then you'll need to have your constructor stash a copy of each instance as it's built somewhere. Then provide a class method to return or walk that list of instances.

package Foo; my @_instances; sub new { ... push @_instances, $self; return $self; } sub zorbnitz { print "Weeble\n"; } sub for_All_Foos { my $class = shift; my $code = shift; $code->( $_ ) for @_instances; } package main; $a = Foo->new; $b = Foo->new; Foo->for_All_Foos( sub { $_[0]->zorbnitz } );

Or if you use Class::MethodMaker I believe it'll write something similar for you if you tickle it correctly (I believe its the key specifier).

Replies are listed 'Best First'.
Re: Re: looping over class instances
by liz (Monsignor) on Jan 31, 2004 at 00:39 UTC
    push @_instances, $self;

    Because you're saving a reference to the object, the object will never get DESTROYed, which may not be what you want.

    You may want to have a look at Scalar::Util's weaken() function, which allows you to save references without interfering with the lifetime of objects.

    Liz

Stashing references (Re: looping over class instances)
by jonadab (Parson) on Jan 31, 2004 at 01:28 UTC

    Here be dragons, I think. Especially if you're doing this in a module that other people are going to use without worrying about the details of your implementation. I think you maybe ought to put some big hairy warnings in your documentation to the effect that your objects won't necessarily play nicely with code that treats them in unexpected ways (such as Clone::PP, which is *extremely* useful in a wide assortment of situations but might result in objects that are not on your list).

    Of course, I could be mistaken. I don't actually know, for example, how Clone::PP works internally, but that's just what came to my mind off the top of my head. Whether that's one of them or not, it seems very likely to me that there are ways in which your list of objects of the class might get broken.

    I'm hoping against hope that Perl6 will have an object forest like Inform has. If so, that would make it possible to walk through every object in the forest and check each one to see if it's a member of the class in question. (This would not be terribly efficient, but it would be possible. Inform programmers do it from time to time, for one reason or another. Though in Inform there's no list of objects on the forest floor, I would imagine that Perl6 would provide this, if it goes to the trouble to implement he forest in the first place. Inform also provides a way to enumerate every object in existence, irrespective of its position in the forest, but that is due to a lowlevel implementation detail and I would *not* expect Perl6 to have that. Which is the main reason I would expect it to keep a list of all the toplevel objects.)


    $;=sub{$/};@;=map{my($a,$b)=($_,$;);$;=sub{$a.$b->()}} split//,".rekcah lreP rehtona tsuJ";$\=$ ;->();print$/

      Good points in both above posts. As to Clone::PP, glancing at the docs it says it'll call clone_self to do cloning for the object in question and/or clone_init after the clone's done. Of course you'd also want to look into STORABLE_thaw for Storable and Thaw for FreezeThaw for completeness.

      As for forests, Ruby also provides something similar where you can do ObjectSpace.each( MyClass ) { |instance| ... }.