Performance of Eval

Scott Penrose scottp at myinternet.com.au
Wed Oct 30 17:28:47 CST 2002


Hey Dudes,

A BIG HUGE thank you to all of your responses.

My conclusion is that I have been working in Perl without exceptions 
for so long that I had gotten into the habit of checking everything so 
often that I had ruled out using eval {}.

In my bad old programming days :-) (M$ Visual BASIC) the standard way 
of programming was to do a catch on exceptions for ALL errors. While 
this produces good output to the user, I found it annoying for 
debugging, and encouraged catching ALL errors that way, and in fact it 
was sometimes the only way to catch errors.

Using the Java Style Try, Catch approach, which is basically the same 
as doing the eval {} in the previous examples has given the best 
results not only on shorter code (= less bugs), more readable, and as 
Paul has discovered - FASTER. This in combination with a global DIE 
handler for pretty user output gives I think one of the best possible 
combinations.

I have played around a little with with Error, which is kind of cool, 
allows you to do

            try {
                do_some_stuff();
                die "error!" if $condition;
                throw Error::Simple -text => "Oops!" if $other_condition;
            }
            catch Error::IO with {
                my $E = shift;
                print STDERR "File ", $E->{'-file'}, " had a problem\n";
            }
            except {
                my $E = shift;
                my $general_handler=sub {send_message 
$E->{-description}};
                return {
                    UserException1 => $general_handler,
                    UserException2 => $general_handler
                };
            }
            otherwise {
                print STDERR "Well I don't know what to say\n";
            }
            finally {
                close_the_garage_door_already(); # Should be reliable
            }; # Don't forget the trailing ; or you might be surprised

But for my simple example that is probably overkill, both in look and 
feel and in performance.

But for bigger things, it is quite a neat structure, not totally 
dissimilar to what is being worked on in Perl 6.

Again, big thanks to all the feedback !

Scott
-- 
Scott Penrose
Open source developer
http://linux.dd.com.au/
scottp at dd.com.au

Dismaimer: Open sauce usually ends up never coming out (of the bottle).





More information about the Melbourne-pm mailing list