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!
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.
Re:A dangerous crutch
jjore on 2006-08-22T02:26:21
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.