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

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.