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 ]

Journal of jjore (6662)

Monday August 14, 2006
05:30 PM

This is not the exception you are looking for.

[ #30629 ]

I just solved a problem by having an exception object which is false whenever looked at but it occurs to me that I might have solved this problem in a sub-optimal way. Oh well. I wanted to abort from a function that was using a callback to process data. Both File::Find and a few functions in B::Utils are like this.

So the following code is a way to solve this but I'm not at all sure it's a good way. Oh well. At least I don't have to rewrite File::Find or something just to get different flow control. Here I've just thrown an object that overloads everything, always returns false, and clears $@.

use File::Find 'find';
use Exception::NoException;
eval {
    find( sub {
        if ( ... ) {
            # do something
            # Just abort the eval. Don't be an error.
            die Exception::NoException->new;
    }, ... );
# Automatically skip Exception::NoException objects
die $@ if $@;
# Ok!

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.
  • Of course, this is really only applicable in limited cases like this one where you know where the exception will be caught. If you let it bubble up past code you control, all sorts of havoc may happen.

    • You bet. I don't think it'd be safe to use with anything that didn't fit completely on the screen. This is really about small scale tweaks to flow control.