in reply to Methods supporting both package and OOP style calls
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."
|
---|