Beefy Boxes and Bandwidth Generously Provided by pair Networks
go ahead... be a heretic
 
PerlMonks  

In time for Wimbledon

by dash2 (Hermit)
on Jun 30, 2003 at 16:59 UTC ( #270223=perlmeditation: print w/ replies, xml ) Need Help??

I wonder if this would be useful? Nothing Perl-specific, but maybe to think about as Perl 6 approaches.

# pseudocode try { serve(); } bounce(Error::Return) { $rally++; } catch (Error::OutOfBounds){ new_point(); } sub serve { throw Error::OutOfBounds if int rand 10 <=1; lob Error::Return foreach (0..2); return "Advantage Caller"; }

The idea is that sometimes you definitely want to throw an error - there's no way to continue. On the other hand, you might just want to lob an error back to the caller, and let them decide if they want to go on. So then the caller could either catch the exception, or bounce it back - in which case the callee would continue executing as if nothing had happened.

Useful? Well, suppose you had a function to parse HTML. Your function hits some dodgy syntax. Should it give up or press on? Depends whether you're in strict mode or not.

Or... you read files in a directory tree, and then one of the needed files is missing. Is this a disaster, or do you just want to keep gathering up the information, but note that you missed a file?

# pseudocode sub dir_reader { # ... while (my ($curdir, $name) = next_in_tree()) { -e "$curdir/$name" or lob "Couldn't open file $name"; push @files, $name; } # ... return @files; } sub keep_on { try { dir_reader(@_) } bounce { /.*(\S+)$/; push @badfiles, $1;} } sub give_up { try { dir_reader(@_) } catch{ die "Couldn't open file $_"} }

Just a thought. It's a way to make communication between caller and callee a bit more flexible - even streamlike. I suspect someone is now going to point out that this violates fundamental design principles, or can be done easily using XOR and a box of matchsticks, but I thought it might be an interesting concept.
A massive flamewar beneath your chosen depth has not been shown here

Comment on In time for Wimbledon
Select or Download Code
Re: In time for Wimbledon
by PodMaster (Abbot) on Jul 01, 2003 at 01:47 UTC
    Consider this
    FOOBAR: try { serve(); } catch (Error::Return){ goto FOOBAR; # redo FOOBAR? whatever } catch (Error::OutOfBounds){ new_point(); }
    I don't see a need for new syntaxt, long live the goto ;)

    MJD says "you can't just make shit up and expect the computer to know what you mean, retardo!"
    I run a Win32 PPM repository for perl 5.6.x and 5.8.x -- I take requests (README).
    ** The third rule of perl club is a statement of fact: pod is sexy.

      Ah, but I mean go back to exactly where you were.

      So for example

      for (0..20) { lob Argh if some_error_condition(); }
      could be bounced back to the same position in the loop.

      I have no idea how this could be implemented. I just think it would be kewl.
      A massive flamewar beneath your chosen depth has not been shown here

Re: In time for Wimbledon
by yosefm (Friar) on Jul 01, 2003 at 19:32 UTC
    The whole point of raising an exception is saying "I can't deal with it, dude", so if you bounce back, well, it still can't handle it. What if a subroutine is designed to throw an exception when a DB connection cannot be established, and then the stupid user bounces the exception back? you'll get a worse condition.

    If you just want to change policy of what is fatal and what isn't, just pass an argument saying so.

    or, alternatively, use ACME::comefrom to put all your checking in one place... :-)

      Yes, this is why I distinguished between lob and throw, and said:

      The idea is that sometimes you definitely want to throw an error - there's no way to continue. On the other hand, you might just want to lob an error back to the caller, and let them decide if they want to go on.

      In other words, if the subroutine throws an exception, you can't bounce it back. But if it lobs one, you can. Obviously, letting user code ignore serious errors would be foolish.

      You could do it by argument-passing, just as you can do exception throwing by returning undef and letting the caller decide whether to die. But that doesn't mean exception-throwing isn't better. The advantage of this is that different calling code can decide to bounce different classes of exceptions. Just like try ... catch, it gives you more fine-grained control.
      A massive flamewar beneath your chosen depth has not been shown here

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: perlmeditation [id://270223]
Approved by broquaint
Front-paged by abell
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others contemplating the Monastery: (3)
As of 2014-10-25 10:22 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    For retirement, I am banking on:










    Results (142 votes), past polls