Special Variables

Beginning | Previous | Next

Filehandle Special Variables

There are pre-existing special variables that can be associated with filehandles. The one most used is $| (dollar sign and pipe symbol) or $OUTPUT_AUTOFLUSH. This variable is used to control forcing a flush on a filehandle, be it STDOUT, a disk file, pipe or socket. For example, to perform non-buffered I/O to the screen, we would do the following: $old_handle = select (STDOUT); # "select" STDOUT and save # previously selected handle $| = 1; # perform flush after each write to STDOUT select ($old_handle); # restore previously selected handle

Global Special Variables

There are some other special (scalar) global variables that come into existence as soon as your script does. To use the more descriptive names, place this in your script: use English;

A few examples of some of the variables I use the most are listed below:

The default input and pattern-searching space. The following pairs are equivalent:

while (<>) {...} # only equivalent in while! while ($_ = <>) {...} /^Subject:/ $_ =~ /^Subject:/ tr/a-z/A-Z/ $_ =~ tr/a-z/A-Z/ chop chop($_)

(Mnemonic: underline is understood in certain operations.)

If set to nonzero, forces a flush after every write or print on the currently selected output channel. Default is 0. Note that STDOUT will typically be line buffered if output is to the terminal and block buffered otherwise. Setting this variable is useful primarily when you are outputting to a pipe, such as when you are running a Perl script under rsh and want to see the output as it's happening. (Mnemonic: when you want your pipes to be piping hot.)
If used in a numeric context, yields the current value of errno, with all the usual caveats. (This means that you shouldn't depend on the value of "$!" to be anything in particular unless you've gotten a specific error return indicating a system error.) If used in a string context, yields the corresponding system error string. You can assign to "$!" in order to set errno if, for instance, you want "$!" to return the string for error n, or you want to set the exit value for the die() operator. (Mnemonic: What just went bang?)
$PID $$
The process number of the Perl running this script. (Mnemonic: same as shells.)

Contains the name of the file containing the Perl script being executed. Assigning to "$0" modifies the argument area that the ps(1) program sees. This is more useful as a way of indicating the current program state than it is for hiding the program you're running. (Mnemonic: same as sh and ksh.)

The time at which the script began running, in seconds since the epoch (beginning of 1970). The values returned by the -M, -A and -C filetests are based on this value.

To calulate the elapsed time anywhere in your script, you can use the following code snippet: # If you want to convert times into readable # formats, require ctime.pl. require 'ctime.pl' or dir Can't open ctime.pl: $!\n; # stuff happens here...like your code $elapsed_time = time - $^T; print "Elapsed time = $elapsed_time sec\n";
The array @ARGV contains the command line arguments intended for the script. Note that $#ARGV is the generally number of arguments minus one, since $ARGV[0] is the first argument, NOT the command name. See "$0" for the command name.

The array @INC contains the list of places to look for Perl scripts to be evaluated by the do EXPR , require , or use constructs. It initially consists of the arguments to any -I command line switches, followed by the default Perl library, probably "/usr/local/lib/perl", followed by ".", to represent the current directory.

The hash %INC contains entries for each filename that has been included via do or require . The key is the filename you specified, and the value is the location of the file actually found. The require command uses this array to determine whether a given file has already been included.

The hash %ENV contains your current environment. Setting a value in ENV changes the environment for child processes.

Beginning | Previous | Next
Last Modified: $Date: 1997/09/18 08:42:15 $