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

I have some code where I use callbacks to run the appropriate sub. The code works.
sub run_parser { my $parser = shift; &{$parser}; }
However, I need to be able to pass parameters (scalar, array, hash, whatever). What is the correct syntax for passing a parameter (lets say a scalar $url)? Thanks,

Replies are listed 'Best First'.
Re: Pass a parameter to a callback function
by ikegami (Patriarch) on Oct 15, 2009 at 01:11 UTC
    You shouldn't use &func; (or &$func_rec;) unless you intend to. It doesn't pass "no parameters", it passes the @_ unlocalised, unchanged.
    &$func_rec() # No params &$func_rec(param, ...) # With params $func_rec->() # Same as above, $func_rec->(param, ...) # but easier to read
Re: Pass a parameter to a callback function
by BruceDB (Novice) on Oct 15, 2009 at 01:04 UTC
    I figured it out. I was sending the parameter using:
    &{parser}($url);
    The correct way should have the parameter in quotes:
    &{parser}("$url");
      The quotes creates a copy of the variable after forcing it into a string. You shouldn't ever have to do that. Something is very funky in your parser.