Search code examples
perlbackticks

perl: Here documents inside backtick operator


Given a program utility that takes commands from stdin and returns a useful exit code, this perl syntax works:

my $result = `utility -switch1 -switch2 <<HERE
set ridin_round_the_world TRUE;
do this;
sign that;
try { 
   make some_girl; 
   return 0;
} 
except { 
   sleep --week;  
   exit 1;
}
HERE`;
print "result is $result\n";

By "works," I mean the perl script launches the utility, pipes in the command list, waits until the utility exits, and returns the result.

But why does it work? How does backtick know to invoke the program, then pipe in the HERE document? Am I just lucky with my perl implementation, or is this standard behavior?


Solution

  • It's shell's here-doc, not Perl's one. Try running in the shell:

    % cat <<EOF
    some
    thing
    EOF