Slash Boxes
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.
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

    • by Ovid (2709) on 2004.04.02 10:07 (#29842) Homepage Journal

      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 = shift; is misleading, so calling that variable $proto makes it clear that you're passing in a prototype (actually, at my work, I've found that we have a convention of calling it $class_or_self).

      The real problem, though, is not whether this bit of code is useful. It certainly is when you need it. The problem is that it's copied blindly and detracts from learning OO Perl, which is what those docs are about. Having that mysterious ref ($proto) || $proto line in there can strain a new programmers brain, particularly when they see it so much that they begin to think that this is just somehow part of OO, even though they're not sure exactly why. I've pointed this out to programmers who tell me "But it's in the docs!", as if they're some sort of sacred text that should not be deviated from. The intent is to simplify those docs so that programmers can learn OO without getting distracted by bits that they probably don't need.

      It's also interesting that you brought up the two-argument form of bless. I mentioned offhand that the one argument form should probably be removed, but I didn't actually propose that. Now I'm beginning to wonder if I should :)