| Previous | Next
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 accessed with a numeric subscript (subscripts start at 0). They are preceded by an "at" sign ( Hashes are unordered sets of key/value pairs accessed with the keys as subscripts. They are preceded by a percent sign ( NumbersPerl stores numbers internally as either signed integers or double-precision, floating-point values. Numeric literals are specified by 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 the legibility of a large number. To improve legibility, Perl allows you to use an underscore character instead. The underscore works only within literal numbers specified in your program, not in strings functioning as numbers or in data read from somewhere else. Similarly, the leading Be aware that in Perl 5.8, there are many changes in how Perl deals with integers and floating-point numbers. Regardless of how your system handles numbers and conversion between characters and numbers, Perl 5.8 works around system deficiencies to force more accurate number handling. Furthermore, whereas prior to 5.8 Perl used floating-point numbers exclusively in math operations, Perl 5.8 now uses and stores integers in numeric conversions and in arithmetic operations. String InterpolationStrings 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-1. Double-quoted string representations
Table 4-2 lists alternative quoting schemes that can be used in Perl. These are useful in diminishing the number of commas and quotes you may have to type, and they allow you not to 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 ( Table 4-2. Quoting syntax in Perl
Here DocumentsA line-oriented form of quoting is based on the Unix shell "here-document" syntax. Following a #!/usr/local/bin/perl -w my $Price = 'right'; print <<"EOF"; The price is $Price. EOF The terminating string does not have to be quoted. For example, the previous example could have been written as: #!/usr/local/bin/perl -w my $Price = 'right'; print <<EOF; The price is $Price. EOF You can assign here documents to a string: my $assign_this_heredoc =<< "EOS"; This string is assigned to $whatever. EOS You can use a here document to execute commands: #!/usr/local/bin/perl -w print <<`CMD`; ls -l CMD You can stack here documents: #!/usr/local/bin/perl -w print <<"joe", <<"momma"; # You can stack them I said foo. joe I said bar. momma One caveat about here documents: you may have noticed in each of these examples that the quoted text is always left-justified. That's because any whitespace used for indentation will be included in the string. For example: #!/usr/local/bin/perl -w print <<" INDENTED"; Same old, same old. INDENTED Although you can use a trick of including whitespace in the terminating tag to keep it indented (as we did here), the string itself will have the whitespace embedded-in this case, it will be 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 qw/snap crackle pop/ With the quoting operators, you're not limited to qw!snap crackle pop! It's important that you remember not to use any delimiters except whitespace with
VariablesA 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 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 ArraysAn array is a variable that stores an ordered list of scalar values. Arrays are preceded by an "at" sign ( @numbers = (1,2,3); # Set the array @numbers to (1,2,3) To refer to a single element of an array, use the dollar sign ( @date = (8, 24, 70);
HashesA hash is a set of key/value pairs. Hashes are preceded by a percent sign (
has two values (in key/value pairs). If you want to get the value associated with the key It is often more readable to use the %fruit = ( apples => 3, oranges => 6 ); Scalar and List ContextsEvery operation that you invoke in a Perl script is evaluated in a specific context, and how that operation behaves may depend on the 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 ($sec,$min,$hour,$mday,$mon,$year,$wday,$yday,$isdst) = localtime( ); But in a scalar context, $now = localtime( ); 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: $a = (2, 4, 6, 8); gives Another type of statement that might be confusing is the evaluation of an array or hash variable as a scalar. For example: $b = @c; 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 If necessary, you can force a scalar context in the middle of a list by using the 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 |