in reply to Stdin for just numbers.
If you want only numbers, you have to check for numbers. One way to do that:
print"Enter the starting amount: "; while( $startAmount !~ /^\d+/ ) { $startAmount = <STDIN>; chomp( $startAmount ); if ( $startAmount !~ /^\d+$/ ) { print"Numbers only! Try again: "; } else { last; } }
The above checks $startAmount twice. We can avoid that with a bare block and redo:
print"Enter the starting amount: "; { chomp( $startAmount = <STDIN> ); unless ( $startAmount =~ /^\d+$/ ) { print"Numbers only! Try again: "; redo; } # this line is reached if $startAmount is a number print "ok, \$startAmount is $startAmount\n"; }
There are many more ways to do that...
|
|---|