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

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.