http://qs321.pair.com?node_id=339404


in reply to Re^5: It's a dog, but what kind? (polymorphism , in Perl OO) (!abstract)
in thread It's a dog, but what kind? (polymorphism , in Perl OO)

The alternative is that if you want a copy or clone operation, they should be seperate methods from the constructor.

----
: () { :|:& };:

Note: All code is untested, unless otherwise stated

Replies are listed 'Best First'.
Re^7: It's a dog, but what kind? (polymorphism , in Perl OO) (alternative?)
by tye (Sage) on Mar 24, 2004 at 15:41 UTC

    That is so far off from what I was talking about as far as what I meant by "alternative" and completely misses the point of why the obvious alternative is unacceptable to me that I suspect you did not bother to read Re^2: A few Perl OOP questions. (disparaging).

    Normally I'd try to steer back toward that but I'm just not coming up with anything. In fact, I'm not sure you even read and understood all of what I wrote in this thread.

    - tye        

      And tye, back at ya. Did you understand the responses you got to that posting, or the thread in this posting?

      The "alternative" is the simple approach:

      sub new { my $class = shift; my $self = { ... }; bless $self, $class; }
      None of the extra cut-n-pasted stuff to make $instance->new do anything in particular, because as I said in my quoted item above, it's wrong to two-thirds of the audience, and therefore is dangerous.

      If that's confusing to you, I wish you could say what you're missing in a different way, because I'm not getting it.

      -- Randal L. Schwartz, Perl hacker
      Be sure to read my standard disclaimer if this is a reply.

        Yes, you aren't getting it (on at least two counts, it appears). Yes, I understood your response and even responded to it. I linked to where I explain why I consider the alternative above to be unacceptable (it makes a simple mistake hopelessly confusing).

        I wasn't particularly terse in my explanations so I won't rehash them further (I doubt I'd be successful in conveying the points to you based on how completely I've failed so far). Maybe some other time we'll be closer to the same "wavelength".

        - tye        

      Re^2: A few Perl OOP questions. (disparaging) shows what happens when the common Perl idiom $class = ref $class || $class isn't followed and someone expects it to be implemented that way. I would thus argue that someone expecting new to act like clone when it isn't explicitly documented is relying on implementation details of the class. Even if it is documented, it's still a bad idiom.

      If you want alternatives, here they are:

      package Foo; use base 'Clone::PP'; # And don't bother writing it yourself sub new { my $class = shift; my $self = { }; # Intitilze $self bless $self, $class; }

      Although people shouldn't be expecting the constructor to make a clone or copy anyway, this idiom is so common that it may be wise to explicitly document that your constructor won't work that way. Better yet, have new die if it receives a reference.

      ----
      : () { :|:& };:

      Note: All code is untested, unless otherwise stated