Re: Mmmm ... cargo cult progamming is nummy!by Anonymous Monk
|on Feb 27, 2002 at 02:41 UTC||Need Help??|
What that's really doing is co-opting your constructor (typically called new()) and using it as a copy function, (typically called copy() or clone()).
In addition, EVERY single time I've seen ref $proto || $proto written, there was no provision made for a copy constructor.
How do you manage to combine those two statements? Why is there a belief that new as an instance method is a clone method? As you say, it's hardly ever (never?) used as one so why do people think that it is? Is it just paranoia due to that one module once did that? Or that someone just invented that idea? Or is it an idea from some other language?
I too have never seen new being used for anything else than a constructor. Or perhaps I have, but it must have been a long time ago if so. Neither have I ever even thought about incorporating a clone functionality into new. But I can understand the objections to using it if many people manage interpret $bar = $foo->new as $bar = $foo->clone.
I also believe that this interpretation is not very probable if new has any arguments. Then you'd typically see that it's not a clone method. $other_dog = $dog->new($name) can hardly be confused as a clone operation, or can it?
Just for fun I wrote a script that (tries to) find usages of $class = ref $proto || $proto or similar constructs. I ran it on all my installed Perl modules. I found quite a few. I rehacked the program to find usages of $obj->new; or similar. I found... none. Perhaps it's not terrific to make this search on these modules, since many of the installed modules doesn't use other objects, so let's not draw hasted conclusions. But perhaps it gives a pointer.
I'm not sure I'll stop using $proto though. It's already a wide-spread style and some people like it. To stop using it would be to enforce a style upon another. It's not an either-or situation. Those that like to use $obj->new can continue if they feel like it. Those that prefer ref($obj)->new can do that as well. As for the confusion argument: if people get confused by seeing $class = ref $proto || $proto they're probably not too familiar with the Perl culture. If they get confused by $other_dog = $dog->new then I'd say the same: this is not common Perl practice and isn't to be expected. The "make a new object with the same class as this object" interpretation is the common interpretation.
The one thing I might "enforce" or at least encourage through providing this usage is that the maintainer should have a fair share of knowledge about the common Perl practices. That I really do want to enforce. (Of course, this argument stands and falls on that new indeed never is being used a clone method -- something I have no reason to doubt in this moment.)