Slash Boxes
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 ]

chromatic (983)

  (email not shown publicly)

Blog Information [] Profile for chr0matic []

Journal of chromatic (983)

Sunday November 06, 2005
05:20 PM

Resumable Exceptions (sort of)

[ #27491 ]

Sometimes it's nice to have a single exception block guarding several potentially fatal operations. This lets you re-use error-handling code. However, sometimes you can recover from a single exception and move on to the next operation. Unfortunately, Perl doesn't really support this.

That's why I wrote some very simple code. Here's how it looks like from the Perl side:


use strict;
use warnings;

use Runops::Resume;

my $text;
    $text = 'before';
    print $text, "\n";
    $text = 'after';
    die "Goodbye!\n";
    print $text, "\n";

warn "Died '$@'" if $@;

As you ought to expect, the output is:

Died 'Goodbye!
' at line 17.

Note that declaring lexicals within the eval block doesn't quite work correctly. That would be scary code.

Between Perl and C, it's fewer than 70 lines of well-spaced code. It's reasonably trivial to make a stack of resumable exceptions, too -- probably fewer than ten more lines of C. Runops::Resume.

The Fine Print: The following comments are owned by whoever posted them. We are not responsible for them in any way.
More | Login | Reply
Loading... please wait.
  • You're getting very close to wanting mess with the stacks and serialize/marshal them. If the die wasn't within the current frame then I imagine stuff would be all fu-bar...

    except that I also see you're doing another goto() to immediately following the resume() on falling out of the eval{}. Hmm. If the eval does some kind of further exceptional flow control with die/next/goto perhaps that's a problem.

    Your additional runloop has me wishing these modules were implemented as advice to another runloop. Consider