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

I have yet another simple and likely easy to answer question for you smart folks :) I have this debug section of code:
print STDERR "FUNCTION IS ($func)\n"; print STDERR "(\&is_int) TRUE!\n" if &is_int(42, 1); print STDERR "(is_int) TRUE!\n" if is_int(42, 1); print STDERR "(Module::Configurable::is_int) TRUE!\n" if Module::Configurable::is_int(42, 1); no strict 'refs'; print STDERR "(\&\$func) TRUE!\n" # line 137 from output +below if &$func(42, 1); # line that's dieing
My output from my test script looks like this:
FUNCTION IS (is_int) (&is_int) TRUE! (is_int) TRUE! (Module::Configurable::is_int) TRUE! not ok 3 - Configure is successful with valid data # Failed test 'Configure is successful with valid data' # at t/Module-Configurable.t line 49. # died: Undefined subroutine &Module::Configurable::is_int called at / +nfs/pdx/disks/nehalem.pde.077/projects/libraries/Module/blib/lib/Modu +le/Configurable.pm line 137.
As you can see from the debug output, the first three versions of my attempt to use the is_int function are working fine. The function is_int is imported into my namespace (evident since is_int works by itself) and can be called successfully from the longer Module::Configurable::is_int form as well. However, when I try to access it with the name stored into the $func scalar, it doesn't seem to know what it is any more.

The function name seems correct, and it's checking the correct package namespace for the function. I've been trying many variations to pinpoint what's going on here, but I'm not having much luck and would appreciate an experienced eye :) I can make it work, but I'd like to know what I'm doing wrong as opposed to working around something I don't understand.

Update:

print STDERR "(\&{\$func}) TRUE!\n" if &{'Params::SimpleValidate::'.$func}(42, 1);
This works by specifying the full module name of the function in use...but why does it not work the other way?

Replies are listed 'Best First'.
Re: Problem calling a function with name stored into scalar
by BrowserUk (Patriarch) on Jun 10, 2011 at 22:50 UTC

    Try:

    sub fred{ print 'this is fred:' . "@_" ; };; $f = 'fred';; &{ $f }( 1,2,3 );; this is fred:1 2 3

    Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
    "Science is about questioning the status quo. Questioning authority".
    In the absence of evidence, opinion is indistinguishable from prejudice.
      That seems to be what I'm doing. I've tried adding the braces around the $func var like so:
      print STDERR "(\&\$func) TRUE!\n" if &{$func}(42, 1);
      But it doesn't seem to matter. Same error. This is from a larger piece of code, obviously, so perhaps something strange is going on behind the scenes.
Re: Problem calling a function with name stored into scalar
by ikegami (Patriarch) on Jun 10, 2011 at 23:00 UTC

    Did you read the error message?

    Undefined subroutine &Module::Configurable::is_int

    so

    Module::Configurable::is_int

    doesn't exist. It's not a problem with the call itself.

    Update:

    Since you call Module::Configurable::is_int on the previous line, something is fishy. Maybe some kind of namespace cleaning?

      Sure...but notice that right above it I'm able to use that function name:
      print STDERR "(Module::Configurable::is_int) TRUE!\n" if Module::Configurable::is_int(42, 1);
      Output:
      (Module::Configurable::is_int) TRUE!
        Yeah, see my update. &Module::Configurable::is_int existed at compile-time, but not at run-time.