Beefy Boxes and Bandwidth Generously Provided by pair Networks
go ahead... be a heretic

Comment on

( #3333=superdoc: print w/replies, xml ) Need Help??
merlyn has railed against this construct for years. Do a Super Search for "cargo-cult programming" and you'll find hundreds of nodes on the topic.

Basically, that construct is saying "This method can be called as either a class or instance method". While this may be neat (for example, Test::Cmd takes advantage of this by inheriting from File::Spec), it's certainly not helpful in most cases.

But, I must take exception with one of the conclusions you're implying. The error in line 14 is caused by the fact that your new() method isn't verifying that it was actually passed anything. The better solution is:

package Foo; sub new { my $proto = shift || die "Must pass a class or object into new()"; my $class = ref($proto) || $proto; bless {}, $class; }

Of course, this still doesn't fully work because it doesn't take into account Foo::new( {} );, which will return an object blessed into the HASH class. So, maybe we should do something like:

package Foo; use Scalar::Utils qw( blessed ); sub new { die "Must pass a class or object into new()" unless @_; my $proto = shift; my $class = blessed($proto) or $proto; bless {}, $class; }

That should take care of most situations I can think of. But, we're just getting stupid. :-)

We are the carpenters and bricklayers of the Information Age.

Then there are Damian modules.... *sigh* ... that's not about being less-lazy -- that's about being on some really good drugs -- you know, there is no spoon. - flyingmoose

I shouldn't have to say this, but any code, unless otherwise stated, is untested

In reply to Re: Is "ref($class) || $class" a bad thing? by dragonchild
in thread Is "ref($class) || $class" a bad thing? by stvn

Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post; it's "PerlMonks-approved HTML":

  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.
  • Log In?

    What's my password?
    Create A New User
    [Mr. Muskrat]: Oh that is odd. I got the message that it was reparented but yeah, it didn't actually do it. lol
    [Corion]: LanX: No, Co-Rion appears as Monkey God :-)
    [Corion]: Mr. Muskrat: Yeah, that message is automatic when the "normal" reparenting is done, but I think some rows that make up the node were not added to the DB properly

    How do I use this? | Other CB clients
    Other Users?
    Others wandering the Monastery: (14)
    As of 2017-01-19 16:30 GMT
    Find Nodes?
      Voting Booth?
      Do you watch meteor showers?

      Results (170 votes). Check out past polls.