Next | Tricks of the Wizards | 199 |
Perl magic $! variable reflects the operating system error status
Example of use:
unless (open FH, $filename) { if ($! == EACCES) { # Permission denied... } elsif ($! == ENOENT) { # No such file... } elsif ($! == ENOTDIR) { # Some part of the path is not a directory... } elsif ... } }
This doesn't work---where did EACCESS etc. come from?
Solution 1: Import lots and lots of compile-time constants. (Blecch.)
Solution 2: Use %! instead: (5.005 and later.)
unless (open FH, $filename) { if ($!{EACCES}) { # Permission denied... } elsif ($!{ENOENT}) { # No such file... } elsif ($!{ENOTDIR}) { # Some part of the path is not a directory... } elsif ... } }
When Perl saw you use %!, it loaded the Errno module and tied %! into it.
FETCH method checks the value of $!.
Next | Copyright © 2003 M. J. Dominus |