4.2 Data Types and VariablesPerl has three basic data types: scalars , arrays , and hashes .
Scalars are essentially simple variables.
They are preceded by a
dollar sign (
Arrays are ordered lists of scalars that you
access with a numeric subscript (subscripts start at 0).
They are preceded by an
"at" sign (
Hashes are unordered sets of
key/value pairs that you access using
the keys as subscripts.
They are preceded by a
percent sign ( 4.2.1 NumbersPerl stores numbers internally as either signed integers or double-precision floating-point values. Numeric literals are specified in any of the following floating-point or integer formats: Since Perl uses the comma as a list separator, you cannot use a comma for improving legibility of a large number. To improve legibility, Perl allows you to use an underscore character instead. The underscore only works within literal numbers specified in your program, not in strings functioning as numbers or in data read from somewhere else. Similarly, the leading12345 # integer -54321 # negative integer 12345.67 # floating point 6.02E23 # scientific notation 0xffff # hexadecimal 0377 # octal 4_294_967_296 # underline for legibility
0x
for hex and
0
for octal work only for literals.
The automatic conversion of a string to a number does not recognize these
prefixes - you must do an explicit conversion.
4.2.2 String Interpolation
Strings are sequences of characters.
String literals are usually delimited by either single ( Table 4-1 lists all the backslashed or escape characters that can be used in double-quoted strings.
Table 4-2 lists alternative quoting schemes that can be used in Perl.
They are useful in diminishing the number of commas and quotes you may
have to type, and also allow you to not worry about escaping characters
such as backslashes when there are many instances in your data.
The generic forms allow you to use any non-alphanumeric, non-whitespace
characters as delimiters in place of the slash (
4.2.3 ListsA list is an ordered group of scalar values. A literal list can be composed as a comma-separated list of values contained in parentheses, for example: The generic form of list creation uses the quoting operator(1,2,3) # array of three values 1, 2, and 3 ("one","two","three") # array of three values "one", "two", and "three"
qw//
to
contain a list of values separated by white space:
qw/snap crackle pop/ 4.2.4 Variables
A variable always begins with the character that identifies its
type:
Variables have the
Simple variable assignment uses the assignment operator ( Scalar variables are always named with an initial$age = 26; # assigns 26 to $age @date = (8, 24, 70); # assigns the three-element list to @date %fruit = ('apples', 3, 'oranges', 6); # assigns the list elements to %fruit in key/value pairs
$
,
even when referring to a scalar value that is part of an array or hash.
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 4.2.4.1 ArraysAn array is a variable that stores an ordered list of scalar values. Arrays are preceded by an "at" (@) sign. To refer to a single element of an array, use the dollar sign (@numbers = (1,2,3); # Set the array @numbers to (1,2,3)
$
) with the variable
name (it's a scalar), followed by the index of the element in square brackets (the
subscript
operator
).
Array elements
are numbered starting at 0. Negative indexes count backwards from the
last element in the list (i.e., -1 refers to the last element of the list).
For example, in this list:
@date = (8, 24, 70);
$date[2]
is the value of the third element, 70.
4.2.4.2 HashesA hash is a set of key/value pairs. Hashes are preceded by a percent (%) sign. To refer to a single element of a hash, you use the hash variable name followed by the "key" associated with the value in curly brackets. For example, the hash: has two values (in key/value pairs). If you want to get the value associated with the key%fruit = ('apples', 3, 'oranges', 6);
apples
, you use
$fruit{'apples'}
.
It is often more readable to use the %fruit = ( apples => 3, oranges => 6 ); 4.2.5 Scalar and List Contexts
Every operation
that you invoke in a Perl script is evaluated in a
specific context, and how that operation behaves may depend on
which context it is being called in. There are two major contexts:
scalar
and
list
.
All operators know which context they are in,
and some return lists in contexts wanting a list, and scalars in
contexts wanting a scalar.
For example, the But in a scalar context,($sec,$min,$hour,$mday,$mon,$year,$wday,$yday,$isdst) = localtime();
localtime
returns the number of seconds since
January 1, 1970:
Statements that look confusing are easy to evaluate by identifying the proper context. For example, assigning what is commonly a list literal to a scalar variable:$now = localtime(); gives$a = (2, 4, 6, 8);
$a
the value 8. The context forces the right side to evaluate to a scalar,
and the action of the comma operator in the expression (in the scalar context) returns the
value farthest to the right.
Another type of statement that might be confusing is the evaluation of an array or hash variable as a scalar, for example: When an array variable is evaluated as a scalar, the number of elements in the array is returned. This type of evaluation is useful for finding the number of elements in an array. The special$b = @c;
$#
array
form of an array value returns the
index of the last member of the list (one less than the number of elements).
If necessary, you can force a scalar context in the middle of a list by
using the 4.2.6 Declarations and ScopeIn Perl, only subroutines and formats require explicit declaration. Variables (and similar constructs) are automatically created when they are first assigned. Variable declaration comes into play when you need to limit the scope of a variable's use. You can do this in two ways:
Therefore, we can say that a See Section 4.7, "Subroutines " later in this chapter for further discussion. Copyright © 2001 O'Reilly & Associates. All rights reserved. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|