20

Is possible somewhat write a perl script in a bash script as heredoc?

This is not working (example only)

#/bin/bash
perl <<EOF
while(<>) {
    chomp;
    print "xxx: $_\n";
}
EOF

Is here some nice way how to embed a perl script into a bash script? Want run perl script from an bash script and don't want put it into external file.

2
  • Don't you need some command line switches after the name of the executable? Such as perl -we?
    – TLP
    Jul 19, 2013 at 16:39
  • 1
    @TLP No, OP is just passing the script as standard input.
    – mob
    Jul 19, 2013 at 16:42

4 Answers 4

27

The problem here is that the script is being passed to perl on stdin, so trying to process stdin from the script doesn't work.

1. String literal

perl -e '
while(<>) {
    chomp;
    print "xxx: $_\n";
}
'

Using a string literal is the most direct way to write this, though it's not ideal if the Perl script contains single quotes itself.

2. Use perl -e

#/bin/bash

script=$(cat <<'EOF'
while(<>) {
    chomp;
    print "xxx: $_\n";
}
EOF
)
perl -e "$script"

If you pass the script to perl using perl -e then you won't have the stdin problem and you can use any characters you like in the script. It's a bit roundabout to do this, though. Heredocs yield input on stdin and we need strings. What to do? Oh, I know! This calls for $(cat <<HEREDOC).

Make sure to use <<'EOF' rather than just <<EOF to keep bash from doing variable interpolation inside the heredoc.

You could also write this without the $script variable, although it's getting awfully hairy now!

perl -e "$(cat <<'EOF'
while(<>) {
    chomp;
    print "xxx: $_\n";
}
EOF
)"

3. Process substitution

perl <(cat <<'EOF'
while(<>) {
    chomp;
    print "xxx: $_\n";
}
EOF
)

Along the lines of #2, you can use a bash feature called process substitution which lets you write <(cmd) in place of a file name. If you use this you don't need the -e since you're now passing perl a file name rather than a string.

4
  • Second problem: $_ is interpolated by the shell, probably to an empty string. Escape it ... print "xxx: \$_\n";
    – mob
    Jul 19, 2013 at 16:40
  • It can only be used once per file, but perl's -x flag can be used with less than ugly results.
    – tjd
    Jul 19, 2013 at 18:53
  • @john-kugelman is it possible to use Bash's variables inside Perl code via heredoc?
    – user3905644
    Oct 20, 2016 at 4:42
  • @sdkks Inside perl script, you can use exported bash variables $ENV{variable}, e.g. set | perl -lne 'print if m/$ENV{HOME}/;'
    – mosh
    Dec 7, 2017 at 4:28
4

You know I never thought of this.

The answer is "YES!" it does work. As others have mentioned, <STDIN> can't be used, but this worked fine:

$ perl <<'EOF'
print "This is a test\n";
for $i ( (1..3) ) {
print "The count is $i\n";
}
print "End of my program\n";
EOF
This is a test
The count is 1
The count is 2
The count is 3
End of my program

In Kornshell and in BASH, if you surround your end of here document string with single quotes, the here document isn't interpolated by the shell.

2
  • @JohnKugelman I mentioned that you can't use STDIN in my post. The question was "Is possible somewhat write a perl script in a bash script as heredoc". Not whether you can read from STDIN or not. The answer is yes, it does work.
    – David W.
    Jul 19, 2013 at 16:54
  • You can use STDIN if you define a specific FD and use it's linked device file: echo 'foo' | perl /dev/fd/3 3<<-'EOPERL' Feb 28 at 20:34
2

Only small corection of @John Kugelman's answer. You can eliminate the useless cat and use:

read -r -d '' perlscript <<'EOF'
while(<>) {
    chomp;
    print "xxx: $_\n";
}
EOF

perl -e "$perlscript"
0

Here's another way to use a PERL HEREDOC script within bash, and take full advantage it.

    #!/bin/sh
    #If you are not passing bash var's and single quote the HEREDOC tag
    perl -le "$(cat <<'MYPL'
    # Best to build your out vars rather than writing directly
    # to the pipe until the end.
    my $STDERRdata="", $STDOUTdata="";
    while ($i=<STDIN>){ chomp $i;
        $STDOUTdata .= "To stdout\n";
        $STDERRdata .= "Write from within the heredoc\n";
    MYPL
    print $STDOUTdata; #Doing the pipe write at the end
    warn $STDERRdata;  #will save you a lot of frustration.
    )" [optional args] <myInputFile 1>prints.txt 2>warns.txt

or

    #!/bin/sh
    set WRITEWHAT="bash vars"
    #If you want to include your bash var's
    #Escape the $'s that are not bash vars, and double quote the HEREDOC tag
    perl -le "$(cat <<"MYPL"
    my $STDERRdata="", $STDOUTdata="";
    while (\$i=<STDIN>){ chomp \$i;
        \$STDOUTdata .= "To stdout\n";
        \$STDERRdata .= "Write $WRITEWHAT from within the heredoc\n";
    MYPL
    print \$STDOUTdata; #Doing the pipe write at the end
    warn \$STDERRdata;  #will save you a lot of frustration.
    )" [optional args] <myInputFile 1>prints.txt 2>warns.txt

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.