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

In Perl, TIMTOWTDI, There Is More Than One Way To Do It :-)

However, if your constructor has more than ~3 arguments, a hash is probably better - especially if some arguments are optional.

Secondly, is there a way to tell how many objects have been created/exist in a particular class?

Again TIMTOWTDI, a quick implementation might have a global variable in the package as a counter, which gets incremented in new and decremented in DESTROY. If there is more than one way to create an object and/or there are subclasses, one has to watch out that those are covered.

And lastly, what is the best way to access individual object attributes? Always through a method or can you access them directly?

Again, it depends. If your class is just a blessed hash, there is no practical way from stopping anyone to dig into the hash other than telling people not to in the documentation. However, from outside the class, it's normally preferred to abstract it and go through accessors, because that hides the internal storage details and allows for things like validation. From inside the class, how you access fields depends on whether the accessors do more than just get/set a value, such as validation.

Your code looks ok. I'd just recommend adding individual accessors for the fields, the change_nums and dump methods kind of break encapsulation. Also, for real code, you'd probably want to put each class in its own file; at the very least you should put the package in its own block ({ package Foo; ... }).

The Perl documentation contains more info on OO, a good starting point is probably perlootut.


In reply to Re: OO best practice basic questions by Anonymous Monk
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.