in reply to consolidating menu option processing
poorly thought out example
use strict; use warnings; use Data::Dump qw/ dd pp /; my $stash = menu_one( "say hi or say bye:\n", [ qr/hi/i => sub { print "hi\n"; $_[1]->{hi}++; 1 }, ], [ qr/quit|bye|adios/i => sub { print "bye bye\n"; 0 }, ], ); dd( $stash ); dd( menu_one( "say why :", [ qr/.*/ => sub { die "why?!?!?\n"; 0 }, ], ), ); sub menu_one { my( $header, @actions ) = @_; my $ret = 1; my $stash = {}; while( $ret ){ my $line = getone($header); ACTION_LOOP: for my $action ( @actions ){ my( $match, $callback ) = @$action; if( $line =~ $match ){ $ret = $callback->( $line, $stash ); last ACTION_LOOP; } } } return $stash; } sub getone { print @_; scalar <>; }
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re^2: consolidating menu option processing
by temporal (Pilgrim) on Jul 19, 2013 at 13:32 UTC |