You basic code works just fine on Win10 using Active State Perl v5.24.3.
Get to a native Win10 command line and try it.
Update: If you are running some kind of Unix variant under Windows. I would favor using a regex instead of chomp for line ending removal. A regex will always work, not matter which direction you move input files around. $line =~ s/^\s*|\s*$//g removes leading and trailing whitespaces (including any CR or LF characters) and is faster than 2 separate regexes for that purpose.
use strict ;
use warnings ;
my $numClients ;
use Scalar::Util qw(looks_like_number) ;
print "How many Clients are you entering?" ;
$numClients = <STDIN> ;
chomp($numClients) ;
if ( !looks_like_number($numClients) ) {
print " Your input is NOT a numeric\n" ;
exit(1) ;
}
print "$numClients\n";
__END__
C:\PerlProjects\Monks>perl getposinteger3.pl
How many Clients are you entering?0
0
C:\PerlProjects\Monks>perl getposinteger3.pl
How many Clients are you entering? -12
-12
C:\PerlProjects\Monks>perl getposinteger3.pl
How many Clients are you entering? 0005
0005
C:\PerlProjects\Monks>perl getposinteger3.pl
How many Clients are you entering?14.3
14.3
I would code a loop like this as below.....
It loops until a valid number is entered instead of aborting.
Instead of "looks like a number" which allows fractions and exponentials, the code below looks for a non-zero positive integer although leading zeroes are allowed.
In this scenario, parens around the print statement are required to get the print to happen "in the middle" of the while statement. Try with and without the parens to see what I mean.
use strict;
use warnings;
my $numClients;
while ( (print "Enter Number of Clients: "), $numClients=<STDIN>,
$numClients !~ /^\s*(?:[0]*)?[1-9]\d*\s*$/)
{
print "Your input is NOT a positive integer!\n";
}
$numClients += 0; #optional conversion to numeric
print "Number of Clients: $numClients\n";
__END__
C:\PerlProjects\Monks>perl getposinteger2.pl
Enter Number of Clients: 5
Number of Clients: 5
C:\PerlProjects\Monks>perl getposinteger2.pl
Enter Number of Clients: -23
Your input is NOT a positive integer!
Enter Number of Clients: 2 3
Your input is NOT a positive integer!
Enter Number of Clients: 14.3
Your input is NOT a positive integer!
Enter Number of Clients: -12 23
Your input is NOT a positive integer!
Enter Number of Clients: 5
Number of Clients: 5
C:\PerlProjects\Monks>perl getposinteger2.pl
Enter Number of Clients: 00003
Number of Clients: 3
You can add extra error checking like this:
use strict;
use warnings;
my $numClients;
while ( (print "Enter Number of Clients: "), $numClients=<STDIN>,
$numClients !~ /^\s*(?:[0]*)?[1-9]\d*\s*$/ or $numClients >=1
+00)
{
print "Your input is NOT a positive integer less than 100!\n";
}
$numClients += 0; #optional conversion to numerio
print "Number of Clients: $numClients\n";
__END__
C:\PerlProjects\Monks>perl getposinteger2.pl
Enter Number of Clients: 123
Your input is NOT a positive integer less than 100!
Enter Number of Clients: 5
Number of Clients: 5
|