Stories
Slash Boxes
Comments
NOTE: use Perl; is on undef hiatus. You can read content, but you can't post it. More info will be forthcoming forthcomingly.

All the Perl that's Practical to Extract and Report

use Perl Log In

Log In

[ Create a new account ]

davebaker (4482)

davebaker
  davebakerNO@SPAMbenefitslink.com
http://benefitslink.com/about.html

I'm married and living in the mountains of western North Carolina with our 14-year-old son. I've loved Perl since 1995, when I started a web site that became my livelihood when I discovered employers would pay to put help wanted ads on it.
+ -

  Comment: Re:critic to the rescue (Score 1) on 2009.02.03 0:36

by davebaker on 2009.02.03 0:36 (#67180)
Attached to: Remember Your Responsibilities

Did you mean to say

if (
    eval {
        my $obj = SomeClass->new();
        die "Code Red!\n";
        1;
    }
) {
    print "No exception occurred\n";
} else {
    print "Exception caught: $@\n";
}

Read More 6 comments
Comments: 6
+ -

  Comment: Updated my original post to show the correct link (Score 1) on 2009.02.01 22:46

Comments: 4
+ -

  Journal: CORRECTED: Learn Pearl While Getting Your Bachelor's Degree on 2009.01.31 21:04

Journal by davebaker on 2009.01.31 21:04
User Journal Read More 4 comments
Comments: 4
+ -

  Comment: eval {} or do_something (Score 1) on 2009.01.31 15:56

by davebaker on 2009.01.31 15:56 (#67147)
Attached to: Remember Your Responsibilities

Following the eval block with an "or" seems to be more reliable than using "if ($@)" to check for an exception in an eval block, because an eval block (or even an eval EXPR) returns undef if there is a syntax error or runtime error or a die statement is executed (according to perldoc -f eval).


        eval {
                my $obj = SomeClass->new();
                die "Code Red!\n";
        } or print "Exception caught: $@\n";

But even with this technique the $@ is reset by the destructor in your example, so while the exception at least can be caught we lose the information $@ would provide but for the destructor. The output is merely

"Exception caught:"

The documentation for eval says that $@ is guaranteed to be a null string if no exception occurred; I had not appreciated the precise scope of that statement until seeing your example (it doesn't go on to say that, conversely, $@ is guaranteed not to be a null string if an exception occurred).

I believe I heard schwern advocating at YAPC 2008 the use of

eval { ... }
        or warn_about_problem();

rather than

eval { ... };
if ($@) { warn_about_problem() }

This might have been the kind of situation he had in mind.

Read More 6 comments
Comments: 6
+ -

  Comment: Re:I like the other argument better (Score 1) on 2009.01.28 23:28

by davebaker on 2009.01.28 23:28 (#67094)
Attached to: Dead Babies
In the abstract I like the idea that one shouldn't force a choice on another. But as applied to abortion it is curious that the parent gets a choice, but the child in the womb doesn't get a choice when her mother decides to kill her.
Read More 21 comments
Comments: 21