<?xml version="1.0" encoding="windows-1252"?>
<node id="632816" title="moritz's scratchpad" created="2007-08-15 14:07:38" updated="2007-08-15 10:07:38">
<type id="182711">
scratchpad</type>
<author id="616540">
moritz</author>
<data>
<field name="doctext">
&lt;h2&gt;Allowing a callback to return more than one level&lt;/h2&gt;

&lt;p&gt;I want to write some kind of text-only menu system, and that defines callbacks which are called for specific input.&lt;/p&gt;

&lt;p&gt;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.&lt;/p&gt;

&lt;p&gt;How? A normal return() doesn't accomplish that, so here is my hack:

&lt;code&gt;

use strict;
use warnings;
use 5.010;

my $return_args;

sub RETURN {
    $return_args = [@_];
    no warnings 'exiting';
    last 'UNIQUELABEL';
}


sub menu {
UNIQUELABEL:
    for (@_) {
        $_-&gt;();
    }

    return @{$return_args} if $return_args;
};

say menu
    sub { say 'go on'    },
    sub { RETURN 'ended' },
    sub { 'never called' },
&lt;/code&gt;</field>
</data>
</node>
