perl - If statement with scalar conditional -
i having difficulty understanding following expression in perl
.
(my $attempts = 0; $attempts < $maxatt; $attempts++) { print "please try again.\n" if $attempts; print "$prompt: "; $response = readline(*stdin); chomp($response); return $response if $response; }
what mean use scalar conditional? i.e. makes return true vs false? assuming statement reads if ($attempts) { print ... }
.
perl doesn't have distinct concept of "boolean value".
per the perldata
manpage:
a scalar value interpreted false in boolean sense if undefined, null string or number 0 (or string equivalent, "0"), , true if else. boolean context special kind of scalar context no conversion string or number ever performed.
so in example, if $attempts
means if $attempts > 0
.
incidentally, means $attempts > 0
not evaluate boolean, because there's no such thing. instead, per the perlop
manpage, operators >
:
[…] return
1
true , special version of defined empty string,""
, counts 0 exempt warnings improper numeric conversions,"0 true"
is.
(hat-tip ikegami pointing out return false not quite normal empty string.)
Comments
Post a Comment