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
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |