When referenced,$!
retrieves the current value of the Cerrno
integer variable. If$!
is assigned a numerical value, that value is stored inerrno
. When referenced as a string,$!
yields the system error string corresponding toerrno
.
Many system or library calls seterrno
if they fail, to indicate the cause of failure. They usually donot seterrno
to zero if they succeed. This meanserrno
, hence$!
, is meaningful onlyimmediately after afailure:
if (open my $fh, "<", $filename) {# Here $! is meaningless.... } else {# ONLY here is $! meaningful....# Already here $! might be meaningless. } # Since here we might have either success or failure, # $! is meaningless.
Here,meaningless means that$!
may be unrelated to the outcome of theopen()
operator. Assignment to$!
is similarly ephemeral. It can be used immediately before invoking thedie()
operator, to set the exit value, or to inspect the system error string corresponding to errorn, or to restore$!
to a meaningful state.
Mnemonic: What just went bang?
Perldoc Browser is maintained by Dan Book (DBOOK). Please contact him via theGitHub issue tracker oremail regarding any issues with the site itself, search, or rendering of documentation.
The Perl documentation is maintained by the Perl 5 Porters in the development of Perl. Please contact them via thePerl issue tracker, themailing list, orIRC to report any issues with the contents or format of the documentation.