in reply to Re: Re: Re: Philosophical Perly Queues
in thread Philosophical Perly Queues
Yeah its right.. but why? when? where? If you shrug don't use it. How many modules will break for no apparent reason due to that one singular line?
I don't think that would be right anywhere, and certainly not in a consturctor. Perhaps you meant the opposite?
my $class=ref($proto) || $proto;
which AFAICT, wouldnt break any code at all. Why would it? The primary objection to this idiom (assuming you mean when it is used in a new()) is one of semantic meaning. Class->new() has a clear meaning, but $obj->new() doesn't really. Does it mean to copy the object? Does it mean to create a new object of the same type as the old? Does it mean something deeper?
This is primarily an issue that detailed documentation resolves nicely. I would say that you could replace my $class=shift with my $proto=shift; my $class=ref($proto) or $proto; just about everywhere without a failure.
One trap with using this idom (and about the only real trap regarding that I am aware of) is when someone accidentally mixes indirect notation with direct notation, or calls new in a strange way
my $Obj=new Class->new(@args); my $New=Class->new(@argS)->new()->foo(); # probably an error
most likely will create an initialized object, then use that to create an unitialized object. A hard to track down error that is thankfully rather uncommon as well.
|
---|