Hu, this breaks strictures, better use code references :
use strict;
use warnings;
my $state = \&start;
while (my $next = $state->()) {
$state = $next;
}
sub start {
...get input...
if (/1/) { return \&menu_1 }
elsif (/2/) { return \&menu_2 }
...
}
sub menu_1 {
...
}
sub menu_2 {
...
}
By the way instead of chaining multiple
if () {} blocks, using a dispatch table is more efficient and looks nicer:
use strict;
use warnings;
my $state = \&start;
while (my $next = $state->()) {
$state = $next;
}
sub start {
my %next_action = ( 1 => \&menu_1,
2 => \&menu_2,
3 => \&menu_3,
);
return $next_action{$_} if exists $next_action{$_}
}