On 7/2/07, Gabriel Striewe <[EMAIL PROTECTED]> wrote:
Dear List,
I wanted to interpolate a function reference in a here doc.
The following works fine:
my $hello = sub {
return "hello world!";
};
printf "hello $s\n", &$hello();
But when I use a heredoc instead, it doesn't work:
print <<END;
hello &$hello()
END
At least it does not properly dereference this function
reference.
What do I do wrong?
You will encounter the problem if instead of
printf "hello %s\n", &$hello();
you would have tried:
print "hello &$hello()\n";
That's an issue with the rules of string interpolation. (Read "perldoc
perlop", section on quote-like operators.) Simplistically, I think the
right thing you are expecting for (calling the code ref in $hello) is
not done because the only sigils that matter in interpolation are $
and @. That's why
$ perl -e '$h = sub {1}; print "hello &$h()" '
hello &CODE(0x10240d64)()
(where the only thing interpolated was the stringification of the code
ref in $h).
The weird/scary way to make it work is
$ perl -e '$h = sub {1}; print "hello @{[ &$h() ]}" '
hello 1
Thanks for your advice.
Gabriel
--
http://www.gabriel-striewe.de
--
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
http://learn.perl.org/
--
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
http://learn.perl.org/