in reply to Re:^2 $self->{foo} in sub new {}
in thread $self->{foo} in sub new {}
It is usually a good idea to use the $class = ref($class) || $class so a new object can be created from an existing object.
It is? Why would you want to do a thing like that?
Yes, Perl lets you do this sort of thing, but unless you're doing some rather unusual factory/exemplar style OO programming it should be avoided. It just makes Perl's objects look more mysterious than they are.
Sometimes I have wished to clone an object, but that is another issue entirely.
Certainly if you have lots of objects of different packages being created dynamically and delegated to other objects and the package name is not be readily available, then sure, this is the way to go, but I suspect that this is not the case here. Think of it from the caller's point of view: are they able to say PackageName->new($foo) instead of $pn->new($foo)? Usually the answer is yes, so the ref($class) trick is not needed.
|
|---|