jeanluca has asked for the wisdom of the Perl Monks concerning the following question:

Dear Monks

Is it possible todo something like
$t1 = 'test' ; $t2 = '123' ; my $obj = new BlaBal ; $obj->{$t1.$t2}() ; # <-- error
so I need to call the function test123 inside the package BlaBla

Cheers

Replies are listed 'Best First'.
Re: concat method inside method call
by almut (Canon) on Jan 25, 2010 at 14:29 UTC

    You could write

    ... my $method = $t1.$t2; $obj->$method() ;
    A reply falls below the community's threshold of quality. You may see it by logging in.
Re: concat method inside method call
by AnomalousMonk (Archbishop) on Jan 25, 2010 at 17:44 UTC

    The comments of almut and other(s) along the lines of "But why...?" should be heeded.
    But if you want to press on regardless, here's a slightly different approach:

    >perl -wMstrict -le "package BlaBal; sub hi { print 'hi there ', $_[0] }; sub new { my $class = shift; return bless { test123 => \&hi } => $class; } package main; my $obj = BlaBal->new; $obj->{ 'test' . (120+3) }->('sailor'); " hi there sailor

    BTW: The statement
        return bless {}, __PACKAGE__;
    in the code in Re^4: concat method inside method call does what bless does by default. So the statement
        return bless {};
    does the same and is 'prettier'! (However, a constructor so defined cannot be used by derived classes, so this is perhaps a bug, or at least a mis-feature.)