#!/usr/bin/perl use warnings; use strict; my @fruits = ("Apples", "Peaches", "Bananas"); my $choice = getChoice("Fruits", \@fruits, 2); print $fruits[$choice] . "\n\n"; sub getChoice { my $headline = shift; my $choicesref = shift; my $default = shift; system("clear"); my @choices = @{$choicesref}; my $x = 0; print "\n$headline:\n\n"; for my $i (0 .. $#choices) { print $i + 1 . ". $choices[$i]\n"; } print "\n"; my $inputstr = ""; while ($x == 0) { print "Enter your choice"; if ($default) { print " (Default: \"$default. $choices[$default - 1]\")"; } print ": "; $inputstr = ; chomp($inputstr); if ($inputstr eq "") { if ($default) { $inputstr = $default; $x = 1; } else { next; } } if ($inputstr eq "q") { print "Bye.\n\n"; exit 0; } if ($inputstr !~ /\D/ && $inputstr >= 1 && $inputstr <= $#choices + 1) { $x = 1; } } print "\n"; return $inputstr - 1; }