Allen Allen - 2 months ago 8
Perl Question

where is the error in this perl code

my $secret = int(1+rand(100));
LOOP: {

print "Please enter a guess from 1 to 100: ";
chomp(my $guess = <STDIN>);
my $found_it = 0;
given( $guess ) {
when ( ! /\A\d+\Z/ ) { say "Not a number!" }
when ( $_ > $secret ) { say "Too High!" }
when ( $_ < $secret ) { say "Too low!" }
default { say "Just right!"; $found_it++ }
}
last LOOP if $found_it;
redo LOOP;
}


This code cannot run. I cannot find what mistakes are!
Errors in pic.

Answer

It looks to me like whatever you are using isn't recognizing the newer keywords you are using.

If you aren't already, enable them with:

use 5.010; # or higher
# or
use feature 'switch';

In addition, on newer Perl versions, you will need to say

no warnings 'experimental::smartmatch';

since you are implicitly using smartmatch, and the way that works is planned to change in a future Perl version.

Comments