The syntax for a scalar variable is $variable_name.
A variable name is set up and addressed in
the same way as Bourne shell variables. To assign values to a scalar, you use statements like
these:
$name="Kamran";
$number=100;
$phone_Number='5551232';
A variable in Perl is evaluated at runtime to derive a value that is one of the following: a string, a
number, or a pointer to scalar. (To see the use of pointers and references, refer to Chapter 3,
"References.")
To print out the value of a variable, you use a print statement. Therefore, to print the value of
$name, you would make the following call:
print$name;
The value of $name is printed to the screen. Perl scripts "expect" input from a standard input (the
keyboard) and to write to the standard output. Of course, you can also use the print statement to
print the values of special variables that are built into Perl.
Special Variables
Table 2.1 lists the special variables in Perl. The first column contains the variable, and the second
contains a verbose name that you can use to make the code readable. The third column in the
table describes the contents of each variable.
You can use the verbose names (in column 2) by including the following line in the beginning of
your code:
useEnglish;
This statement will let you use the English.pm module in your code. (I cover the use of modules
in Chapter 4, "Introduction to Perl Modules.") Not all Perl variables have an equivalent name in
the English.pm module. The entry "n/a" in the second column indicates that there is not an
English name for the variable.
Table 2.1. Special variables in Perl.
Variable
English Name
Description
$_
$ARG
The default input and
output pattern searching
space
$1$9
n/a
The subpattern from the
last set of parentheses in a
pattern match
$&
$MATch
The last pattern matched
(RO)
$`
$PREMATch
The string preceding a
pattern match (RO)
$POSTMATch
The string following a
pattern match (RO)
$+
$LAST_PAREN_MATch
The last bracket matched
in a pattern (RO)
$*
$MULTILINE_MATchING
Set to 1 to enable multiline matching; set to 0 by
default
$.
$INPUT_LINE_NUMBER
The current input line
number; reset on close()
call only
$/
$INPUT_RECORD_SEPARATOR
The newline by default
$|
$AUTO_FLUSH
If set to 1, forces a flush
on every write or print;
0 by default
$,
$OUTPUT_FIELD_SEPARATOR
Specifies what is printed
between fields
$\
$INPUT_RECORD_SEPARATOR
The output record
separator for the print
operator
$"
$LIST_SEPARATOR
The separator for elements
within a list
$;
$SUBSCRIPT_SEPARATOR
The character for
multidimensional array
emulation
$#
$FORMAT
Output format for printed
numbers
$%
$FORMAT_PAGE_NUMBER
The current page number
$=
$FORMAT_LINES_PER_PAGE
The number of lines per
page
$FORMAT_LINES_LEFT
The number of lines still
left to draw on the page
$~
$FORMAT_NAME
The name of the current
format being used
$^
$FORMAT_TOP_NAME
The name of the current
top-of-page format
$:
$FORMAT_LINE_BREAK_chARACTERS
The set of characters after
which a string can be
broken up to fill with
continuation characters