Re: Handling errors when working with files



On Dec 30, 2007 10:17 AM, oscar gil <racsolig@xxxxxxxxx> wrote:

What I still don't understand is why $! and $^E are set as there was an
error though there was not :-?.

Those variables may be set when your perl binary internally needs to
use a system call or something similar. During or shortly after your
program uses open(), for example, perl has to make some several
complex system calls, some of which may fail without causing problems
at the Perl-programming level. The chief use of those variables is
just after certain operations have failed; after a success they may
have some irrelevant failure code left over from an unrelated
operation.

Tom, How would you handle an error after an acction done on files,
directories and so on?.

I'm not sure what you're asking for here. In most cases in Perl code,
the thing to do is to write something like "or die" with a good
diagnostic string wherever a key step might fail. It's common to need
to do some kind of cleanup after a failure; in Perl, that's often done
with an eval block, although an END block can also be useful,
depending upon the type of cleanup. But for some cases, it's easiest
to just use Perl's normal control structures:

foreach my $file (@lots_of_file_names) {
if (unlink $file) {
print "You'll never see '$file' again!\n";
} else {
print "Couldn't unlink '$file': $!; continuing...";
}
}

Hope this helps!

--Tom Phoenix
Stonehenge Perl Training
.



Relevant Pages

  • Re: Using Subroutines with CGI::Session::MySQL?
    ... On 08/16/2007 07:26 PM, L. D. James wrote: ... exiting the application from a subroutine without doing cleanup. ... This wasn't a Perl leak--it was an effect of the additional complexity brought by mod_perl. ...
    (comp.lang.perl.misc)
  • Re: Handling errors when working with files
    ... Tom Phoenix wrote: ... Those variables may be set when your perl binary internally needs to ... with an eval block, although an END block can also be useful, ... depending upon the type of cleanup. ...
    (perl.beginners)
  • Re: split inconsistency- why?
    ... I think most of the cleanup was pre-5.6. ... I think the problematic areas are split on a REx which can match an ... empty substring, and split of an empty string. ... work on Perl. ...
    (comp.lang.perl.misc)
  • Re: question about installation of GD::Text on cygwin (could be a Makefile q)
    ... > That is odd. ... What I did do is a print "$@\n"; after each test in the eval block doing ... $ perl Makefile.PL ... Could not find/open font ...
    (comp.lang.perl.misc)
  • Re: Eval scoping question
    ... Illegal division by zero at -e line 6. ... The size of the eval block has nothing to do with it. ... There is just no other sound way to handle exceptions in Perl. ... That's very good info and robust exception handling is always ...
    (perl.beginners)