jcee14
jcee14

Reputation: 928

Perl writing to file stream causing unexpected SIGPIPE error

I have the following perl code which tries to write a string to a newly created file:

open(OUT, ">$file") or die "file out error!\n";
print OUT $string;

Normally, this code works fine. If we do not have write permissions to the directory where $file exists, the program fails, which is expected. However, instead of printing "file out error!" as the error message, the program simply exits with exit code 13 (Permission denied).

  1. Why does the open method succeed when we are unable to write to the file?
  2. How do we get the appropriate error message in this instance?

Upvotes: 2

Views: 1172

Answers (1)

JRFerguson
JRFerguson

Reputation: 7516

I think you are confusing the programs exit code and its reporting of a standard system system error code. Error code (errno) 13 equates to "permission denied".

perl -lE '$!=13;say $!'
Permission denied

perl -lE '$!=32;say $!'
Broken pipe

Of course the actual message may vary slightly depending on your OS.

And for that matter, a better (IMO) way to construct an error message for the open is something like: `open OUT,">","$file" or die "Can't open $file: $!\n".

Upvotes: 3

Related Questions