in reply to Re: What does the SHIFT bit do in a constructor
in thread What does the SHIFT bit do in a constructor

Note that many people who have used OO programming for a very long time (starting with merlyn who has been using it since the early 80's) detest the use of ref there. Object methods and instance methods are different, no need to confuse them. That said I would write the above as:
sub new { my $self = bless {}, shift; @$self{'PEERS', 'NAME', 'AGE'} = []; return $self; }
which looks cleaner to my eyes. YMMV.

Replies are listed 'Best First'.
(tye)Re: What does the SHIFT bit do in a constructor
by tye (Sage) on May 26, 2001 at 00:00 UTC

    In the abstract, I understand and somewhat agree with this criticism. But when you get into a specific object implementation, it often makes a great deal of sense.

    It comes down to whether the reader will clearly understand what: my $other= $object->new(); is meant to do. I find that for many specific object implementations, this is a quite natural idiom. So I agree that you should think about what that idiom is supposed to mean in your specific class before you decide either way!

    A similar criticism is that you should not name your constructors "new" either. Again, I agree with this in the abstract but find that when I get down to real cases, the criticism usually doesn't apply. For many objects, trying to find a more descriptive name for the constructor that describes how I want to go about creating the object just adds confusion.

            - tye (but my friends call me "Tye")