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.
  • I propose a change instead of a removal,

    ref ($proto) || $proto;

    should be changed to:

    ref ($class) || $class;

    This would keep it in a OOP wording and possibly make it more essential for Perl-OOP beginners to understand what is going on.

    The construct is quite essential (I personally always use it).

    If you use inheritance, the result of not using could be strange since many people would perhaps not understand why their inheritance does not work and errors, which might be quite difficult to debug will occur

    • No, I doubt this was the kind of thing he was looking for. You should use 2-argument bless, but not one this complex. The form you ought to be using should simply look like

      sub new {
          my $class = shift;
          my $self = ... # any kind of ref, any data
          return bless $self, $class;
      }

      The idea is that it's bad to make one constructor both for actually making a new object (class method), as for cloning (object method). Probably nobody ever uses new() as an object method, I'm sure.

    • Unfortunately this is a classic example of not understanding it - just copying it.

      This has nothing to do with inheritance or the two arg form of bless. It only has to do with being able to call ->new() on an instance of the class, and whether or not that's a good idea.

      Most people will never ever do this (as what they really want is a copy constructor, not a "call new on the object" method). I agree that all of these should be removed from the docs forthwith, but not just the docs, I think they should b
      • Then I must have misunderstood what Damian wrote... maybe I should reschedule a re-reading of that book.

        So this does relate to cloning of objects as bart writes in his comment earlier.

        Hmm don't mind me - I just need to do some reading and experimenting...
      • Ok, now I read the docs on bless and AFAIK I can see I copied something without clearly understanding it - WHOOPS!

        Well the recommended syntax mentioned in the documentation should perhaps read:

        bless ... , (ref class);

        Instead of:

        bless ... , (ref class) || $class;

        It is custom to buy a kvajebajer in Denmark if you make an obvious mistake like this, so Matts or bart if I meet you some point remind me that I owe you a beer.

        And perhaps an excuse to MJD for not paying attention :)

        • Sorry,

          The recommended construct should of course read:
          bless ... , $class;
          Hence the ref and || are the elements, which should be removed.
    • No offense, but I think your suggestion of changing it to ref ($class) || $class; actually demonstrates the problem we're trying to solve. Specifically, this is bad:

      sub new {
        my $class = shift;
        $class = ref ($class) || $class;
        bless {}, $class;
      }

      I would mark that down on a code review because in this case, the line in controversy makes it clear that you expect to be able to pass in either $self or $class. Since you don't know what it is when you call the constructor, having my $class

  • 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 =

    --
    • 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