in reply to method invocation syntax at perl

Yes, it can be done, but I wouldn't recommend it:

$obj->${\EXPR}(ARGS)
For instance:
use strict; sub foo { "@_" } my ($x, $y) = qw/ f oo /; print __PACKAGE__->${\($x . $y)}('FOO'); __END__ main FOO

lodin

Replies are listed 'Best First'.
Re^2: method invocation syntax at perl
by braveghost (Acolyte) on Jan 03, 2008 at 13:21 UTC
    Thanks a large. This exactly thing i want.
    for my case it will be:
    print $l->${\($str.$str1)}($str2);
    But why don't you recomend this solution?

      For one thing if the two strings are coming from an outside source you've just given them a means to call an arbitrary method in your code; granted it's not a gaping hole, but that's generally enough to raise the hackles of the security conscious.

      That aside, there's also no error checking so if anything does go wrong you merely get the generic "unknown method" error message. Better would be to explicitly validate that it's an allowed method name (either through a dispatch table or hash of valid method names, or at the least through using $obj->can( $method_name ) to check that the method will be callable). If it's not then you can explicitly print your own error message with the two distinct parts of the method name (so you don't have to go diving through the debugger to try and determine if someone tried to call with "xxy4" and "4", or "xxy" and "44", or "xx" and "y44", or . . . ).

      The cake is a lie.
      The cake is a lie.
      The cake is a lie.

        Thank you very match. As i says it's just an example, but i will add some more security checks using your advice.
        Thanks again.