Beefy Boxes and Bandwidth Generously Provided by pair Networks
good chemistry is complicated,
and a little bit messy -LW
 
PerlMonks  

Re: stat : error handling

by johngg (Abbot)
on Jan 03, 2013 at 14:03 UTC ( #1011454=note: print w/ replies, xml ) Need Help??


in reply to stat : error handling

The o/s error is recorded in $!.

$ perl -E ' > @stats = stat q{notThere} or die $!;' No such file or directory at -e line 2. $

Cheers,

JohnGG


Comment on Re: stat : error handling
Select or Download Code
Re^2: stat : error handling
by gepebril69 (Beadle) on Jan 03, 2013 at 14:08 UTC

    Thx Johngg

    I stick to the non-die solutions. They are not real handy in 24/7 operational systems.... ;)

      Well, you don’t need to use die to get information from $!:

      #! perl use Modern::Perl; for my $file ('465_SoPW.pl', 'Non-Existent.txt') { if (my @stats = stat $file) { say "File '$file' last modified: ", $stats[9]; } else { say "stat failed for file '$file': $!"; } }

      Output (on my machine):

      0:17 >perl 466_SoPW.pl File '465_SoPW.pl' last modified: 1357188675 stat failed for file 'Non-Existent.txt': No such file or directory 0:17 >

      Hope that helps,

      Athanasius <°(((><contra mundum Iustus alius egestas vitae, eros Piratica,

      The solution was $! and, as Athanasius points out, die had nothing to do with it other than to illustrate that the variable is available ;-)

      Cheers,

      JohnGG

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://1011454]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others romping around the Monastery: (9)
As of 2015-07-03 09:07 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    The top three priorities of my open tasks are (in descending order of likelihood to be worked on) ...









    Results (51 votes), past polls