in reply to 'open for read' fails as "no such file or directory" but file is there
G'day fasoli,
Hand-crafting your own I/O error messages is tedious and error-prone. In this instance, the die message does not contain sufficient information. Consider using the autodie pragma and let Perl do the work for you.
Equivalent of your code, giving a basic message only:
$ perl -e 'open $fh, "<", "not_a_file" or die $!' No such file or directory at -e line 1.
With autodie you get the same basic message, the I/O operation, and the filename:
$ perl -e 'use autodie; open $fh, "<", "not_a_file"' Can't open 'not_a_file' for reading: 'No such file or directory' at -e + line 1
On an unrelated note, it's good that you've used lexical variables for the filehandles, but it's less good that you've given those variables global scope. Limit the scope of those variables like this:
open my $fh, $mode, $filename;
See open.
— Ken
|
|---|