Perl language structure: Difference between revisions

Content deleted Content added
AnomieBOT (talk | contribs)
m Dating maintenance tags: {{When}}
Line 119:
$false = '0'; # the string zero
$false = ""; # the empty string
$false = (); # the empty list
$false = undef; # the return value from undef
$false = 2-3+1 # computes to 0 that is converted to "0" so it is false
</source>
 
All other (non-zero evaluating) values evaluate to true. This includes the odd self-describing literal string of "0 but true", which in fact is 0 as a number, but true when used as a boolean. All non-numeric strings also have this property, but this particular string is truncated by Perl without a numeric warning. A less explicit but more conceptually portable version of this string is '0E0' or '0e0', which does not rely on characters being evaluated as 0, because '0E0' is literally zero times ten to the power zero. The empty hash <code>{}</code> is also true.
 
Evaluated boolean expressions are also scalar values. The documentation does not promise which ''particular'' value of true or false is returned. Many boolean operators return 1 for true and the empty-string for false. The ''defined()'' function determines whether a variable has any value set. In the above examples, ''defined($false)'' is true for every value except ''undef''.