
framp
Novice

Apr 10, 2009, 4:24 AM
Post #1 of 6
(692 views)
|
|
[solved] Problem to call an object method on objects collected in an arry
|
Can't Post
|
|
Hi, I'm new in the world of Perl - but not in the world of programming languages and OO . I want to rewrite a huge bash script and use OO Perl to learn and practice Perl. I already created a lot of OO Perl sample code to get familiar with Perl and now I'm struggling with a weired problem. I have a collection of objects in an arry and want to loop over the collection of objects and call a method on all objects. It works when I use a plain array. When I use Class::Std (as suggested by Conway in his Best Practices) to encapsulate the object data of my class I get Can't call method "collect" on unblessed reference at . That's the code which works:
my $ic1 = InterfaceCollector->new( { name => "IfCollector1" } ); my $ic2 = InterfaceCollector->new( { name => "IfCollector2" } ); $ic1->collect(); # explicit method call $ic2->collect(); my @collectorList; push @collectorList, $ic1; push @collectorList, $ic2; foreach my $collector ( @collectorList ) { # method call to all objects in collection $collector->collect(); } That's the code which doesn't work:
package NwDataCollector; use Class::Std; use Data::Dumper; { my %name : ATTR( : name => 'name' ); my %collectorList : ATTR( : get => 'collectorList' ); sub BUILD { my ( $self, $ident, $arg_ref ) = @_; $collectorList{ ident $self} = (); print "*** colletcorList after initialization\n"; print Data::Dumper->Dump([$collectorList{ident $self}]); return; } sub run { my ($self) = @_; print "*** " . $self->get_name() . " started ***\n"; foreach my $collector ( $self->get_collectorList() ) { $collector->collect(); # <----- boom, unblessed reference } } sub add_collector { my ( $self, $collector ) = @_; print "Adding " . $collector->get_name() . "\n"; push @{$collectorList{ident $self}}, $collector; # push @{$self->get_collectorList()},$collector; print "*** collectorList in add_collector\n"; print Data::Dumper->Dump($collectorList{ident $self}); } } "Really, I'm not out to destroy Microsoft. That will just be a completely unintentional side effect." Linus Benedict Torvalds, 28.9.2003
(This post was edited by framp on Apr 11, 2009, 12:23 AM)
|