in reply to Re^2: 'open for read' fails as "no such file or directory" but file is there
in thread 'open for read' fails as "no such file or directory" but file is there

Here's a suggestion for future development -- instead of creating the filename to be opened just in the open statement, create it ahead of time:

my $filename = # complicated work that builds filename open ( my $fh, '<', $filename ) or die "Unable to open $filename for read: $!"; # ... close ( $fh );
You may hear about using autodie instead, but I like writing out the open statement and the die statement, as it gives the debugger me more information from the developer me. In this case, if it fails, you can immediately copy and paste the filename into an ls command and check to see if Perl can't find the file .. or if developer me is an idiot. :)

Alex / talexb / Toronto

Thanks PJ. We owe you so much. Groklaw -- RIP -- 2003 to 2013.

Replies are listed 'Best First'.
Re^4: 'open for read' fails as "no such file or directory" but file is there
by kcott (Archbishop) on Nov 02, 2016 at 20:40 UTC

    G'day Alex,

    Hand-crafting your own die messages can provide a lot more information than that provided by autodie. In some cases, using die instead of autodie is entirely appropriate. However,

    ... or die "Unable to open $filename for read: $!";

    provides no more information than

    $ 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

    See my reply to the OP for more on this.

    — Ken

      Absolutely right. My current preference to hand-rolled exceptions is based on my current environment, one that I'm reluctant to modify (it's git-bash running on an Azure cloud). However, I did just check, and autodie is present in this installation (5.22.0), so I will try it the next time I build a script.

      Mostly, what also contributes to me attitude about error-checking is my background as a C programmer -- my experience has taught me to be pessimistic, and check for errors just about everywhere. :)

      Alex / talexb / Toronto

      Thanks PJ. We owe you so much. Groklaw -- RIP -- 2003 to 2013.