Stories
Slash Boxes
Comments
NOTE: use Perl; is on undef hiatus. You can read content, but you can't post it. More info will be forthcoming forthcomingly.

All the Perl that's Practical to Extract and Report

The Fine Print: The following comments are owned by whoever posted them. We are not responsible for them in any way.
 Full
 Abbreviated
 Hidden
More | Login | Reply
Loading... please wait.
  • And not to be fighting this debate in multiple domains, but just for the record, here's my contribution to P5P:
    Here's what I said on an upcoming SysAdmin column (archived at my site [stonehenge.com], but not available yet per publisher embargo):
        And now, before I run out of space, let me touch on a hot-button for
        me.  The I<perltoot> manpage contains an archetypal C<new> routine
        that looks like:

          sub new {
            my $proto = shift;
            my $class = ref($proto) || $proto;
            my $self  = {};
            ...
          }

        The purpose of these few lines of extra code is to permit:

          my $other = $dog->new;

        to act like

          my $other = (ref $dog)->new;

        But here's the problem.  When I survey experienced object-oriented
        programmers, and ask them what they expect C<new> means when called on
        an instance (without looking at the implementation), the result
        usually divides rather equally into three camps: those that go "huh,
        why would you do that" and think it should throw an error, those that
        say that it would B<clone> the object, and those that say it would
        B<copy> the object's class but not the contents.

        So, no matter what you intend if you make your C<new> do one of those
        three things, two thirds of the people who look at it will be wrong.
        It's not intuitive.  So, don't write code like that, and especially
        don't just cargo-cult that from the manpage into your code.  If you
        want an object B<like> another object, use C<ref> explicitly, as shown
        above.  If you want a clone, put cloning code into your package, and
        call C<clone>, as we saw earlier.
    I'll let these words stand as probably the clearest rendering I've had on the subject so far. :)
    --
    • Randal L. Schwartz
    • Stonehenge
    • There was a similar discussion at one point in a corner of the Smalltalk community, but without the "it should make a copy" camp (since folks in the Smalltalk community were painfully aware of the differences between a deep and a shallow copy). As I recall, the concensus opinion was that it's easy enough to say

        newObject := someObject class new

      which is the equivalent of

        $newObject = (ref $someObject)->new

      The only instance-side new I could find when I went looking through an old VisualWor

      • Actually, I believe the proper idiom in Smalltalk-80 (and thus Squeak) is: newObject := someObject species new because "species" returns "the class that can make something like this". "species" defaults to "class" for most classes, but check to see where it's defined (and thus overriding class): it's quite fascinating.
        --
        • Randal L. Schwartz
        • Stonehenge