Allen Allen - 1 year ago 91
Perl Question

where is the error in this perl code

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

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 Source

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.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download