On Mon, Mar 17, 2008 at 4:47 PM, Dermot <[EMAIL PROTECTED]> wrote:
snip
>                 print $fh $status."\n";
>                 print STDERR "$0: $! $?\n";
snip

$? holds the status returned by the last external call (call to
system() function, pipes, qx// operator, call to wait() or waitpid(),
or possibly the gethost*() functions), but you are not making an
external call in your code, so it is inappropriate to be checking its
value.

$! is not gaurenteed to have a valid value unless an error code has
been returned by the last function called, so unless print has an
error $! could be anything (and often is something weird).

from perldoc perlvar
       $?      The status returned by the last pipe close, backtick ('')
               command, successful call to wait() or waitpid(), or from the
               system() operator.  This is just the 16−bit status word
               returned by the wait() system call (or else is made up to look
               like it).  Thus, the exit value of the subprocess is really
               ("$? >> 8"), and "$? & 127" gives which signal, if any, the
               process died from, and "$? & 128" reports whether there was a
               core dump.  (Mnemonic: similar to sh and ksh.)

               Additionally, if the "h_errno" variable is supported in C, its
               value is returned via $? if any "gethost*()" function fails.

               If you have installed a signal handler for "SIGCHLD", the value
               of $? will usually be wrong outside that handler.

               Inside an "END" subroutine $? contains the value that is going
               to be given to "exit()".  You can modify $? in an "END"
               subroutine to change the exit status of your program.  For
               example:

                   END {
                       $? = 1 if $? == 255;  # die would make it 255
                   }

               Under VMS, the pragma "use vmsish 'status'" makes $? reflect
               the actual VMS exit status, instead of the default emulation of
               POSIX status; see "$?" in perlvms for details.
snip
       $!      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(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.

               In the above 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?)

-- 
Chas. Owens
wonkden.net
The most important skill a programmer can have is the ability to read.

-- 
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
http://learn.perl.org/


Reply via email to