Tải bản đầy đủ (.pdf) (125 trang)

perl the complete reference second edition phần 2 docx

Bạn đang xem bản rút gọn của tài liệu. Xem và tải ngay bản đầy đủ của tài liệu tại đây (859.58 KB, 125 trang )

Chapter 4: Variables and Data
85
FUNDAMENTALS
Arrays
An array is just a set of scalars. It’s made up of a list of individual scalars that are
stored within a single variable. You can refer to each scalar within that list using a
numerical index. You can use arrays to store any kind of list data, from the days of
the week to a list of all the lines in a file. Creating individual scalars for each of these
is cumbersome, and in the case of the file contents, impossible to prepare for. What
happens if the input file has 100 lines instead of 10? The answer is to use an array,
which can be dynamically sized to hold any number of different values.
Creation
Array variables have are prefixed with the @ sign and are populated using either
parentheses or the qw operator. For example:
@array = (1, 2, 'Hello');
@array = qw/This is an array/;
The second line uses the qw// operator, which returns a list of strings, separating the
delimited string by white space. In this example, this leads to a four-element array; the
first element is 'this' and last (fourth) is 'array'. This means that you can use newlines
within the specification:
@days = qw/Monday
Tuesday

Sunday/;
We can also populate an array by assigning each value individually:
$array[0] = 'Monday';

$array[6] = 'Sunday';
However, you should avoid using square brackets to create a normal array.
The line
@array = [1, 2, 'Hello'];


initializes @array with only one element, a reference to the array contained in the
square brackets. We’ll be looking at references in Chapter 10.
86
Perl: The Complete Reference
Extracting Individual Indices
When extracting individual elements from an array, you must prefix the variable with
a dollar sign (to signify that you are extracting a scalar value) and then append the
element index within square brackets after the name. For example:
@shortdays = qw/Mon Tue Wed Thu Fri Sat Sun/;
print $shortdays[1];
Array indices start at zero, so in the preceding example we’ve actually printed “Tue.”
You can also give a negative index—in which case you select the element from the end,
rather than the beginning, of the array. This means that
print $shortdays[0]; # Outputs Mon
print $shortdays[6]; # Outputs Sun
print $shortdays[-1]; # Also outputs Sun
print $shortdays[-7]; # Outputs Mon
Remember:
■ Array indices start at zero, not one, when working forward; for example:
@days = qw/Monday
Tuesday

Sunday/;
print "First day of week is $days[0]\n";

Array indices start at –1 for the last element when working backward.
The use of $[, which changes the lowest index of an array, is heavily deprecated, so the
preceding rules should always apply.
Be careful when extracting elements from an array using a calculated index. If you
are supplying an integer, then there shouldn’t be any problems with resolving that to

an array index (provided the index exists). If it’s a floating point value, be aware that
Perl always truncates (rounds down) values as if the index were interpreted within
the int function. If you want to round up, use sprintf—this is easily demonstrated;
the script
Chapter 4: Variables and Data
87
FUNDAMENTALS
@array = qw/a b c/;
print("Array 8/5 (int) is: ", $array[8/5], "\n");
print("Array 8/5 (float) is: ",
$array[sprintf("%1.0f",(8/5))],"\n");
generates
Array index 8/5 (int) is: b
Array index 8/5 (float) is: c
The bare 8 / 5, which equates to 1.6, is interpreted as 1 in the former statement, but
2 in the latter.
Slices
You can also extract a “slice” from an array—that is, you can select more than one item
from an array in order to produce another array.
@weekdays = @shortdays[0,1,2,3,4];
The specification for a slice must a list of valid indices, either positive or negative, each
separated by a comma. For speed, you can also use the range operator:
@weekdays = @shortdays[0 4];
Ranges also work in lists:
@weekdays = @shortdays[0 2,6,7];
Note that we’re accessing the array using an @ prefix—this is because the return value
that we want is another array, not a scalar. If you try accessing multiple values using
$array you’ll get nothing, but an error is only reported if you switch warnings on:
$ perl -ew "print $ARGV[2,3];" Fred Bob Alice
Multidimensional syntax $ARGV[2,3] not supported at -e line 1.

Useless use of a constant in void context at -e line 1.
Use of uninitialized value in print at -e line 1.
Single Element Slices
Be careful when using single element slices. The statement
print @array[1];
is no different than
print $array[1];
except that the former returns a single element list, while the latter returns a single
scalar. This can be demonstrated more easily using the fragment
@array[1] = <DATA>;
which actually reads in all the remaining information from the DATA filehandle,
but assigns only the first record read from the filehandle to the second argument
of the array.
Size
The size of an array can be determined using scalar context on the array—the returned
value will be the number of elements in the array:
@array = (1,2,3);
print "Size: ",scalar @array,"\n";
The value returned will always be the physical size of the array, not the number of
valid elements. You can demonstrate this, and the difference between scalar @array
and $#array, using this fragment:
@array = (1,2,3);
$array[50] = 4;
print "Size: ",scalar @array,"\n";
print "Max Index: ", $#array,"\n";
This should return
Size: 51
Max Index: 50
88
Perl: The Complete Reference

Chapter 4: Variables and Data
89
FUNDAMENTALS
There are only four elements in the array that contain information, but the array is
51 elements long, with a highest index of 50.
Hashes
Hashes are an advanced form of array. One of the limitations of an array is that the
information contained within it can be difficult to get to. For example, imagine that you
have a list of people and their ages. We could store that information in two arrays, one
containing the names and the other their ages:
@names = qw/Martin Sharon Rikke/;
@ages = (28,35,29);
Now when we want to get Martin’s age, we just access index 0 of the @ages array.
Furthermore, we can print out all the people’s ages by printing out the contents of each
array in sequence:
for($i=0;$i<@names;$i)
{
print "$names[$i] is $ages[$i] years old\n";
}
But how would you print out Rikke’s age if you were only given her name, rather than
her location within the @names array? The only way would be to step through @names
until we found Rikke, and then look up the corresponding age in the @ages array. This is
fine for the three-element array listed here, but what happens when that array becomes
30, 300, or even 3000 elements long? If the person we wanted was at the end of the list,
we’d have to step through 3000 items before we got to the information we wanted.
The hash solves this, and numerous other problems, very neatly by allowing us to
access that @ages array not by an index, but by a scalar key. Because it’s a scalar, that
value could be anything (including a reference to another hash, array, or even an object),
but for this particular problem it would make sense to make it the person’s name:
%ages = ('Martin' => 28,

'Sharon' => 35,
'Rikke' => 29,);
Now when we want to print out Rikke’s age, we just access the value within the hash
using Rikke’s name as the key:
print "Rikke is $ages{Rikke} years old\n";
90
Perl: The Complete Reference
The process works on 3000 element hashes just as easily as it does on 3:
print "Eileen is $ages{Eileen} years old\n";
We don’t have to step through the list to find what we’re looking for—we can just
go straight to the information. Perl’s hashes are also more efficient than those supported
by most other languages. Although it is possible to end up with a super-large hash
that takes a long time to locate its values, you are probably talking tens or hundreds of
thousands of entries. If you are working with that level of information though, consider
using a DBM file—see Chapter 13 for more information.
Creation
Hashes are created in one of two ways. In the first, you assign a value to a named key
on a one-by-one basis:
$ages{Martin} = 28;
In the second, you use a list, which is converted by taking individual pairs from the
list: the first element of the pair is used as the key, and the second, as the value. For
example,
%hash = ('Fred' , 'Flintstone', 'Barney', 'Rubble');
For clarity, you can use => as an alias for , to indicate the key/value pairs:
%hash = ('Fred' => 'Flintstone',
'Barney' => 'Rubble');
When specifying the key for a hash element, you can avoid using quotes within the
braces according to the normal brace expansion rules:
$ages{Martin} = 28;
However, if the contents are a more complex term, they will need to be quoted:

$ages{'Martin-surname'} = 'Brown';
You can also use the - operator in front of a word, although this makes the key
include the leading - sign as part of the key:
%hash = (-Fred => 'Flintstone', -Barney => 'Rubble');
print $hash{-Fred};
TEAMFLY






















































Team-Fly

®

For single-letter strings, however, this will raise a warning; use single quotes to
explicitly define these arguments.
Extracting Individual Elements
You can extract individual elements from a hash by specifying the key for the value
that you want within braces:
print $hash{Fred};
Care needs to be taken when embedding strings and/or variables that are made
up of multiple components. The following statements are identical, albeit with a slight
performance trade-off for the former method:
print $hash{$fred . $barney};
print $hash{"$fred$barney"};
When using more complex hash keys, use sprintf:
print $hash{sprintf("%s-%s:%s",$a,$b,$c)};
You can also use numerical values to build up your hash keys—the values just
become strings. If you are going to use this method, then you should use sprintf to
enforce a fixed format for the numbers to prevent minor differences from causing you
problems. For example, when formatting time values, it’s better to use
$hash{sprintf("%02d%02d",$hours,$min)};
than
$hash{$hours . $min};
With the former, all times will be displayed in the form ‘0505’ instead of ‘55’.
Extracting Slices
You can extract slices out of a hash just as you can extract slices from an array.
You do, however, need to use the @ prefix because the return value will be a list
of corresponding values:
%hash = (-Fred => 'Flintstone', -Barney => 'Rubble');
print join("\n",@hash{-Fred,-Barney});
Chapter 4: Variables and Data

91
FUNDAMENTALS
92
Perl: The Complete Reference
Using $hash{-Fred, -Barney} would return nothing.
Extracting Keys, Values, or Both
You can get a list of all of the keys from a hash by using keys:
%ages = ('Martin' => 28, 'Sharon' => 35, 'Rikke' => 29);
print "The following are in the DB: ",join(', ',keys %ages),"\n";
You can also get a list of the values using values:
%ages = ('Martin' => 28, 'Sharon' => 35, 'Rikke' => 29);
print "The following are in the DB: ",join(', ',values %ages),"\n";\
These can be useful in loops when you want to print all of the contents of a hash:
foreach $key (%ages)
{
print "$key is $ages{$key} years old\n";
}
The problem with both these functions is that on large hashes (such as those
attached to external databases), we can end up with very large memory-hungry
temporary lists. You can get round this by using the each function, which returns
key/value pairs. Unlike keys and values, the each function returns only one pair
for each invocation, so we can use it within a loop without worrying about the size
of the list returned in the process:
while (($key, $value) = each %ages)
{
print "$key is $ages{$key} years old\n";
}
The order used by keys, values, and each is unique to each hash, and its order can’t
be guaranteed. Also note that with each, if you use it once outside of a loop, the
next invocation will return the next item in the list. You can reset this “counter” by

evaluating the entire hash, which is actually as simple as
sort keys %hash;
Chapter 4: Variables and Data
93
FUNDAMENTALS
Checking for Existence
If you try to access a key/value pair from a hash that doesn’t exist, you’ll normally get
the undefined value, and if you have warnings switched on, then you’ll get a warning
generated at run time. You can get around this by using the exists function, which
returns true if the named key exists, irrespective of what its value might be:
if (exists($ages{$name}))
{
print "$name if $ages{$name} years old\n";
}
else
{
print "I don't know the age of $name\n";
}
Sorting/Ordering
There is no way to simply guarantee that the order in which a list of keys, values, or
key/value pairs will always be the same. In fact, it’s best not even to rely on the order
between two sequential evaluations:
print(join(', ',keys %hash),"\n");
print(join(', ',keys %hash),"\n");
If you want to guarantee the order, use sort, as, for example:
print(join(', ',sort keys %hash),"\n");
If you’re accessing a hash a number of times and want to use the same order,
consider creating a single array to hold the sorted sequence, and then use the array
(which will remain in sorted order) to iterate over the hash. For example:
my @sortorder = sort keys %hash;

foreach my $key (@sortorder)
Size
You get the size—that is, the number of elements—from a hash by using scalar context
on either keys or values:
print "Hash size: ",scalar keys %hash,"\n";
Don’t use each, as in a scalar context it returns the first key from the hash, not a
count of the key/value pairs, as you might expect.
If you evaluate a hash in scalar context, then it returns a string that describes the
current storage statistics for the hash. This is reported as “used/total” buckets. The
buckets are the storage containers for your hash information, and the detail is only
really useful if you want to know how Perl’s hashing algorithm is performing on your
data set. If you think this might concern you, then check my Debugging Perl title, which
details how hashes are stored in Perl and how you can improve the algorithm for
specific data sets (see Appendix C for more information).
Lists
Lists are really a special type of array—essentially, a list is a temporary construct that
holds a series of values. The list can be “hand” generated using parentheses and the
comma operator,
@array = (1,2,3);
or it can be the value returned by a function or variable when evaluated in list context:
print join(',' @array);
Here, the @array is being evaluated in list context because the join function is
expecting a list (see Chapter 6 for more information on contexts).
Merging Lists (or Arrays)
Because a list is just a comma-separated sequence of values, you can combine lists together:
@numbers = (1,3,(4,5,6));
The embedded list just becomes part of the main list—this also means that we can
combine arrays together:
@numbers = (@odd,@even);
Functions that return lists can also be embedded to produce a single, final list:

@numbers = (primes(),squares());
94
Perl: The Complete Reference
Selecting Elements from Lists
The list notation is identical to that for arrays—you can extract an element from an
array by appending square brackets to the list and giving one or more indices:
$one = (5,4,3,2,1)[4];
Similarly, we can extract slices, although without the requirement for a leading
@ character:
@newlist = (5,4,3,2,1)[1 3];
Selecting List Elements from Function Calls
We can even use list notation on the return value from a function call. For example, the
localtime function returns a list of time values (hours, minutes, days, and so on), and
we can extract just the elements we want:
($hours,$minutes) = (localtime())[2 3];
Note that the parentheses go around the expression that returns the list, to imply
list context on the overall expression. The following are all examples of how not to
extract individual elements from a function that returns a list:
$hours = localtime()[2];
$hours,$minutes = localtime()[2 3];
($hours,$minutes) = localtime()[2 3];
List Assignment
We’ve now seen an example of list assignment, but it’s a useful feature that can be
applied to any statement or sequence of statements. You can use list assignment to
assign a series of values to a series of valid lvalues; for example, we can shorten
$one = 1;
$two = 2;
$three = 3;
to
($one, $two, $three) = (1,2,3);

Chapter 4: Variables and Data
95
FUNDAMENTALS
96
Perl: The Complete Reference
Note that you need list context on both sides of the assignment operator. If you
don’t want one of the values, you can also assign to the undefined value:
($one, undef, $three) = (1,2,3);
Finally, you can assign a value to an empty list, which will force list context on to
the function, although any value it returns will be lost:
() = function();
Arrays in List Context
When accessing an entire array or slice, arrays work as lists—that is
@array = (1,2);
($a, $b) = @array;
is equivalent to
($a, $b) = (1, 2);
Hashes in List Context
In the same way that hashes are essentially populated using a list, if you evaluate a
hash in list context, then what you get is a list of key/value pairs. For example,
my %hash = (Fred => 'Flintstone', Barney => 'Rubble');
@list = %hash;
print join(', ',@list);
produces
Barney, Rubble, Fred, Flintstone
Typeglobs
The typeglob is a special type of variable that literally means “everything called….” In
fact, a typeglob is a pointer to a symbol table entry. Typeglobs start with an asterisk;
the typeglob *foo contains the values of $foo, @foo, %foo and &foo. Typeglobs are
useful when you want to refer to a variable but don’t necessarily know what it is.

Chapter 4: Variables and Data
97
FUNDAMENTALS
Although this isn’t particularly useful for the three main data types, it can be useful
for exchanging filehandles:
$myfh = *STDOUT;
This is useful when you want to use filehandles within a function call—although it’s
more natural to use references. See Chapter 6 for some more examples of this use.
The defined Function and the Undefined Value
The undefined value, undef, is an alternative to the null value used in C. In essence,
undef means that the variable has had no value assigned. This is useful if you want to
create an undefined variable—one that has no value. Compare the undefined value with
an integer with a value of 0 or an empty string, both of which indicate valid values.
The undefined value will always evaluate to false if used in an expression, for
example the test in this fragment:
$value = undef;
if ($value)
{

will always fail. It will also raise an error because you’ve tried to access the contents of
an undefined value. In these situations, you can use the defined function to check the
value of a scalar. The defined function returns true if the scalar contains a valid value,
or false if the scalar contains undef:
if (defined($value))
{

Just to confuse you, defined will return false if a variable has never been named or
created, and also false if the variable does exist but has the undef value.
Note that the same rules apply to the scalar components of arrays or hashes: they
can contain the undefined value, even though the index or key is valid. This can cause

problems if you only use defined on a hash element. For example:
$hash{one} = undef;
print "Defined!\n" if (defined($hash{one}));
print "Exists!\n" if (defined($hash{one}));
This will only print “Exists!,” since the element’s value remains undefined.
98
Perl: The Complete Reference
Default Values
It’s not necessary within Perl to initialize variables with some default values. Perl
automatically creates all scalars as empty (with the undefined value). Lists and hashes
are automatically created empty. That said, there is nothing wrong with setting the
initial value of a variable—it won’t make any difference to Perl—it’s good programming
practice if only for its sheer clarity effect, especially if you are using my to declare the
variables beforehand. See Chapter 6 for information on using my.
Other Tokens
Perl supports a few other tokens that are identified by Perl as containing a value or
indicating a state, even though they are aren’t truly variables. These are listed in Table 4-4.
Special Variables
Perl keeps an internal list of special variables that supply information and data about
the current scripts environment. The subsections that follow include standard variables
built into the interpreter, variables that have special meanings to core modules (such as
pragmas and Exporter), and also the special filehandles used for communicating with
the outside world.
Token Value
_ _LINE_ _ The current line number within the current file.
_ _FILE_ _ The name of the current file.
_ _PACKAGE_ _ The name of the current package. If there is no current
package, then it returns the undefined value.
_ _END_ _ Indicates the end of the script (or interpretable Perl) within a
file before the physical end of file.

_ _DATA_ _ As for __END__, except that it also indicates the start of the
DATA filehandle that can be opened with the open, therefore
allowing you to embed script and data into the same script.
Table 4-4.
Literal Tokens in Perl
Chapter 4: Variables and Data
99
FUNDAMENTALS
Note that Perl uses a combination of special characters and names to refer to the
individual variables. To use the long (named) variables, you must include the English
module by placing
use English;
at the top of your program. By including this module, you arrange that the longer
names will be aliased to the shortened versions. Although there is no standard for
using either format, because the shortened versions are the default, you will see them
used more widely. See Web Appendix A for a listing of the variables and their English
module equivalents. The named examples are given here for reference.
Some of the variables also have equivalent methods that are supported by the IO::*
range of modules. The format of these method calls is method HANDLE EXPR (you
can also use HANDLE->method(EXPR)), where HANDLE is the filehandle you want
the change to apply to, and EXPR is the value to be supplied to the method.
_ (underscore) The underscore represents the special filehandle used to cache
information from the last successful stat, lstat, or file test operator.
$0
$PROGRAM_NAME
The name of the file containing the script currently being
executed.
$1 $xx The numbered variables $1, $2, and so on are the variables used to hold the
contents of group matches both inside and outside of regular expressions.
$_

$ARG
The $_ and $ARG variables represent the default input and pattern searching
spaces. For many functions and operations, if no specific variable is specified, the
default input space will be used. For example,
$_ = "Hello World\n";
print;
would print the “Hello World” message. The same variable is also used in regular
expression substitution and pattern matches. We’ll look at this more closely in Chapter 7.
100
Perl: The Complete Reference
Perl will automatically use the default space in the following situations even if you
do not specify it:

Unary functions, such as ord and int.

All file tests except -t, which defaults to STDIN.

Most of the functions that support lists as arguments (see Appendix A).

The pattern matching operations, m//, s///, and tr///, when used without an
=~ operator.

The default iterator variable in a for or foreach loop, if no other variable
is supplied.

The implicit operator in map and grep functions.

The default place to store an input record when reading from a filehandle.
$&
$MATCH

The string matched by the last successful pattern match.
$`
$PREMATCH
The string preceding the information matched by the last pattern match.
$’
$POSTMATCH
The string following the information matched by the last pattern match.
$+
$LAST_PARENT_MATCH
The last bracket match by the last regular expression
search pattern.
$* Set to 1 to do multiline pattern matching within a string. The default value is 0. The
use of this variable has been superseded by the /s and /m modifiers to regular expressions.
Use of this variable should be avoided.
@+
@LAST_MATCHED
Contains a list of all the offsets of the last successful submatches
from the last regular expression. Note that this contains the offset to the first character
following the match, not the location of the match itself. This is the equivalent of the
value returned by the pos function. The first index, $+[0] is offset to the end of the
entire match. Therefore, $+[1] is the location where $1 ends, $+[2], where $2 ends.
TEAMFLY























































Team-Fly
®

Chapter 4: Variables and Data
101
FUNDAMENTALS
@-
@LAST_MATCH_START
Contains a list of all the offsets to the beginning of the last
successful submatches from the last regular expression. The first index, $-[0], is offset to
the start of the entire match. Therefore, $-[1] is equal to $1, $-[2] is equal to $2, and so on.
$.
$NR
$INPUT_LINE_NUMBER
The current input line number of the last file from which

you read. This can be either the keyboard or an external file or other filehandle (such as
a network socket). Note that it’s based not on what the real lines are, but more what the
number of the last record was according to the setting of the $/ variable.
$/
$RS
$INPUT_RECORD_SEPARATOR
The current input record separator. This is
newline by default, but it can be set to any string to enable you to read in delimited
text files that use one or more special characters to separate the records. You can also
undefine the variable, which will allow you to read in an entire file, although this is
best done using local within a block:
{
local $/;
$file = <FILE>;
}
@ISA The array that contains a list of other packages to look through when a method
call on an object cannot be found within the current package. The @ISA array is used
as the list of base classes for the current package.
$|
$AUTOFLUSH
$OUTPUT_AUTOFLUSH
autoflush HANDLE EXPR
By default all output is buffered (providing the OS
supports it). This means all information to be written is stored temporarily in memory
and periodically flushed, and the value of $| is set to zero. If it is set to non-zero, the
filehandle (current, or specified) will be automatically flushed after each write operation.
It has no effect on input buffering.
$,
$OFS
$OUTPUT_FIELD_SEPARATOR

The default output separator for the print series of
functions. By default, print outputs the comma-separated fields you specify without
any delimiter. You can set this variable to commas, tabs, or any other value to insert a
different delimiter.
$\
$ORS
$OUTPUT_RECORD_SEPARATOR
The default output record separator. Ordinarily,
print outputs individual records without a standard separator, and no trailing newline
or other record separator is output. If you set this value, then the string will be appended
to the end of every print statement.
%OVERLOAD Set by the overload pragma to implement operator overloading.
$”
$LIST_SEPARATOR
This defines the separator inserted between elements of an
array when interpolated within a double-quoted string. The default is a single space.
$;
$SUBSEP
$SUBSCRIPT_SEPARATOR
The separator used when emulating multidimensional
arrays. If you refer to a hash element as
$foo{$a,$b,$c}
it really means
$foo{join($;,$a,$b,$c)}
The default value is “\034.”
$# The default number format to use when printing numbers. The value format
matches the format of numbers printed via printf and is initially set to %.ng, where n is
the number of digits to display for a floating point number as defined by your operating
system (this is the value of DBL_DIG from float.h under Unix).
The use of this variable should be avoided.

102
Perl: The Complete Reference
$%
$FORMAT_PAGE_NUMBER
format_page_number HANDLE EXPR
The page number of the current output
channel.
$=
$FORMAT_LINES_PER_PAGE
format_lines_per_page HANDLE EXPR
The number of printable lines of the current
page; the default is 60.
$-
$FORMAT_LINES_LEFT
format_lines_left HANDLE EXPR
The number of lines available to print to on the
current page.
$~
$FORMAT_NAME
format_name HANDLE EXPR
The name of the current report format in use by the
current output channel. This is set by default to the name of the filehandle.
$^
$FORMAT_TOP_NAME
format_top_name HANDLE EXPR
The name of the current top-of-page output
format for the current output channel. The default name is the filehandle with _TOP
appended.
$:
$FORMAT_LINE_BREAK_CHARACTERS

format_line_break_characters HANDLE EXPR
The set of characters after which a
string may be broken to fill continuation fields. The default is “\n-,” to allow strings to
be broken on newlines or hyphens.
$^L
$FORMAT_FORMFEED
format_formfeed HANDLE EXPR
The character to be used to send a form feed to
the output channel. This is set to “\f” by default.
$@
$EVAL_ERROR
The error message returned by the Perl interpreter when Perl has
been executed via the eval function. If empty (false), then the last eval call executed
successfully.
Chapter 4: Variables and Data
103
FUNDAMENTALS
104
Perl: The Complete Reference
$$
$PID
$PROCESS_ID
The process number of the Perl interpreter executing the current script.
$<
$UID
$REAL_USER_ID
The real ID of the user currently executing the interpreter that is
executing the script.
$>
$EUID

$EFFECTIVE_USER_ID
The effective user ID of the current process.
$(
$GID
$REAL_GROUP_ID
The real group ID of the current process. If the OS supports
multiple simultaneous group membership, this returns a space-separated list of group IDs.
$)
$EGID
$EFFECTIVE_GROUP_ID
The effective group ID of the process. If the OS supports
multiple simultaneous group membership, this returns a space-separated list of group IDs.
$!
$ERRNO
$OS_ERROR
Returns the error number or error string of the last system call
operation. This is equivalent to the errno value and can be used to print the error
number or error string when a particular system or function call has failed.
%!
%ERRNO
%OS_ERROR
Defined only when the Errno module has been imported. Allows
you to compare the current error with an error string as determined by the C #define
definitions in the system header files.
$[ The index of the first element in an array or of the first character in a substring.
The default is zero, but this can be set to any value. In general, this is useful only when
emulating awk, since functions and other constructs can emulate the same functionality.
The use of this variable should be avoided.
$]
$OLD_PERL_VERSION

The old version + patchlevel/1000 of the Perl interpreter.
This can be used to determine the version number of Perl being used, and therefore
what functions and capabilities the current interpreter supports. The $^V variable
holds a UTF-8 representation of the current Perl version.
$a The variable used by the sort function to hold the first of each pair of values being
compared. The variable is actually a reference to the real variable so that you can modify
it, but you shouldn’t—see Chapter 8 for information on usage.
@_
@ARG
Within a subroutine (or function), the @_ array contains the list of parameters
supplied to the function.
ARGV The special filehandle that iterates over command line filenames in @ARGV.
Most frequently called using the null filehandle in the angle operator <>.
$ARGV The name of the current file when reading from the default filehandle <>.
@ARGV The @ARGV array contains the list of the command line arguments
supplied to the script. Note that the first value, at index zero, is the first argument,
not the name of the script.
ARGVOUT The special filehandle used to send output to a new file when processing
the ARGV filehandle under the -i switch.
$b The variable supplied as the second value to compare when using sort, along
with the $a variable.
$^A
$ACCUMULATOR
When outputting formatted information via the reporting
system, the formline functions put the formatted results into $^A, and the write
function then outputs and empties the accumulator variable. This the current value
of the write accumulator for format lines.
$?
$CHILD_ERROR
The status returned by the last external command (via backticks

or system) or the last pipe close. This is the value returned by wait, so the true return
value is $? >> 8, and $? & 127 is the number of the signal received by the process, if
appropriate.
Chapter 4: Variables and Data
105
FUNDAMENTALS
106
Perl: The Complete Reference
$^C
$COMPILING
The value of the internal flag associated with the -c switch. This
has a true value when code is being compiled using perlcc or when being parsed with
the -MO option.
DATA The filehandle that refers to any text following either the _ _END_ _ or
_ _DATA_ _ token within the current file. The _ _DATA_ _ token automatically opens
the DATA filehandle for you.
$^D
$DEBUGGING
The current value of the internal debugging flags, as set from the -D
switch on the command line.
%ENV The list of variables as supplied by the current environment. The key is the
name of the environment variable, and the corresponding value is the variable’s value.
Setting a value in the hash changes the environment variable for child processes.
@EXPORT The list of functions and variables to be exported as normal from a
module when using the standard Exporter module.
%EXPORT_TAGS A list of object groups (in the keys) and objects (in the values) to
be exported when requesting groups of objects when importing a module.
$^E
$EXTENDED_OS_ERROR
Contains extended error information for operating

systems other than Unix. Under Unix the value equals the value of $!. We’ll look more
closely at the use of this variable when we study the use of Perl as a cross-platform
development solution.
@F The array into which the input lines fields are placed after splitting when the -a
command line argument has been given.
%FIELDS The hash used by the fields pragma to determine the current legal fields in
an object hash.
$^F
$SYSTEM_FD_MAX
The maximum system file descriptor number, after STDIN (0),
STDOUT (1) and STDERR (2)—therefore it’s usually two. System file descriptors are
duplicated across exec’d processes, although higher descriptors are not. The value of
this variable affects which filehandles are passed to new programs called through exec
(including when called as part of a fork).
$^H The status of syntax checks enabled by compiler hints, such as use strict.
@INC The list of directories that Perl should examine when importing modules via
the do, require, or use construct.
%INC Contains a list of the files that have been included via do, require, or use. The
key is the file you specified, and the value is the actual location of the imported file.
$^I The value of the inplace-edit extension (enabled via the -i switch on the command
line). True if inplace edits are currently enabled, false otherwise.
$^M The size of the emergency pool reserved for use by Perl and the die function
when Perl runs out of memory. This is the only standard method available for trapping
Perl memory overuse during execution.
$^O
$OSNAME
The operating system name, as determined via the configuration system
during compilation.
$^P
$PERLDB

The internal variable used for enabling the Perl debugger.
$^R
$LAST_REGEXP_CODE_RESULT
The value of the last evaluation in a (?{ code })
block within a regular expression. Note that if there are multiple (?{code}) blocks
within a regular expression, then this contains the result of the last piece of code that
led to a successful match within the expression.
%SIG The keys of the %SIG hash correspond to the signals available on the current
machine. The value corresponds to how the signal will be handled. You use this
mechanism to support signal handlers within Perl. We’ll look at this in more detail
when we examine interprocess communication in Chapter 10.
$^S
$EXCEPTIONS_BEING_CAUGHT
The current interpreter state. The value is
undefined if the parsing of the current module is not finished. It is true if inside an
eval block, otherwise, false.
STDERR The special filehandle for standard error.
STDIN The special filehandle for standard input.
STDOUT The special filehandle for standard output.
Chapter 4: Variables and Data
107
FUNDAMENTALS
108
Perl: The Complete Reference
$^T
$BASETIME
The time at which the script started running, defined as the number of
seconds since the epoch.
$^V
$PERL_VERSION

The current revision, version, and subversion of the currently
executing Perl interpreter. Specified as a v-string literal.
$VERSION The variable accessed to determine whether a given package matches the
acceptable version when the module is imported. For example
use Module 2.5;
would check $Module::VERSION to see whether it was equal to or greater than 2.5.
$^W
$WARNING
The current value of the warning switch (specified via the -w, -W, and
-X command line options).
$^X
$EXECUTABLE_NAME
The name of the Perl binary being executed, as determined
via the value of C’s argv[0]. This is not the same as the name of the script being
executed, which can be found in $0.
${^WARNING_BITS} The current set of warning checks enabled through the
warnings pragma.
${^WIDE_SYSTEM_CALLS} The global flag that enables all system calls made
by Perl to use the wide-character APIs native to the system. This allows Perl to
communicate with systems that are using multibyte characters sets, and therefore wide
characters within their function names.
Chapter 5
Statements and
Control Structures
109
Copyright 2001 The McGraw-Hill Companies, Inc. Click Here for Terms of Use.

×