Beefy Boxes and Bandwidth Generously Provided by pair Networks
Come for the quick hacks, stay for the epiphanies.
 
PerlMonks  

Re: OO sub, hash or hash reference.

by dragonchild (Archbishop)
on Feb 18, 2002 at 15:07 UTC ( [id://146158]=note: print w/replies, xml ) Need Help??


in reply to OO sub, hash or hash reference.

There's a few ways you could do it. I'd do it as such:
sub someMethod { my $self = shift; my $hashRef; if (UNIVERSAL::isa($_[0], 'HASH')) { $hashRef = shift; } else { $hashRef = { @_ }; } }
Of course, you can add more checks and lots of cool stuff. But, that's basically the idea.

------
We are the carpenters and bricklayers of the Information Age.

Don't go borrowing trouble. For programmers, this means Worry only about what you need to implement.

Replies are listed 'Best First'.
Re: Re: OO sub, hash or hash reference.
by dreadpiratepeter (Priest) on Feb 18, 2002 at 15:11 UTC
    Out of curiosity, why the isa instead of a ref($_[0]) eq 'HASH'?

    -pete
    Entropy is not what is used to be.
      after seeing the above code I did some reading:

      "The reason the original test is fragile ( ref($_[0]) eq 'HASH' ) is that it will make your code refuse to work on a hash if it happens to be blessed."

      It was simpler than I thought.

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others romping around the Monastery: (7)
As of 2024-04-19 14:03 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found