Your first example solution would probably be the way I might do this (although my step 0 would be to question this particular requirement ;-) ), although I'm not sure what you mean by "does not deal with case 2" unless your case 2 is a typo and was supposed to be Utility->myUtility(@params) instead of Utility::myUtility(@params)? In that case, you might have to go with something more like shift if @_ && (blessed($_[0]) && $_[0]->isa(__PACKAGE__) || $_[0] eq __PACKAGE__); shift if @_ && (!ref $_[0] || blessed $_[0]) && $_[0]->isa(__PACKAGE__); (Updated after choroba showed an even better way, calling UNIVERSAL's isa as a class method)
I'm also not sure if that's really too "expensive". Sure, you could do trickery like my ($foo, $bar) = cond ? @_[1..$#_] : @_; or even my $I = cond ? 1 : 0; my $foo = $_[$I+0]; my $bar = $_[$I+1];, but that's getting pretty ugly and unwieldy. Or, if you wanted to get really creative, have &Utility::myUtility always discard its first argument, and but when you export it to someone else's namespace so they can call it as in your third example, actually export a wrapper sub that inserts an extra argument before calling &Utility::myUtility (Update: kind of like LanX has shown in the meantime). But whether all this is really necessary depends on what your benchmarks and profiling show about whether the above shift method is "too expensive".
In reply to Re: Perl OOO Function Entrance
by haukex
in thread Perl OOP Function Entrance
by Mano_Man
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |