On Jul 2, 2007, at 2:46 PM, Gabriel Striewe 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();

In Perl printf is rarely used because double-quote strings allow interpolation of scalars and arrays, and because print accepts an arbitrary number of arguments.

Interpolation does not understand function calls, though, so you either use a multi-argument call like this:

  print "hello ", $hello->(), "\n";

or either use this hack:

  print "hello @{[ $hello->() ]}\n";

Here-documents have double-quote semantics unless you put single quotes around the terminating string, so that last trick works in here-documents as well.

-- fxn


--
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
http://learn.perl.org/


Reply via email to