in reply to Guess That Number

Instead of many ifs and explicite nexts, I'd use given/when:
use 5.010; use strict; use warnings; my $low = 1; my $high = 1000; my $goal = $low + int rand $high - $low + 1; sub prompt {print "Enter a number between $low and $high: "} prompt; while (my $guess = <>) { # Allows you terminate the program with ^D $guess =~ s/^\s+//; $guess =~ s/\s+$//; given ($guess) { when ("") {;} when (/[^0-9]/) {say "Please enter digits only";} when ($goal) {say "You won!"; exit;} when ($_ < $low || $_ > $high) {say "Please stay within limits +"} when ($_ < $goal) {$low = $_ + 1} when ($_ > $goal) {$high = $_ - 1} } } continue {prompt} __END__

Replies are listed 'Best First'.
Re^2: Guess That Number
by jdporter (Paladin) on Jul 09, 2009 at 23:20 UTC
    /[^0-9]/

    more succintly written as /\D/.

      No actually. /[^0-9]/ is correct, where /\D/ isn't. Your perl is from the previous century if /\D/ equals /[^0-9]/.

        s/succinctly/correctly/, then.