#! /usr/bin/perl use warnings; use strict; use feature qw{ say }; menu('Main Menu', [ 'Get date and time', sub { say scalar localtime } ], [ 'Go to submenu', sub { $_[0] = submenu() } ], [ 'Quit', sub { $_[0] = 1 } ], ); sub menu { my ($title, @options) = @_; my $done; while (! $done) { say $title; say 1 + $_, ". $options[$_][0]" for 0 .. $#options; chomp( my $reply = <> ); next unless $reply =~ /^[1-9][0-9]*$/; next if $reply > @options; --$reply; $options[$reply][1]->($done, $reply); } return $done } sub submenu { my $result = menu('Submenu', [ 'Get username', sub { say scalar getpwuid $> } ], [ 'Back', \&pass ], [ 'Quit', \&pass ], ); return $result == 2 # Propagate Quit. } sub pass { $_[0] = $_[1] }