home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
BURKS 2
/
BURKS_AUG97.ISO
/
SLAKWARE
/
D13
/
PERL2.TGZ
/
perl2.tar
/
usr
/
lib
/
perl5
/
pod
/
perldata.pod
< prev
next >
Wrap
Text File
|
1996-06-28
|
21KB
|
522 lines
=head1 NAME
perldata - Perl data types
=head1 DESCRIPTION
=head2 Variable names
Perl has three data structures: scalars, arrays of scalars, and
associative arrays of scalars, known as "hashes". Normal arrays are
indexed by number, starting with 0. (Negative subscripts count from
the end.) Hash arrays are indexed by string.
Scalar values are always named with '$', even when referring to a scalar
that is part of an array. It works like the English word "the". Thus
we have:
$days # the simple scalar value "days"
$days[28] # the 29th element of array @days
$days{'Feb'} # the 'Feb' value from hash %days
$#days # the last index of array @days
but entire arrays or array slices are denoted by '@', which works much like
the word "these" or "those":
@days # ($days[0], $days[1],... $days[n])
@days[3,4,5] # same as @days[3..5]
@days{'a','c'} # same as ($days{'a'},$days{'c'})
and entire hashes are denoted by '%':
%days # (key1, val1, key2, val2 ...)
In addition, subroutines are named with an initial '&', though this is
optional when it's otherwise unambiguous (just as "do" is often
redundant in English). Symbol table entries can be named with an
initial '*', but you don't really care about that yet.
Every variable type has its own namespace. You can, without fear of
conflict, use the same name for a scalar variable, an array, or a hash
(or, for that matter, a filehandle, a subroutine name, or a label).
This means that $foo and @foo are two different variables. It also
means that C<$foo[1]> is a part of @foo, not a part of $foo. This may
seem a bit weird, but that's okay, because it is weird.
Since variable and array references always start with '$', '@', or '%',
the "reserved" words aren't in fact reserved with respect to variable
names. (They ARE reserved with respect to labels and filehandles,
however, which don't have an initial special character. You can't have
a filehandle named "log", for instance. Hint: you could say
C<open(LOG,'logfile')> rather than C<open(log,'logfile')>. Using uppercase
filehandles also improves readability and protects you from conflict
with future reserved words.) Case I<IS> significant--"FOO", "Foo" and
"foo" are all different names. Names that start with a letter or
underscore may also contain digits and underscores.
It is possible to replace such an alphanumeric name with an expression
that returns a reference to an object of that type. For a description
of this, see L<perlref>.
Names that start with a digit may only contain more digits. Names
which do not start with a letter, underscore, or digit are limited to
one character, e.g. C<$%> or C<$$>. (Most of these one character names
have a predefined significance to Perl. For instance, C<$$> is the
current process id.)
=head2 Context
The interpretation of operations and values in Perl sometimes depends
on the requirements of the context around the operation or value.
There are two major contexts: scalar and list. Certain operations
return list values in contexts wanting a list, and scalar values
otherwise. (If this is true of an operation it will be mentioned in
the documentation for that operation.) In other words, Perl overloads
certain operations based on whether the expected return value is
singular or plural. (Some words in English work this way, like "fish"
and "sheep".)
In a reciprocal fashion, an operation provides either a scalar or a
list context to each of its arguments. For example, if you say
int( <STDIN> )
the integer operation provides a scalar context for the <STDIN>
operator, which responds by reading one line from STDIN and passing it
back to the integer operation, which will then find the integer value
of that line and return that. If, on the other hand, you say
sort( <STDIN> )
then the sort operation provides a list context for <STDIN>, which
will proceed to read every line available up to the end of file, and
pass that list of lines back to the sort routine, which will then
sort those lines and return them as a list to whatever the context
of the sort was.
Assignment is a little bit special in that it uses its left argument to
determine the context for the right argument. Assignment to a scalar
evaluates the righthand side in a scalar context, while assignment to
an array or array slice evaluates the righthand side in a list
context. Assignment to a list also evaluates the righthand side in a
list context.
User defined subroutines may choose to care whether they are being
called in a scalar or list context, but most subroutines do not
need to care, because scalars are automatically interpolated into
lists. See L<perlfunc/wantarray>.
=head2 Scalar values
All data in Perl is a scalar or an array of scalars or a hash of scalars.
Scalar variables may contain various kinds of singular data, such as
numbers, strings, and references. In general, conversion from one form to
another is transparent. (A scalar may not contain multiple values, but
may contain a reference to an array or hash containing multiple values.)
Because of the automatic conversion of scalars, operations and functions
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.
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 same thing for nearly all purposes, references are strongly-typed
uncastable pointers with built-in 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
defined() operator to determine whether the value is defined or not.
To find out whether a given string is a valid non-zero number, it's usually
enough to test it against both numeric 0 and also lexical "0" (although
this will cause B<-w> noises). That's because strings that aren't
numbers count as 0, just as the do in I<awk>:
if ($str == 0 && $str ne "0") {
warn "That doesn't look like a number";
}
That's usually preferable because otherwise you won't treat IEEE notations
like C<NaN> or C<Infinity> properly. At other times you might prefer to
use a regular expression to check whether data is numeric. See L<perlre>
for details on regular expressions.
warn "has nondigits" if /\D/;
warn "not a whole number" unless /^\d+$/;
warn "not an integer" unless /^[+-]?\d+$/
warn "not a decimal number" unless /^[+-]?\d+\.?\d*$/
warn "not a C float"
unless /^([+-]?)(?=\d|\.\d)\d*(\.\d*)?([Ee]([+-]?\d+))?$/;
The length of an array is a scalar value. You may find the length of
array @days by evaluating C<$#days>, as in B<csh>. (Actually, it's not
the length of the array, it's the subscript of the last element, since
there is (ordinarily) a 0th element.) Assigning to C<$#days> changes the
length of the array. Shortening an array by this method destroys
intervening values. Lengthening an array that was previously shortened
I<NO LONGER> recovers the values that were in those elements. (It used to
in Perl 4, but we had to break this make to make sure destructors were
called when expected.) You can also gain some measure of efficiency by
preextending an array that is going to get big. (You can also extend
an array by assigning to an element that is off the end of the array.)
You can truncate an array down to nothi