in reply to Calling a class method name in a scalar using :: syntax

Hello,

I have a similar problem. I want to call a class method of a "dynamic" class "dynamically", i.e. I want to make a call like

my $result = MODULES::$submodule::$method ($data);

I tried different things like

my $request = "MODULES::$submodule::$method"; my $result = &$request ($data);

or

my $result = eval "$request ($data)";

but I didn't succeed. The even worst thing is that the program keeps on running without any error, only the method trying to call another method seems to "die" without any hint.
Actually this problem is common to my current project. Does anybody have a clue about that?

Thanks.

Marco

Replies are listed 'Best First'.
Re: Re: Calling a class method name in a scalar using :: syntax
by broquaint (Abbot) on Sep 04, 2003 at 14:46 UTC
    If it's a static method then you could do this
    my $result = "MODULES::$submodule"->can($method)->($data);
    This works because can is returning a reference to the subroutine in MODULES::$submodule and then that it is being called with $data being passed in.

    Or, simpler still, if it's a class method then you don't need any trickery

    my $result = "MODULES::$submodule"->$method($data);
    That just uses the string "MODULES::$submodule" as the class and then calls the method as designated by $method.
    HTH

    _________
    broquaint

      Excellent, thank you.

      One of my mistakes was to put the method name between the quotation marks like

      my $result = "MODULES::$submodule->$method($data)"