Beefy Boxes and Bandwidth Generously Provided by pair Networks
Pathologically Eclectic Rubbish Lister
 
PerlMonks  

Re^2: Is "ref($class) || $class" a bad thing?

by hardburn (Abbot)
on Jul 12, 2004 at 16:29 UTC ( #373650=note: print w/ replies, xml ) Need Help??


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

Basically, that construct is saying "This method can be called as either a class or instance method".

merlyn's writings have been against using this in constructors specifically, not against using it in general methods to allow it to be called against both a class and an object. This is because a chunk of programmers out there expect it to act as a clone, another chunk thinks it should act as a copy, and yet another chunk thinks it should be an error.

I go for something like this:

sub new { my $class = shift; die "Need to be called as a class method" if ref $class; my %params = %{ +shift }; # Define what params we expect my %self = map { $_ => $params{$_} || '' } qw/ foo bar baz /; # Validation bless \%self => $class; }

----
send money to your kernel via the boot loader.. This and more wisdom available from Markov Hardburn.


Comment on Re^2: Is "ref($class) || $class" a bad thing?
Download Code

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://373650]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others taking refuge in the Monastery: (16)
As of 2014-12-19 08:45 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    Is guessing a good strategy for surviving in the IT business?





    Results (75 votes), past polls