Beefy Boxes and Bandwidth Generously Provided by pair Networks
more useful options
 
PerlMonks  

Re: Methods supporting both package and OOP style calls

by frag (Hermit)
on Aug 06, 2002 at 04:38 UTC ( [id://187922]=note: print w/replies, xml ) Need Help??


in reply to Methods supporting both package and OOP style calls

The magic you seek is UNIVERSAL::isa.
if (UNIVERSAL::isa($self, __PACKAGE__)) { print "1st arg was object OR package name: "; $text = shift; } else { print "1st arg is just an arg: "; $text = $self; };

I concur with dragonchild though: pick some subset of the 3 possible WTDI (function, class method, object method) and stick to it, and make it clear in the docs which is preferred. If people using the module get used to "Foo::bar(arg)" then what if some subs really must have the object passed in? Will it be easy to remember which form goes with which subroutine?

Personally, I think the sensible way is: public subroutines should be all methods or all functions (in which case it isn't an object package), or else mostly methods with a simple exported procedural function wrapping the most central method (see Data::Dumper). Private subs can vary freely, depending on if they manipulate object/class data or not. (And note this all assumes that speed isn't much of a factor - procedural function calls are always faster than methods.)

-- Frag.
"Never could stand that dog."

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others about the Monastery: (4)
As of 2024-04-19 22:56 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found