Welcome to perl. Grandfather is right, objects are, by definition, references in Perl. I thought I'd give you some additional feedback on your other code.
It's good to see that you are using strict and warnings>, and the two argument form of bless in your creator functions. Keep it up!
In most places you are explicitly unpacking @_ using shift. That's my preferred practice, and TheDamian recommends it Perl Best Practices. Others seem to like to use list assignment to collect arguments. Direct access can be risky since perl sub arguments are passed by reference.
sub upack_with_shift { my $foo = shift; my $bar = shift; my $baz = shift; .... DO STUFF ... } sub list_assignment { my ( $foo, $bar, $baz ) = @_; } sub direct_access { $_[0] = "PROCESSED"; $_[1] += $_[2]; $_[2] = undef; return 1; } # direct access is dangerous. Perl passes sub arguments by reference. # it is possible to change values in a calling function's variables if + you aren't careful. my use_da { my @foo = ('', 5, 7); my $result = direct_access( @foo ); print join ', ', $result, @foo; # prints: 1, PROCESSED, # generates a warning if warnings on. "Use of uninitialized value + in join at line mumble" }
Some people like to separate accessor and mutator methods. I like combined methods, such as you experiment with above. For combined methods, it is better to look at magnitude of the argument list, than it is to check for definedness when determining whether we are getting or setting. This way it is possible to say things like $foo->attribute(undef); and have attribute cleared.
#Accessor sub Name { my $self = shift; if (@_) { # returns true if the arg list contains any more elements, + regardless of their values. $self->{NAME}= shift; } return $self->{NAME}; }
All this code is untested, so there may be typos that I've missed.
You've make a great start, keep up the good work.
TGI says moo
In reply to Re: Aggregation problem
by TGI
in thread Aggregation problem
by pankajisgarg
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |