Skip to content

Instantly share code, notes, and snippets.

@shlomif
Created September 12, 2009 15:08
Show Gist options
  • Save shlomif/185867 to your computer and use it in GitHub Desktop.
Save shlomif/185867 to your computer and use it in GitHub Desktop.
#!/usr/bin/perl
use warnings;
use strict;
use diagnostics;
use IO::Handle;
sub input_number
{
print "What is your number? (0-25) ";
STDOUT->flush();
my $number = <>;
chomp($number);
return $number;
}
sub check_number
{
my $number = shift;
if ($number< 0) {
print {*STDERR}
"The number can not be less than 0, please try again.\n";
return 0;
}
elsif ($number> 25) {
print {*STDERR}
"The number can not be more than 25, please enter your number.\n";
return 0;
}
elsif (($number !~ m{\A\d+\z}) || ($number =~ m{\A0\d})) {
print {*STDERR} "The number must be an integer.";
return 0;
}
else {
return 1;
}
}
my $number = input_number();
while (! check_number($number))
{
$number = input_number();
}
print "You've chosen $number! Congrats.\n";
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment