>>>>> "aa" == aa aa <[EMAIL PROTECTED]> writes:

aa> Hi,
 
aa> I try to open several files, if one of them failed, my program will die and 
then send the died information to a file. 
 
aa> eg. 
aa> open(AA, "a.txt") or die "can't open file a.txt\n";
 
aa> But I want to this string "can't open file a.txt\n" print to a file. 
 
aa> Is anyone can help me?

Run your code inside an eval block, which will trap any "die" messages.

    eval {
         ...
         code here that might fail
         ...
         ...
         open AA, "a.txt" or die "cannot open a.txt: $!";
         ...
         ...
         more code that might fail
         ...
         ...
    };
    if ($@) { # there was an error
       open LOGFILE, ">>logfile" or die "now we're really in trouble :)";
       print LOGFILE localtime . ":$@";
       close LOGFILE;
       die $@; # rethrow the error for the error log as well
    }

For further information on eval, see "perldoc -f eval".

-- 
Randal L. Schwartz - Stonehenge Consulting Services, Inc. - +1 503 777 0095
<[EMAIL PROTECTED]> <URL:http://www.stonehenge.com/merlyn/>
Smalltalk/Perl/Unix consulting, Technical writing, Comedy, etc. etc.
See http://methodsandmessages.vox.com/ for Smalltalk and Seaside discussion

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


Reply via email to