pankajisgarg has asked for the wisdom of the Perl Monks concerning the following question:

Hi I am perl newbie, i am trying to achieve aggregation in Perl. Here is representative of what i wan tto achieve i.e. pass reference of a class (Person) to another class (Updater) and call Person (updateName)method using the passed reference, but when i try to do so i get "Not a HASH reference at" error. I tried few things but no use, niether could find similar code anywhere.
package Person; use strict; use warnings; sub new { my $class = shift; my $self = {}; $self->{NAME} = shift; bless ($self, $class); return $self; } sub getPerson { my $o = Person->new ("TestName"); return $o; } sub createUpdater { my $self = shift; my $o = Person2->new (\$self); return $o; } #Accessor sub Name { $_[0]->{NAME}=$_[1] if defined $_[1]; $_[0]->{NAME}; } package Updater; use strict; use warnings; sub new { my $class = shift; my $self = {}; $self->{Person} = \shift; bless ($self, $class); return $self; } sub updateName { my $self = shift; #my $pe1 = \$self->{Person}; #bless ($pe1, "Person"); my $pe1 = $self->{Person}; $self->{NAME} = $pe1->Name; } 1;

Replies are listed 'Best First'.
Re: Aggregation problem
by GrandFather (Saint) on Dec 26, 2007 at 21:12 UTC

    $self->{Person} = \shift; in Updater->new should be    $self->{Person} = shift;. The Person object is already a reference. Given that change the following code:

    package main; my $him = Person->new ('Bob wibble'); my $ud = Updater->new ($him); $ud->updateName (); print $ud->{NAME};

    prints:

    Bob wibble

    Perl is environmentally friendly - it saves trees
Re: Aggregation problem
by TGI (Parson) on Dec 26, 2007 at 23:08 UTC

    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

    A reply falls below the community's threshold of quality. You may see it by logging in.