Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl Monk, Perl Meditation
 
PerlMonks  

Re^2: use Fatal;

by Aristotle (Chancellor)
on Jan 10, 2002 at 21:05 UTC ( [id://137762]=note: print w/replies, xml ) Need Help??


in reply to Re: use Fatal;
in thread use Fatal;

Excuse my potential ignorance, but I'm wondering - for cases when you want explicit checking, couldn't you trap failures using eval {}? Then this:
open(FILE1, "<file1"); open(FILE2, "<file2") or die "Can't open file2: $!\n";
is the same as this:
use Fatal qw(open); eval { open(FILE1, "<file1"); } open(FILE2, "<file2");
no?

Replies are listed 'Best First'.
Re: Re: Re: use Fatal;
by Masem (Monsignor) on Jan 10, 2002 at 21:31 UTC
    That is a very ugly use of eval, IMHO.

    I've nothing wrong with the eval function, but I'm of the opinion that eval should be used for code that might be generated on the fly (most likely from user input), thus implying the dynamic nature of the perl language. Using eval to evaluate code that is already hard written into the code, on the other hand, seems to be a way to get around compiler/strict or other issues. Certainly there are cases where using eval on hard-coded code is necessary to achieve certain results (for example, if you have a function from a module that would die on failure, but you want to catch this), but otherwise, it reminds me of when I saw C or C++ code that was wrapped in pragmas in order to disable certain compiler features to get their badly written code working properly.

    IMO, and I think it will be easier in perl 6, I'd much rather move to an OO-based Exception model as Java has, as it forces you to deal with errors, instead of allowing them to slip. This way, you can deal with errors that might be generated from one part of the code differently than errors from other parts. It does require you to think about these errors from the start, but it ends up improving your overall error-catching of the final program.

    -----------------------------------------------------
    Dr. Michael K. Neylon - mneylon-pm@masemware.com || "You've left the lens cap of your mind on again, Pinky" - The Brain
    "I can see my house from here!"
    It's not what you know, but knowing how to find it if you don't know that's important

      eval BLOCK and eval STRING have as much in common as select FILEHANDLE has with the four-argument version, in my opinion. Since the block contents are known at compile time, they're compiled. Since string contents can't be known at compile time, they must be compiled at run time.

      I do agree that using eval to catch death from the Fatal module is particularly ugly.

      Ah. Well personally, I've always thought eval BLOCK should not exist, and that this functionality should be provided by a try BLOCK function. :-)

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://137762]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others avoiding work at the Monastery: (5)
As of 2024-04-19 22:50 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found