Dear monks,

I'm writing a method which removes the first argument of @_ if it is the blessed reference to the package or the class name / package, thus allowing several call syntaxes to be used for static methods (Not requiring any instance variables). I've had a quick super search but couldn't find anything.

Method example: ( Classic pow example ):
package Object; { # pow does not require an object data sub pow { my $val = shift; my $pow = shift; return $val ** $pow; } } 1;

I can quite happily call this method like so:
use Object Object::pow( 2, 2 );
or exported ( With some code added to the module):
use Object 'pow'; pow( 2,2 );


I thought it would be nice to call such method as any of the following:
Object->pow(2,2); Object::pow(2,2); my $obj = Object->new(); $obj->pow(2,2); pow(2,2); # Exported or internal method call

To accomplish this I wrote a method which filters the arguments:
sub rmvObjRef { my $package_name = shift; my $param1 = ref( $_[ 0 ] ) || $_[ 0 ]; # If param1 holds the name of the class / package # remove it from the arguments. if ( $param1 eq $package_name ) { shift @_; } return @_; }
or the one line equivalent to use inline:
shift if ref $_[0] eq __PACKAGE || $_[0] eq __PACKAGE__;
Which is called as follows.
@_ = rmvObjRef( __PACKAGE__, @_ );
The only draw back to my approach I can see is if the arguments contain the object reference which the method has been declared in at $_[0]:
package NumObject; sub addObjects { @_ = rmvObjRef( __PACKAGE__, @_ ); # Just an example, Obviously an overloaded '+' # op could achieve this. return NumObject + NumObject; }
Calling this as:
my $num1 = NumObject->new(); my $num2 = NumObject->new(); NumObject::addObjects( $num1, $num2 );
would remove $num1 from @_
Is there any way to just check just the left argument of the infix dereference operator "->"?

Many Thanks,
John,

In reply to Remove bless reference or class/package name from method arguments by binf-jw

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.