Anonymous Monk has asked for the wisdom of the Perl Monks concerning the following question:

Hello Monks, This is a silly question but I need help understanding a construct in perl. While looking at sample code that checks $! for a specific errno, I came across this syntax: if ($!{EINTR}) { } This is equivalent to writing: if ($! == EINTR) { } My question is I can't find any reference in the perl docs that explains why this works. $! seems to be special somehow. Can anyone point me to where this is documented? Thanks.

Replies are listed 'Best First'.
Re: Perl syntax question
by blue_cowdawg (Monsignor) on Feb 18, 2013 at 21:07 UTC

    Check out perldoc perlvar and you'll get some explanations about Perl's "special variables." Here is a quick sniglet from that doco:

            The variables $@, $!, $^E, and $? contain information about different
           types of error conditions that may appear during execution of a Perl
           program. The variables are shown ordered by the "distance" between the
           subsystem which reported the error and the Perl process. They
           correspond to errors detected by the Perl interpreter, C library,
           operating system, or an external program, respectively.
    

    Digging deeper into your question, here is what the doco says about $!:

           $!      If used numerically, yields the current value of the C "errno"
                   variable, or in other words, if a system or library call fails,
                   it sets this variable. This means that the value of $! is
                   meaningful only immediately after a failure:
    
                           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,
                       # here $! is meaningless.
    
                   The meaningless stands for anything: zero, non-zero, "undef". A
                   successful system or library call does not set the variable to
                   zero.
    
                   If used as a string, yields the corresponding system error
                   string. You can assign a number to $! to set errno if, for
                   instance, you want "$!" to return the string for error n, or
                   you want to set the exit value for the "die()" operator.
    
                   Mnemonic: What just went bang?
    
    
    and what it says about %!
           %!      Each element of "%!" has a true value only if $! is set to that
                   value. For example, $!{ENOENT} is true if and only if the
                   current value of $! is "ENOENT"; that is, if the most recent
                   error was "No such file or directory" (or its moral equivalent:
                   not all operating systems give that exact error, and certainly
                   not all languages). To check if a particular key is meaningful
                   on your system, use "exists $!{the_key}"; for a list of legal
                   keys, use "keys %!". See Errno for more information, and also
                   see "$!".
    
                   This variable was added in Perl 5.005.
    
    which is what the syntax your cite is working with.


    Peter L. Berghold -- Unix Professional
    Peter -at- Berghold -dot- Net; AOL IM redcowdawg Yahoo IM: blue_cowdawg
Re: Perl syntax question
by LanX (Saint) on Feb 18, 2013 at 21:05 UTC
    it's a hash-element of %!

    see perlvar

    %! Each element of "%!" has a true value only if $! is set + to that value. For example, $!{ENOENT} is true if and only if +the current value of $! is "ENOENT"; that is, if the most r +ecent error was "No such file or directory" (or its moral equ +ivalent: not all operating systems give that exact error, and ce +rtainly not all languages). To check if a particular key is me +aningful on your system, use "exists $!{the_key}"; for a list of + legal keys, use "keys %!". See Errno for more information, a +nd also see above for the validity of $!.

    next time please use <code>-tags!

    Cheers Rolf

Re: Perl syntax question
by Anonymous Monk on Feb 18, 2013 at 21:25 UTC
    You guys are good!

    I must be daft, but I didn't see the %! hash in the perlvar docs. Duh!

    Thanks.