First of all, is there a preferred style for "new" constructors? Specifically one which will be taking hash value/key pairs as attributes?

A summary of Perl Best Practices (TM) can be found on the Best Practices(TM) reference card. It does not make any pronouncements on the argument list for a constructor. The key-value syntax is a good choice, but you probably want to include a mod 2 test (die "Uneven list" if @_ % 2) to make sure the list is actually balanced. A single hash-ref is also a popular choice. See Moose::Manual for how the most standard issue object object system does it.

Secondly, is there a way to tell how many objects have been created/exist in a particular class?
You could add a counter to your constructor, and then provide an accessor to the counter, e.g.
my $obj_count = 0; sub new { my $class=shift; my %params=@_; my $self={}; $self->{$_}=$params{$_} foreach keys(%params); bless($self, $class); $obj_count++; return $self; } sub get_count { return $obj_count; }
with the possibility of also adding a decrement to the destructor.
And lastly, what is the best way to access individual object attributes? Always through a method or can you access them directly?
This one, best practices does weigh in on. Always through a accessor. That allows for a layer of abstraction between the object itself, makes you insensitive to implementation changes, and allows for side-effects and internal mutations of the class if the designer needs them.

Side note: if this is not just a learning exercise, consider using a declarative object system, like Moose or one of its lighter-weight derivatives.


#11929 First ask yourself `How would I do this without a computer?' Then have the computer do it the same way.


In reply to Re: OO best practice basic questions by kennethk
in thread OO best practice basic questions by Amblikai

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.