How can I separate Perl variables from text in interpolated strings?

Julien picture Julien · Feb 3, 2009 · Viewed 21.3k times · Source

Consider:

print "    $foo", "AAAAAAAA", $foo, "BBBBBBBB";

Let's say I want to use this code with a print <<EOF;:

print <<EOF;
    $fooAAAAAAAA$fooBBBBBBBB";
EOF

That won't work, because Perl thinks I have a variable called $fooAAAAAAAA. How can I easily use print << with such lines when I have a long test to print?

Answer

Martin Carpenter picture Martin Carpenter · Feb 3, 2009

Use ${foo}:

print <<EOF;
    ${foo}AAAAAAAA${foo}BBBBBBBB";
EOF