Beefy Boxes and Bandwidth Generously Provided by pair Networks
We don't bite newbies here... much

Non-local return via last()

by moritz (Cardinal)
on Sep 27, 2012 at 11:54 UTC ( #995995=CUFP: print w/replies, xml ) Need Help??

For $work I'm writing a text-only menu system, and that defines callbacks which are called for specific input.

Now the default behavior is to simply ignore the return value (trust me, it makes sense in my $work context), and exit the menu when the input is empty. However I want to give some callbacks the possibility to exit the menu too.

How? A normal return() doesn't accomplish that, so here is my hack:

use strict; use warnings; use 5.010; my @return_stack; sub RETURN { push @return_stack, [@_]; no warnings 'exiting'; last 'UNIQUELABEL'; } sub menu { UNIQUELABEL: while (1) { for (@_) { $_->(); } } return @{pop @return_stack}; }; say menu sub { say 'go on' }, sub { RETURN menu sub { say 'still here' }, sub { RETURN 'ended' }, sub { 'never called' }, }, sub { 'never called' }, ; __END__ go on still here ended

In my menu system, the inner loop is a bit different, but this captures the idea pretty well IMHO.

The same could be achieved through exceptions.

One thing it doesn't handle well is context: the callback is always called in the same context (here void context), so wantarray isn't useful in the callback.

Replies are listed 'Best First'.
Re: Non-local return via last()
by tobyink (Abbot) on Sep 27, 2012 at 12:37 UTC

    Nice. I've been looking for a solution for a while for resolving this disparity:

    use 5.010; use List::Util qw(first); sub sub1 { first { return 1 } @_; return 2; } sub sub2 { grep { return 1 } @_; return 2; } say sub1(0); say sub2(0);

    Given that first and grep are conceptually similar, it is counter-intuitive that return inside them means different things. While prototypes help first look like a built-in list operator, they don't help it act like one.

    I've played around with Scope::Upper a little to try to come up with a resolution for this, but haven't had any luck so far. (Though as a by-product I managed to come up with returning which is pretty fun.) I wonder if last might provide some kind of solution.

    perl -E'sub Monkey::do{say$_,for@_,do{($monkey=[caller(0)]->[3])=~s{::}{ }and$monkey}}"Monkey say"->Monkey::do'

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: CUFP [id://995995]
Front-paged by Corion
and all is quiet...

How do I use this? | Other CB clients
Other Users?
Others studying the Monastery: (10)
As of 2018-05-25 17:49 GMT
Find Nodes?
    Voting Booth?