My solutions could easily be packaged, but I imagine there's something already around that does this...
#!/usr/bin/perl #--------------------------------------------------------------------- +----------- # SELECT.pl # This code is GPL, but I'd love to see any mods: email join('.','rick +m@isite','net','au') #--------------------------------------------------------------------- +----------- use strict; #--------------------------------------------------------------------- +----------- # Example uses #--------------------------------------------------------------------- +----------- print "Do you wish to find a mirror site?\n"; Select( 'Yes' => 1, 'No' => sub{print "OK, exiting now."; exit}, sub{print "Invalid option, assuming you don't want to continue."; +exit} ); print "What nationality are you?\n"; print "I will use " . Insist( 'English' => 'mirror.co.uk', 'French' => 'mirror.fr', 'American' => 'mirror.com', 'None of the above' => sub {print "Enter a custom mirror domain: " +; return <>}, # you should return 0 from your subs if yo +u want to # get the name of the option returned, oth +erwise you'll # get the return value of the sub. "You must select a nationality", ) . "\n"; #--------------------------------------------------------------------- +----------- # The routines #--------------------------------------------------------------------- +----------- # Select allows a single opportunity to make a selection from a list # $result = Select('name0'=>'value', ... , 'namen'=>sub{ ... }, 'Fail +message'); # $result = Select('name0'=>'value', ... , 'namen'=>sub{ ... }, sub { +... }); # $result = Select('name0'=>'value', ... , 'namen'=>sub{ ... }); # If there's a valid response it returns the 'value' or executes the s +ub{} # associated with the selected name, otherwise it executes the fail + sub{} # or prints the 'fail message' and returns a 0 sub Select { my $fail = pop if ($#_ % 2 == 0); my @options = @_; for (my $i=0; $i<$#options; $i+=2) { print '(' . (int($i/2)+1) . ') ' . $options[$i] . "\n"; # Prin +t the menu } my $input = <>; $input=~s/[^\d]//g; if (($input < 1) || ($input > (int($#options/2)+1))) { if ($fail) { if (ref($fail) eq 'CODE') { &$fail } else { print $fail."\ +n" } return 0; } else { die("You have not selected a valid option") #Maybe should +be a warn or something? } } elsif (ref($options[(($input*2)-1)]) eq 'CODE') { # Execute the code and return the response or the name of the +option my $value = &{$options[(($input*2)-1)]}; return $value || $options[($input*2)-2]; } else { # Or just return the value of the option return $options[(($input*2)-1)]; } } # Insist requires a valid entry and keeps asking until it gets one. # $result = Insist('name0'=>'value', ... , 'namen'=>sub{ ... }, 'Fail +message'); # $result = Insist('name0'=>'value', ... , 'namen'=>sub{ ... }); # returns the 'value' or executes the sub{} associated with the select +ed name sub Insist { my $fail = pop if ($#_ % 2 == 0); $fail ||= q|You must select a valid option.|; my @options = @_; my $result; die("Insist: Fail message should be a scalar.") if ref($fail); until ($result = Select(@options, $fail)) {} return $result; }

In reply to My solution... by BigLug
in thread Ksh style select menus in perl by Anonymous Monk

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.