oscar gil wrote:
Hello everybody,

Hello,

I wanted to learn a bit more about how to handle errors when I am
working with files,

Do you want to handle errors or do you want to report errors?

so I wrote this simple script to start with it
where you can see that first I show the normal perl errors variables,
then I try to delete a file and finally I show the errors variables
again.
The file I try to delete exists and it is at the same directory and
after the script the file is deleted, so it works.
What surprised me is that at the end of the script the '$!' is 'No such
file or directory' and '$^E' is 'The system cannot find the file
specified' though the file was deleted correctly, :-?.
My idea was to treat the '$!' when I am working with files but with
this results I don't see how.

Have you read the documentation for the variables you are using?

perldoc perlvar

       $CHILD_ERROR
       $?      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.)

       $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:

       $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 $!.
[ SNIP ]
               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.)

       $EVAL_ERROR
       $@      The Perl syntax error message from the last eval()
               operator.  If $@ is the null string, the last eval()
               parsed and executed correctly (although the operations
               you invoked may have failed in the normal fashion).
               (Mnemonic: Where was the syntax error "at"?)

Let me tell you I am using this version of perl with Windows and I
tried it both, Win2k and WinXP:
This is perl, v5.8.7 built for MSWin32-x86-multi-thread
(with 14 registered patches, see perl -V for more detail)
Here it is the script and afterwards there is the STDOUT exit. ----------------------------------------------------
#!/usr/bin/perl -w
  use strict;
print "\n\n\nBEFORE:\n-----\n";
print '$@'." is ->$@<-\n";
print '$!'." is ->$!<-\n";
print '$^E'." is ->$^E<-\n";
print '$?'." is ->$?<-\n\n\n";
# I tried with open and with system()
  #open (RD , "del pepe.txt |") or die "I cannot RD\n";

Only the $! or $^E variables apply after open() *FAILS*. If open() completed successfully then the values of $! and $^E are meaningless.


  system ("del pepe.txt");

Only the $? variable applies to the system() function. I think that 'del' is an internal command.com function (I haven't used DOS/Windows in a long time) so $? will probably report the return status of command.com?


[ SNIP ]

Questions:

1.- Why a have the '$!' and '$^E' set as it could not find the file
when in reality it could?

Because the values of $! and $^E are only valid after an error occured.

  I tried with 'system()' and with 'open' with the same results. :-(


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