AndrewMcHorney wrote:
Hello

Hello,

Is there a way in Perl to find out why a file failed to open? I am working on a script that is opening and reading a lot of files. This script is right now being written on a Windows based system.

perldoc perlvar

[ *snip* ]

    $OS_ERROR
    $ERRNO
    $!      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?)

            Also see "Error Indicators".

    %!      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 above for the validity of $!.

    $EXTENDED_OS_ERROR
    $^E     Error information specific to the current operating system.
            At the moment, this differs from $! under only VMS, OS/2,
            and Win32 (and for MacPerl).  On all other platforms, $^E is
            always just the same as $!.

            Under VMS, $^E provides the VMS status value from the last
            system error.  This is more specific information about the
            last system error than that provided by $!.  This is
            particularly important when $! is set to EVMSERR.

            Under OS/2, $^E is set to the error code of the last call to
            OS/2 API either via CRT, or directly from perl.

            Under Win32, $^E always returns the last error information
            reported by the Win32 call "GetLastError()" which describes
            the last error from within the Win32 API.  Most
            Win32-specific code will report errors via $^E.  ANSI C and
            Unix-like calls set "errno" and so most portable Perl code
            will report errors via $!.

            Caveats mentioned in the description of $! generally apply
            to $^E, also.  (Mnemonic: Extra error explanation.)

            Also see "Error Indicators".




John
--
Perl isn't a toolbox, but a small machine shop where you
can special-order certain sorts of tools at low cost and
in short order.                            -- Larry Wall

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


Reply via email to