How can I build a simple menu in Perl?

前端 未结 6 1105
野的像风
野的像风 2020-12-11 20:23

I\'m working on a Perl script that requires some basic menu functionality. Ultimately I would like each menu to have a few options and then the option to either return to th

6条回答
  •  情歌与酒
    2020-12-11 21:01

    You could use a module such as Term::Choose:

    use Term::Choose qw( choose );
    
    my $submenus = {
        menu1 => [ qw( s_1 s_2 s_3 ) ],
        menu2 => [ qw( s_4 s_5 s_6 s_7) ],
        menu3 => [ qw( s_8 s_9 ) ],
    };
    my @menus = ( qw( menu1 menu2 menu3 ) );
    my $mm = 0;
    MAIN: while ( 1 ) {
        my $i = choose( 
            [ undef, @menus ],
            { layout => 3, undef => 'quit', index => 1, default => $mm }
        );
        last if ! $i;
        if ( $mm == $i ) {
            $mm = 0;
            next MAIN;
        }
        else {
            $mm = $i;
        }
        $i--;
        SUB: while ( 1 ) {
            my $choice = choose(
                [ undef, @{$submenus->{$menus[$i]}} ],
                { layout => 3, undef => 'back' }
            );
            last SUB if ! defined $choice;
            say "choice: $choice";
        }
    }
    

提交回复
热议问题