in reply to To be, or not to be, a package? That is the question

bless \( my $scalar ), ref $self || $self;
Please stop cargoculting the ref($self) thing.

Replies are listed 'Best First'.
Re^2: To be, or not to be, a package? That is the question
by Bloodnok (Vicar) on Dec 04, 2008 at 14:12 UTC
    As I haven't got my copy of PBP in front of me, pls advise as to the preferred way.

    TIA ,

    A user level that continues to overstate my experience :-))
      I don't know what PBP has to say on the matter, as its one of the few Perl books I haven't bought. But...

      sub new { my $self = shift; bless \( my $scalar ), ref $self || $self; }
      Is better written as

      sub new { my $class = shift; bless \( my $scalar ), $class; }
      You don't normally get a blessed reference passed into new(). If you're using my $foo = Object::Foo->new, or something similar, its the name of the class (Object::Foo in my example) that's passed into new(). So, my $class = shift; is more logical.
Re^2: To be, or not to be, a package? That is the question
by JadeNB (Chaplain) on Dec 05, 2008 at 16:16 UTC
    According to Wikipedia (and my personal understanding), cargo-cult programming is the superstitious inclusion of code that serves no purpose, merely for the sake of conforming to some remembered template. However, as you say in ref($proto) - just say no!:
    Rather, I'm saying that "new" on an instance could mean either "clone" or "make new one like...", and thus you are confusing at least half your audience, guaranteed.
    Thus you have already described at least two purposes of this construction. The fact that experienced OO programmers don't all agree on which purpose it should serve doesn't make the construct invalid—the idea that the existence of differing possible interpretations means bad code seems (to me) entirely counter to TIMTOWTDI.

    Anyway: even if this construction is a bad, or at least inelegant, idea (which I think that it is), I don't think that automatically makes it cargo-cult programming.