Beefy Boxes and Bandwidth Generously Provided by pair Networks
Syntactic Confectionery Delight

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)

Replies are listed 'Best First'.
(tye)Re: The Dynamic Method Calls Strike Back
by tye (Sage) on Aug 28, 2001 at 02:26 UTC
            - tye (but my friends call me "Tye")
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:
    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!

      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.

      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 :-(
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?

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

How do I use this? | Other CB clients
Other Users?
Others making s'mores by the fire in the courtyard of the Monastery: (7)
As of 2019-06-26 07:57 GMT
Find Nodes?
    Voting Booth?
    Is there a future for codeless software?

    Results (109 votes). Check out past polls.