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?

In reply to Problem calling a function with name stored into scalar by tj_thompson

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.