Skip to main content.

Web Based Programming Tutorials

Homepage | Forum - Join the forum to discuss anything related to programming! | Programming Resources

Perl 5 By Example

Chapter 12 -- Using Special Variables

Chapter 12

Using Special Variables


CONTENTS

Perl uses quite a few special variables to control various behaviors of functions. You can use special variables to hold the results of searches, the values of environment variables, and flags to control debugging. In short, every aspect of Perl programming uses special variables.

What Are the Special Variables?

Table 12.1 shows a list of the special variables you can use in your programs. The order of this list is identical to the list in the file PERLVAR.htm, which comes with your Perl distribution. This table lets you quickly find any special variable you may come across in examples or someone else's code.

Table 12.1  Perl's Special Variables

Variable Name
Description
$_
The default parameter for a lot of functions.
$.
Holds the current record or line number of the file handle that was last read. It is read-only and will be reset to 0 when the file handle is closed.
$/
Holds the input record separator. The record separator is usually the newline character. However, if $/ is set to an empty string, two or more newlines in the input file will be treated as one.
$,
The output separator for the print() function. Nor-mally, this variable is an empty string. However, setting $, to a newline might be useful if you need to print each element in the parameter list on a separate line.
$\
Added as an invisible last element to the parameters passed to the print() function. Normally, an empty string, but if you want to add a newline or some other suffix to everything that is printed, you can assign the suffix to $\.
$#
The default format for printed numbers. Normally, it's set to %.20g, but you can use the format specifiers covered in the section "Example: Printing Revisited" in Chapter 9to specify your own default format.
$%
Holds the current page number for the default file handle. If you use select() to change the default file handle, $% will change to reflect the page number of the newly selected file handle.
$=
Holds the current page length for the default file handle. Changing the default file handle will change $= to reflect the page length of the new file handle.
$-
Holds the number of lines left to print for the default file handle. Changing the default file handle will change $- to reflect the number of lines left to print for the new file handle.
$~
Holds the name of the default line format for the default file handle. Normally, it is equal to the file handle's name.
$^
Holds the name of the default heading format for the default file handle. Normally, it is equal to the file handle's name with _TOP appended to it.
$|
If nonzero, will flush the output buffer after every write() or print() function. Normally, it is set to 0.
$$
This UNIX-based variable holds the process number of the process running the Perl interpreter.
$?
Holds the status of the last pipe close, back-quote string, or system() function. You can find more information about the $? Variable in Chapter 13, "Handling Errors and Signals."
$&
Holds the string that was matched by the last successful pattern match.
$`
Holds the string that preceded whatever was matched by the last successful pattern match.
Holds the string that followed whatever was matched by the last successful pattern match.
$+
Holds the string matched by the last bracket in the last successful pattern match. For example, the statement /Fieldname: (.*)|Fldname: (.*)/ && ($fName = $+); will find the name of a field even if you don't know which of the two possible spellings will be used.
$*
Changes the interpretation of the ^ and $ pattern anchors. Setting $* to 1 is the same as using the /m option with the regular expression matching and substitution operators. Normally, $* is equal to 0.
$0
Holds the name of the file containing the Perl script being executed.
$<number>
This group of variables ($1, $2, $3, and so on) holds the regular expression pattern memory. Each set of parentheses in a pattern stores the string that match the components surrounded by the parentheses into one of the $<number> variables.
$[
Holds the base array index. Normally, it's set to 0. Most Perl authors recommend against changing it without a very good reason.
$]
Holds a string that identifies which version of Perl you are using. When used in a numeric context, it will be equal to the version number plus the patch level divided by 1000.
$"
This is the separator used between list elements when an array variable is interpolated into a double-quoted string. Normally, its value is a space character.
$;
Holds the subscript separator for multidimensional array emulation. Its use is beyond the scope of this book.
$!
When used in a numeric context, holds the current value of errno. If used in a string context, will hold the error string associated with errno. For more information about errno, see Chapter 13, "Handling Errors and Signals."
$@
Holds the syntax error message, if any, from the last eval() function call. For more information about errno, see Chapter 13, "Handling Errors and Signals."
$<
This UNIX-based variable holds the read uid of the current process.
$>
This UNIX-based variable holds the effective uid of the current process.
$)
This UNIX-based variable holds the read gid of the current process. If the process belongs to multiple groups, then $) will hold a string consisting of the group names separated by spaces.
$:
Holds a string that consists of the characters that can be used to end a word when word-wrapping is performed by the ^ report formatting character. Normally, the string consists of the space, newline, and dash characters.
$^D
Holds the current value of the debugging flags. For more information, see Chapter 16, "Debugging Perl."
$^F
Holds the value of the maximum system file description. Normally, it's set to 2. The use of this variable is beyond the scope of this book.
$^I
Holds the file extension used to create a backup file for the in-place editing specified by the -i command line option. For example, it could be equal to ".bak."
$^L
Holds the string used to eject a page for report printing. Chapter 11, "Creating Reports," shows how to use this variable to create simple footers.
$^P
This variable is an internal flag that the debugger clears so it will not debug itself.
$^T
Holds the time, in seconds, at which the script begins running.
$^W
Holds the current value of the -w command line option.
$^X
Holds the full pathname of the Perl interpreter being used to run the current script.
$ARGV
Holds the name of the current file being read when using the diamond operator (<>).
@ARGV
This array variable holds a list of the command line arguments. You can use $#ARGV to determine the number of arguments minus one.
@F
This array variable holds the list returned from autosplit mode. Autosplit mode is associated with the -a command line option.
@Inc
This array variable holds a list of directories where Perl can look for scripts to execute. The list is mainly used by the require statement. You can find more information about require statements in Chapter 15, "Perl Modules."
%Inc
This hash variable has entries for each filename included by do or require statements. The key of the hash entries are the filenames, and the values are the paths where the files were found.
%ENV
This hash variable contains entries for your current environment variables. Changing or adding an entry affects only the current process or a child process, never the parent process. See the section "Example: Using the %ENV Variable" later in this chapter.
%SIG
This hash variable contains entries for signal handlers. For more information about signal handlers, see Chapter 13, "Handling Errors and Signals."
_
This file handle (the underscore) can be used when testing files. If used, the information about the last file tested will be used to evaluate the new test.
DATA
This file handle refers to any data following __END__.
STDERR
This file handle is used to send output to the standard error file. Normally, this is connected to the display, but it can be redirected if needed.
STDIN
This file handle is used to read input from the standard input file. Normally, this is connected to the keyboard, but it can be changed.
STDOUT
This file handle is used to send output to the standard output file. Normally, this is the display, but it can be changed.

Table 12.2 puts the variables into different categories so you can see how they relate to one another. This organization is better than Table 12.1 when you are creating your own programs. Some of the categories covered in Table 12.2 have their own chapters. The subheadings in the table point out which chapter you can look at for more information.

Table 12.2  Perl's Special Variables

Variable Name
Description
Variables That Affect Arrays
$"
The separator used between list elements when an array variable is interpolated into a double-quoted string. Normally, its value is a space character.
$[
Holds the base array index. Normally, set to 0. Most Perl authors recommend against changing it without a very good reason.
$;
Holds the subscript separator for multidimensional array emulation. Its use is beyond the scope of this book. For a more in-depth look at Perl programming, see Que's Special Edition Using Perl for Web Programming.
Variables Used with Files (See Chapter 9 "Using Files")
$.
This variable holds the current record or line number of the file handle last read. It is read-only and will be reset to 0 when the file handle is closed.
$/
This variable holds the input record separator. The record separator is usually the newline character. However, if $/ is set to an empty string, two or more newlines in the input file will be treated as one.
$|
This variable, if nonzero, will flush the output buffer after every write() or print() function. Normally, it is set to 0.
$^F
This variable holds the value of the maximum system file description. Normally, it's set to 2. The use of this variable is beyond the scope of this book.
$ARGV
This variable holds the name of the current file being read when using the diamond operator (<>).
_
This file handle (the underscore) can be used when testing files. If used, the information about the last file tested will be used to evaluate the latest test.
DATA
This file handle refers to any data following __END__.
STDERR
This file handle is used to send output to the standard error file. Normally, this is connected to the display, but it can be redirected if needed.
STDIN
This file handle is used to read input from the standard input file. Normally, this is connected to the keyboard, but it can be changed.
STDOUT
This file handle is used to send output to the standard output file. Normally, this is the display, but it can be changed.
Variables Used with Patterns (See Chapter 10, "Regular Expressions")
$&
This variable holds the string that was matched by the last successful pattern match.
$`
This variable holds the string that preceded whatever was matched by the last successful pattern match.
This variable holds the string that followed whatever was matched by the last successful pattern match.
$+
This variable holds the string matched by the last bracket in the last successful pattern match. For example, the statement /Fieldname: (.*)|Fldname: (.*)/ && ($fName = $+); will find the name of a field even if you don't know which of the two possible spellings will be used.
$*
This variable changes the interpretation of the ^ and $ pattern anchors. Setting $* to 1 is the same as using the /m option with the regular expression matching and substitution operators. Normally, $* is equal to 0.
$<number>
This group of variables ($1, $2, $3, and so on) holds the regular expression pattern memory. Each set of parentheses in a pattern stores the string that matches the components surrounded by the parentheses into one of the $<number> variables.
Variables Used with Printing
$,
This variable is the output separator for the print() function. Normally, this variable is an empty string. However, setting $, to a newline might be useful if you need to print each element in the parameter list on a separate line.
$\
The variable is added as an invisible last element to the parameter list passed to the print() function. Normally, it's an empty string, but if you want to add a newline or some other suffix to everything that is printed, you can assign the suffix to $\.
$#
This variable is the default format for printed numbers. Normally, it's set to %.20g, but you can use the format specifiers covered in by the section "Example: Printing Revisited" in Chapter 9to specify your own default format.
Variables Used with Processes (See Chapter 13, "Handling Errors and Signals")
$$
This UNIX-based variable holds the process number of the process running the Perl interpreter.
$?
This variable holds the status of the last pipe close, back-quote string, or system() function. More information about the $? variable can be found in Chapter 13, "Handling Errors and Signals."
$0
This variable holds the name of the file containing the Perl script being executed.
$]
This variable holds a string that identifies which version of Perl you are using. When used in a numeric context, it will be equal to the version number plus the patch level divided by 1000.
$!
This variable, when used in a numeric context, holds the current value of errno. If used in a string context, it will hold the error string associated with errno. For more information about errno, see Chapter 13, "Handling Errors and Signals."
$@
This variable holds the syntax error message, if any, from the last eval() function call. For more information about errno, see Chapter 13, "Handling Errors and Signals."
$<
This UNIX-based variable holds the read uid of the current process.
$>
This UNIX-based variable holds the effective uid of the current process.
$)
This UNIX-based variable holds the read gid of the current process. If the process belongs to multiple groups, then $) will hold a string consisting of the group names separated by spaces.
$^T
This variable holds the time, in seconds, at which the script begins running.
$^X
This variable holds the full pathname of the Perl interpreter being used to run the current script.
%ENV
This hash variable contains entries for your current environment variables. Changing or adding an entry will affect only the current process or a child process, never the parent process. See the section "Example: Using the %ENV Variable" later in this chapter.
%SIG
This hash variable contains entries for signal handlers. For more information about signal handlers, see Chapter 13, "Handling Errors and Signals."
Variables Used with Reports (see Chapter 11, "Creating Reports")
$%
This variable holds the current page number for the default file handle. If you use select() to change the default file handle, $% will change to reflect the page number of the newly selected file handle.
$=
This variable holds the current page length for the default file handle. Changing the default file handle will change $= to reflect the page length of the new file handle.
$-
This variable holds the number of lines left to print for the default file handle. Changing the default file handle will change $- to reflect the number of lines left to print for the new file handle.
$~
This variable holds the name of the default line format for the default file handle. Normally, it is equal to the file handle's name.
$^
This variable holds the name of the default heading format for the default file handle. Normally, it is equal to the file handle's name with _TOP appended to it.
$:
This variable holds a string that consists of the characters that can be used to end a word when word-wrapping is performed by the ^ report formatting character. Normally, the string consists of the space, newline, and dash characters.
$^L
This variable holds the string used to eject a page for report printing. Chapter 11, "Creating Reports," shows how to use this variable to create simple footers.
Miscellaneous Variables
$_
This variable is used as the default parameter for a lot of functions.
$^D
This variable holds the current value of the debugging flags. For more information, see Chapter 16, "Debugging Perl."
$^I
This variable holds the file extension used to create a backup file for the in-place editing specified by the -i command line option. For example, it could be equal to ".bak."
$^P
This variable is an internal flag that the debugger clears so that it will not debug itself.
$^W
This variable holds the current value of the -w command line option.
@ARGV
This array variable holds a list of the command line arguments. You can use $#ARGV to determine the number of arguments minus one.
@F
This array variable holds the list returned from autosplit mode. Autosplit mode is associated with the -a command line option.
@Inc
This array variable holds a list of directories where Perl can look for scripts to execute. The list is used mainly by the require statement. You can find more information about require statements in Chapter 15, "Perl Modules."
%Inc
This hash variable has entries for each filename included by do or require statements. The key of the hash entries are the filenames and the values are the paths where the files were found.

Most of these variables are discussed in other chapters of the book, and some of the variables are simple enough to use that you don't need to see examples by this time. However, the DATA file handle and the %ENV associated array deserve some additional mention. They are discussed in the following sections.

Example: Using the DATA File Handle

As you no doubt realize by now, Perl has some really odd features, and the DATA file handle is one of them. This file handle lets you store read-only data in the same file as your Perl script, which might come in handy if you need to send both code and data to someone via e-mail.

When using the DATA file handle, you don't need to open or close the file handle-just start reading from the file handle using the diamond operator. The following simple example shows you how to use the DATA file handle.

Read all the lines that follow the line containing __END__.
Loop through the
@lines array, printing each element.
Everything above the
__END__ line is code; everything below is data.

@lines = <DATA>;



foreach (@lines) {

    print("$_");



}



__END__

Line one

Line two

Line three

This program displays the following:


Line one

Line two

Line three

Example: Using the %ENV Variable

Environment variables are used by the operating system to store bits of information that are needed to run the computer. They are called environment variables because you rarely need to use them and because they simply remain in the background-just another part of the overall computing environment of your system. When your Perl process is started, it is given a copy of the environment variables to use as needed.

You can change the environment variables, but the changes will not persist after the process running Perl is ended. The changes will, however, affect the current process and any child processes that are started.

You can print out the environment variables by using these lines of code:


foreach $key (keys(%ENV)) {

    printf("%-10.10s: $ENV{$key}\n", $key);

}

On my Windows 95 machine, this program displays the following:


WINBOOTDIR: C:\WINDOWS

TMP       : C:\WINDOWS\TEMP

PROMPT    : $p$g

CLASSPATH : .\;e:\jdk\classes;

TEMP      : C:\WINDOWS\TEMP

COMSPEC   : C:\WINDOWS\COMMAND.COM

CMDLINE   : perl -w 12lst01.pl

BLASTER   : A220 I10 D3 H7 P330 T6

WINDIR    : C:\WINDOWS

PATH      : C:\WINDOWS;C:\WINDOWS\COMMAND;C:\PERL5\BIN;

TZ        : GMT-05:00

Only a few of these variables are interesting. The TMP and TEMP variables let you know where temporary files should be placed. The PATH variable lets the system know where to look for executable programs. It will search each directory in the list until the needed file is found. The TZ variable lets you know which time zone the computer is running in.

The most useful variable is probably the PATH statement. By changing it, you can force the system to search the directories you specify. This might be useful if you suspect that another program of the same name resides in another directory. By placing the current directory at the beginning of the PATH variable, it will be searched first and you'll always get the executable you want. For example:


$ENV{"PATH"} = ".;" . $ENV{"PATH"};

A single period is used to refer to the current directory, and a semicolon is used to delimit the directories in the PATH variable. So this statement forces the operating system to look in the current directory before searching the rest of the directories in PATH.

Environment variables can be useful if you want a quick way to pass information between a parent and a child process. The parent can set the variables, and the child can read it.

Summary

This chapter gathered into one location all the special variables used by Perl. Most of the variables have already been discussed in previous chapters, and a few will be discussed in later chapters.

Table 12.1 was organized to follow the PERLVAR.htm document that comes in the Perl distribution, so if you aren't familiar with a variable used in someone else's code, that's the place to look. The variables are basically ordered alphabetically.

Table 12.2 was organized according to functionality. Some variables are used with files, some with arrays, and so forth.

You saw an example of how to use the DATA file handle to read information from the same file that holds the Perl script.

The %ENV variable was also discussed. This hash is used to hold the environmental variables used mostly by the operating system.

In the next chapter, "Handling Errors and Signals," you learn about how to handle error conditions, use the eval() function, and other things dealing with exceptions that can happen while your program runs.

Review Questions

Answers to Review Questions are in Appendix A.

  1. What is the $/ variable used for?
  2. What file handle is used to avoid a second system call when doing two or more file tests?
  3. What will the following program display?

    $_ = "The big red shoe";
    m/[rs].*\b/;
    print("$`\n");
  4. What variable holds the value of the last match string?
  5. What will the following program display?

    @array = (1..5);
    $" = "+";
    print("@array\n");
  6. What does the following program display?
    @array = ('A'..'E');

    foreach (@array) {
    print();
    }

    $\ = "\n";
    foreach (@array) {
    print();
    }

Review Exercises

  1. Write a program that changes the array element separator used in interpolation of arrays inside double-quoted strings to be a comma instead of a space.
  2. Write a program that displays which version of the Perl interpreter you are running.
  3. Create a file in your temporary directory. (Hint: use the %ENV special variable.)
  4. Write a program that uses the $\ to end each printed element with an ":END" string.
  5. Write a program that prints the last record in a file. The records should be variable-length, but each record starts with the string "START:". (Hint: look at the $/ variable.)