Pathologically Eclectic Rubbish Lister | |
PerlMonks |
Re^3: OOP first-timer shouldn't use ref $proto || $proto :)by Ovid (Cardinal) |
on Sep 09, 2005 at 00:34 UTC ( [id://490379]=note: print w/replies, xml ) | Need Help?? |
The problem with the constructor is the ref $proto || $proto line. What's that for? It allows you to do this:
The problem is, unless you document why you need that, it's very unclear. Is $object2 a brand-new, clean object with no relation to the first object? If so, why are you calling new as an instance method? Some people might think it's supposed to clone the first object. If so, is it a shallow or deep copy? Is there a reason in the code to clone the first object? If it creates a brand new object, either only allow new as a class method or, if you don't have the class handy (maybe it's generated from a factory), then document it like this: my $object2 = (ref $object)->new;If it really is a clone, create a clone method and avoid the confusion. Since few people can agree on the semantics of $object->new, it's a source of confusion. That being said, use it if you need it, but make it clear why you're doing this. In fact, this has been a source of enough confusion and argument that I removed most references to this in the Perl docs. Cheers, New address of my CGI Course.
In Section
Seekers of Perl Wisdom
|
|