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: 
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";

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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