Beefy Boxes and Bandwidth Generously Provided by pair Networks
XP is just a number
 
PerlMonks  

The Dynamic Method Calls Strike Back

by frag (Hermit)
on Aug 28, 2001 at 02:13 UTC ( #108291=perlquestion: print w/ replies, xml ) Need Help??
frag has asked for the wisdom of the Perl Monks concerning the following question:

Given that this is OK:
$method = "my_method_name"; $object->$method();
the following isn't, and causes compilation errors:
$method = "something"; $object->do_$method();
That is, where you want to append a string to $method, but are trying to avoid having to explicitly throw in the line "$method = 'do_'.$method" before the call.

Now, having to add that line isn't exactly a big deal, but I could've sworn that there is a way to do this, only I'm stuck. I can't tell if either a) I've forgotten something more or less obvious that makes this possible, or b) I'm completely off, and what I'm trying to do just won't fly. Can anyone help me?

-- Frag.
(taking ton's theme and running with it)

Comment on The Dynamic Method Calls Strike Back
Select or Download Code
Re: The Dynamic Method Calls Strike Back
by dragonchild (Archbishop) on Aug 28, 2001 at 02:17 UTC
    If you want to stay within strict 'refs', you're going to have to do something like:
    my $method = "do_something"; $object->$method if $object->can($method);
    Otherwise, you could do something like:
    $object->"do_$something";
    I would HIGHLY not reccommend turning off any part of strict unless you are a "Perl Jedi Master"(tm) and have many Dark Side points to your name.

    ------
    /me wants to be the brightest bulb in the chandelier!

    Vote paco for President!

      $object->"do_$something";
      That won't work. But this will:
      eval "\$object->do_$something";
      But I think its better to prepend the 'do_' before you do the method call.
      No arguments about 'no strict' being a bad idea, but it's the Principle of the Thing that's bugging me.

      -- Frag.

        "no strict 'refs'" is not necessarily a bad idea, as long as you know what you're doing, and confine it to some lexical blocks. Its practically required when you dynamically create subroutines with AUTOLOAD, (where you also use goto which is usually bad too, but standard AUTOLOAD practice). Its just that even 'no strict' won't help you in the example above :-(
      The Dark Side beckons... in Class::Prototyped we used "no strict 'refs'" a mere 19 times...

      If you know why you need it, you should be able to use it. This is the Perl Way. We have the knobs on the back of the set at our disposal.

(tye)Re: The Dynamic Method Calls Strike Back
by tye (Cardinal) on Aug 28, 2001 at 02:26 UTC
    $obj->can("do_".$method)->($obj,@args);
            - tye (but my friends call me "Tye")
Re: The Dynamic Method Calls Strike Back
by chipmunk (Parson) on Aug 28, 2001 at 06:13 UTC
    Just one more way to do it: $object->${\"do_$method"}();
    Oh, and by the way, dynamic method calls are not prohibited by use strict.

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others surveying the Monastery: (14)
As of 2014-12-22 20:22 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    Is guessing a good strategy for surviving in the IT business?





    Results (128 votes), past polls