2

How come the following perl code,

mkdir("foo");
print "foo: $!\n";
mkdir("foo");
print "foo: $!\n";

Will output the following (in a directory without a foo)

perl ./asdf.pl 
foo: 
foo: File exists

But removing foo, the following

$ perl <<EOF 
  mkdir("foo");
  print "foo: $!\n";
  mkdir("foo");
  print "foo: $!\n";
EOF

Swallows the error and outputs,

foo: 
foo: 

1 Answer 1

4

You want to quote the word EOF. From the docs (man bash),

If any part of word is quoted, the delimiter is the result of quote removal on word, and the lines in the here-document are not expanded.

So it'll look like,

$ perl <<'EOF'

EOF

If EOF is unquoted, the $! is interpolated from the shell before being sent to Perl.

$ cat <<EOF 
  mkdir("foo");
  print "foo: $!\n";
  mkdir("foo");
  print "foo: $!\n";
EOF
  mkdir("foo");
  print "foo: \n";
  mkdir("foo");
  print "foo: \n";

You must log in to answer this question.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.