"Perl Programmers Reference Guide (англ.) (программ.) /19.12.1998/ " - читать интересную книгу автора

that return scalars don't need to care (and, in fact,
can't care) whether the context is looking for a string or
a number.




10 perl 5.005, patch 02 14/Jun/98





PERLDATA(1) Perl Programmers Reference Guide PERLDATA(1)


Scalars aren't necessarily one thing or another. There's
no place to declare a scalar variable to be of type
"string", or of type "number", or type "filehandle", or
anything else. Perl is a contextually polymorphic
language whose scalars can be strings, numbers, or
references (which includes objects). While strings and
numbers are considered pretty much the same thing for
nearly all purposes, references are strongly-typed
uncastable pointers with builtin reference-counting and
destructor invocation.

A scalar value is interpreted as TRUE in the Boolean sense
if it is not the null string or the number 0 (or its
string equivalent, "0"). The Boolean context is just a
special kind of scalar context.

There are actually two varieties of null scalars: defined
and undefined. Undefined null scalars are returned when
there is no real value for something, such as when there
was an error, or at end of file, or when you refer to an
uninitialized variable or element of an array. An
undefined null scalar may become defined the first time
you use it as if it were defined, but prior to that you
can use the _d_e_f_i_n_e_d_(_) operator to determine whether the
value is defined or not.

To find out whether a given string is a valid nonzero
number, it's usually enough to test it against both
numeric 0 and also lexical "0" (although this will cause
----wwww noises). That's because strings that aren't numbers
count as 0, just as they do in aaaawwwwkkkk:

if ($str == 0 && $str ne "0") {
warn "That doesn't look like a number";