Beefy Boxes and Bandwidth Generously Provided by pair Networks
Problems? Is your data what you think it is?
 
PerlMonks  

Remove bless reference or class/package name from method arguments

by binf-jw (Monk)
on May 20, 2009 at 10:04 UTC ( #765168=perlquestion: print w/replies, xml ) Need Help??

binf-jw has asked for the wisdom of the Perl Monks concerning the following question:

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,

Replies are listed 'Best First'.
Re: Remove bless reference or class/package name from method arguments
by moritz (Cardinal) on May 20, 2009 at 10:32 UTC
    shift if ref $_[0] eq __PACKAGE || $_[0] eq __PACKAGE__;

    That's a bad idea because it breaks inheritance. I'd do this instead:

    shift if eval { $_[0]->isa(__PACKAGE__) }:
      That did cross my mind thanks.
      Would you need to implement a custom isa method that searches @ISA?
      Or is there a built in? can't find anything on perldoc.
      John,
        The isa method is already there, provided by the UNIVERSAL class, and can be called without any further ado:
        $ perl -wle 'print +(bless {})->isa("main")' 1
Re: Remove bless reference or class/package name from method arguments
by duelafn (Vicar) on May 20, 2009 at 10:29 UTC

    For these particular examples, argument counting would be sufficient.

    sub pow { shift if @_ > 2; return $_[0] ** $_[1]; }

    If the number of arguments is unknown (e.g., sum), I do not know how (and do not believe that it is possible) to determine how the sub was invoked.

    Good Day,
        Dean

      Or just
      sub pow { return $_[-2] ** $_[-1]; }

      Approaches that rely on the number of arguments rather than the type of arguments are going to be more resilient.

      Thank you Dean,

      For most of the implementations I can use argument counting, infact I commonly pass multiple arguments as a HASH reference which kind of solves the problem. Variable size lists can even pass as an ARRAY reference:
      $obj->staticMethod( [ $arg1, $arg2, $arg3 ] );

      I was hoping there was some funky way of getting how it was invoked just for curiousity.

      John,
Re: Remove bless reference or class/package name from method arguments
by Bloodnok (Vicar) on May 20, 2009 at 10:43 UTC
    When overloading binary operators e.g. '+', perl calls the overloading sub/method with 3 arguments - the 2 operator arguments (as objects) and a flag indicating whether perl has swapped the 2 arguments before calling the sub/method (see overload - as you probably already have :-). Therefore, your use of rmvObjRef() is incorrect in this context.

    Consider:

    use NumObject; my ($a, $b) = (NumObject->new(), NumObject->new()); my $c = $a + $b; my $d = $a->addObjects($b); my $e = NumObject::addObjects($a, $b);
    In the above, the assignments to $c, $d & $e result in slightly different invocations of addObjects(), but will all have the same post-run value - assuming the call to rmvObjRef() is removed/commented out.

    A user level that continues to overstate my experience :-))
      Yeah I know about the swap property (1,0 or undef), I added that comment as I thought someone might tell me you'd want to overload '+' to call addObjects and was covering my back ( Infact that method would cause deep recursion ).
      John,
Re: Remove bless reference or class/package name from method arguments
by bsb (Priest) on May 20, 2009 at 14:28 UTC
    The technique from Give me something $self-ish might be of use.

    (The isa test might be done a better way, eval wrapped as mentioned in the other replies)

Re: Remove bless reference or class/package name from method arguments
by phaylon (Curate) on May 20, 2009 at 22:46 UTC

    May I ask, if it isn't tied to the class, why is it a method in the first place?


    Ordinary morality is for ordinary people. -- Aleister Crowley
      Static methods have a long history in OOP. Just because it operates at the level of the class without referencing any details of a specific instance doesn't mean it's not tied to the class.

        Alright then. I think I still wouldn't want to do this though, but merely out of maintainability issues. Foo::bar and Foo->bar are not the same, and I wouldn't expect them to be. After all, if you'd want to make a method that accepts an instance as first argument as the invocant, it wouldn't work anymore.


        Ordinary morality is for ordinary people. -- Aleister Crowley

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://765168]
Approved by citromatik
Front-paged by Arunbear
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others pondering the Monastery: (3)
As of 2020-10-22 07:39 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    My favourite web site is:












    Results (225 votes). Check out past polls.

    Notices?