in reply to "use strict" sets $!

IIRC $! is only in a defined state after a failed system call, though there might be a few places where perl or some module sets it to zero before a system call. If the last system call didn't fail, $! is not guaranteed to be zero - it may have any value and its value is without defined meaning.

man 3 errno on my CentOS system says:

The <errno.h> header file defines the integer variable errno, which is set by system calls and some library functions in the event of an error to indicate what went wrong. Its value is significant only when the call returned an error (usually -1), and a function that does succeed is allowed to change errno.

Thus one of the possibilities you should consider is that the last system call succeeded but left errno with a non-zero value. Another possibility is that the last system call (or the last N system calls) all succeeded without changing the value of errno and it still has the value set from either the immediately preceding failed system call (which should have set it) or some subsequent successful system call. In other words, it might be quite difficult to determine which system call set it.

Replies are listed 'Best First'.
Re^2: "use strict" sets $!
by rovf (Priest) on Aug 19, 2009 at 11:06 UTC
    If the last system call didn't fail, $! is not guaranteed to be zero
    I know that! That's why I wondered what in "use strict" might have caused a failed system call. I didn't find anything in the code of strict.pm which might have caused errno to be set. Just out of curiosity...

    -- 
    Ronald Fischer <ynnor@mm.st>
      I wondered what in "use strict" might have caused a failed system call

      Maybe you know this also but your quest for a failed system call suggests that maybe you don't or that there might be some confusion...

      The fact that $! is non-zero is not an indication that any system call failed. If you know that the last system call failed then the value of $! has meaning, otherwise its value doesn't mean anything at all.

        otherwise its value doesn't mean anything at all.
        Isn't "at all" too strong here? $! (i.e. errno is initially zero (as far I remember, this is guaranteed by the C standard), and gets a non-zero value if someone sets it - for instance, in a failed system call or if in Perl it is explicitly set by assignment. Seeing it only this way, the value of $! picked up somewhere in the middle of a program execution indeed doesn't mean much.

        But here we have a different situation: We see that in the program

        perl -lwe "print $!"
        $! is zero, which means that when Perl starts executing a (correct) program, $! happens to be zero. We also see that for instance
        perl -lwe "use warnings; print $!"
        print zero, so just the fact that Perl has to compile a pragma, doesn't change the value of $!. But we see that
        perl -lwe "use strict; print $!"
        prints a non-zero value, and from this I conclude that $! must be set somewhere somehow within strict.pm. Being a curious person, I had a look at the source of strict.pm. I didn't find a direct assignment to $! (which is no surprise - we wouldn't expect this anyway), and the system calls I found (and which could possibly set $! to a 'bad file descriptor' error number), are in blocks belonging to error handling (i.e. I would have seen an error message, if such a branch would have taken). That's why I was puzzled and became curious. I admit that it is only for academic interest and has not much practical value aside of satisfying my curiosity ;-)

        -- 
        Ronald Fischer <ynnor@mm.st>