Re: Handling errors when working with files



Thanks Tom, it helped. :-)

I learned some more few things. ;-)

Regards,

Tom Phoenix <tom@xxxxxxxxxxxxxx> wrote:
On Dec 30, 2007 10:17 AM, oscar gil 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

--
To unsubscribe, e-mail: beginners-unsubscribe@xxxxxxxx
For additional commands, e-mail: beginners-help@xxxxxxxx
http://learn.perl.org/





---------------------------------
Never miss a thing. Make Yahoo your homepage.

Relevant Pages

  • Re: backticks
    ... Tom Phoenix wrote: ... Why not simply use Perl's unlink function? ... For this, I suggest checking out the Perl Cookbook, from O'Reilly ... application is to extract signal and noise history of a microwave radio and plot a time series ...
    (perl.beginners)
  • Re: threading a needle on TEST
    ... Tom Phoenix wrote: ... Stonehenge Perl Training ... I guess what I'm not clear on is I'm not trying to install this module. ... But several of the test scripts just don't run or fail on themselves. ...
    (perl.beginners)
  • Re: compiling DateTime module on SCO OSR5
    ... Tom Phoenix said: ... I recommend that you re-do the installation from the ... > build perl on the same machine that's running it, ...
    (perl.beginners)
  • Re: Polluting the Global Namespace
    ... On 7/20/06, Tom Phoenix wrote: ... You can always make a normal module, 'use' it in the normal way, and ... another package name, thus defining everything in package 'main' by ... using pure perl code or would I have to delve into xs? ...
    (perl.beginners)
  • Re: Add directories to @INC
    ... Tom Phoenix wrote: ... > Since we discuss about common perl scripts not mod_perl,so your way seems not useful here. ... script or module sense? ...
    (perl.beginners)