http://qs1969.pair.com?node_id=86961

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

$ ksh << END_OF_KSH > perl << END_OF_PERL > my $foo = qq{ bar \n }; > print qq{ FOO=$foo }; > END_OF_PERL > END_OF_KSH $ echo $FOO $
The astute Shell hackers among us will see a nested set of heredocs. The more astute Shell hackers will say "but $foo is not escaped, nor is it defined within ksh, so perl sees '' instead of $foo." Well, that's what I thought too.
$ echo "$foo" $ echo "\$foo" $foo $ echo << ECHO $foo ECHO $ echo << ECHO \$foo ECHO $ echo << ECHO \\$foo ECHO \ $
So it would seem that the shell escapes things differently within heredocs. How can I get my $foo's through to perl?

brother dep, shell warrior.

--
Laziness, Impatience, Hubris, and Generosity.

Replies are listed 'Best First'.
Re: Nested Heredocs and Shell Escapes (code)
by bikeNomad (Priest) on Jun 08, 2001 at 20:30 UTC
    Quote the heredoc tags to prevent quoting:
    $ ksh <<'END_OF_KSH'
    > perl <<'END_OF_PERL'
    > my $foo=qq{bar \n};
    > print qq{ FOO=$foo };
    > END_OF_PERL
    > END_OF_KSH
     FOO=bar
     $
    
Re: Nested Heredocs and Shell Escapes (code)
by traveler (Parson) on Jun 08, 2001 at 20:31 UTC
    Umm, the problem is the test. echo doesn't read stdin! Try
    % foo=bar % cat<<EOF >$foo >EOF bar
    If you want to preserve the "$foo" do
    % foo=bar % cat<<'EOF' >$foo >EOF $foo
    --traveler
      Good call. Perl's << is a bit different from the shell's, so you can't blindly translate print to echo in the perl code
      print <<FOO bar FOO
      This should probably be mentioned in perltrap, but it's not.

      Aside from the quoting issue, it's never going to work the way deprecated seems to want, because environment variables don't get passed from child back to parent. If you want to set variables in your current shell, you need to do something like this:

      $ eval `perl -e 'print "FOO=bar"'` $ echo $FOO bar
Re: Nested Heredocs and Shell Escapes (code)
by I0 (Priest) on Jun 08, 2001 at 20:44 UTC
    ksh << END_OF_KSH > perl << END_OF_PERL > my \\\$foo = qq{ bar \n }; > print qq{ FOO=\\\$foo }; > END_OF_PERL > END_OF_KSH