Beruflich Dokumente
Kultur Dokumente
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
$1$9
n/a
$&
$MATch
$`
$PREMATch
$POSTMATch
$+
$LAST_PAREN_MATch
$*
$MULTILINE_MATchING
$.
$INPUT_LINE_NUMBER
$/
$INPUT_RECORD_SEPARATOR
$|
$AUTO_FLUSH
$,
$OUTPUT_FIELD_SEPARATOR
$\
$INPUT_RECORD_SEPARATOR
$"
$LIST_SEPARATOR
$;
$SUBSCRIPT_SEPARATOR
emulation
$#
$FORMAT
$%
$FORMAT_PAGE_NUMBER
$=
$FORMAT_LINES_PER_PAGE
$FORMAT_LINES_LEFT
$~
$FORMAT_NAME
$^
$FORMAT_TOP_NAME
$:
$FORMAT_LINE_BREAK_chARACTERS
}
unless(condition1){#oppositeof"if"statement.
...dothisifconditionisfalse;
}
The condition in these blocks of code is anything from a Perl variable to an expression that
returns either a true or false value. A true value is a non-zero value or a non-empty string.
Code blocks can be declared within code blocks to create levels of code blocks. Variables
declared in one code block are usually global to the rest of the program. To keep the scope of the
variable limited to the code block in which it is declared, use the my$variableName syntax. If
you declare with local$variableName syntax, the $variableName will be available to all
lower levels but not outside the code block.
Figure 2.1 illustrates how the scoping rules work in Perl. The main block declares two variables,
$x and $y. There are two blocks of code between curly braces, block A and block B. The variable
$x is not available to either of these blocks, but $y will be available.
Figure 2.1 : Scoping rules in Perl
Because block A is declared in the main block, the code in it will be able to access $y but not $x
because $x is declared as "my". The variable $f will not be available to other blocks of code even
if they are declared within block A. The variable $g is not declared as "local" or "my", so it's
not visible to the main module nor to block B.
The code in block B declares two variables, $k and $m. The variable $k can be assigned the value
of $g, provided that the code in block A is called before the code in block B. If the code in block
B is called before the code in block A, the variable $g will not be declared, and a value of
'undef' will be assigned to $k. Also, $m cannot use the value of $f because $f is declared in
block A as a "my" variable. The values of $y and $g are available to code in block B.
Finally, another code block (call it C) could be assigned in block B. Block C is not shown in the
figure. All variables in this new block C that are declared as neither "my" nor "local" would be
available to blocks A and B and the main program. Code in block C would not be able to access
variables $f, $k, and $m because they are declared as "my". The variable $g would not be
available to code in block B or C because it is local to block A.
Keep in mind that variables in code blocks are also declared at the first time they are assigned a
value. This creation includes arrays and strings. Variables are then evaluated by the parser when
they appear in code, and even in strings. There are times when you do not want the variable to be
evaluated. This is the time when you should be aware of quoting rules in Perl.
Quoting Rules
Three different types of quotes can be used in Perl. Double quotes (") are used to enclose strings.
Any scalars in double-quoted strings are evaluated by Perl. To force Perl not to evaluate anything
in a quote, you'll have to use single quotes ('). Anything that looks like code and is not quoted is
interpreted as code by the Perl interpreter, which attempts to evaluate the code as an expression
or a set of executable code statements. Finally, to run some values in a shell program and get its
return value back, use the back quote (`) symbol. See the Perl script in Listing 2.1 for an
example.
Let's go over the code shown in Listing 2.1. First of all, note that the actual listing did not have
line numbers. The line numbers in this and subsequent scripts are used to identify specific lines
of code.
Line 1 is the mandatory first line of the Perl script. Change the path shown in Listing 2.1 to
where your Perl interpreter is located if the script does not run. Be sure to make a similar change
to the rest of the source listings in this book.
Line 2 assigns a string value to the $folks variable. Note that you did not have to declare the
variable $folks because it was created when used for the first time.
Line 3 prints the value of $folks in between double quotes. The $ sign in $folks has to be
escaped with a backslash to prevent Perl from evaluating the value of $folks instead of printing
the following line:
$folks=100
In line 4, Perl does not evaluate anything between the single quotes. Therefore, the entire
contents of the line are left untouched and printed here:
\$folks=$folks\n
Perl has several special characters to format text data for you. Line 5 prints multiple blank lines
with the \n character and beeps at the terminal. Notice how the words SOMEBLANK are printed in
lowercase letters. This is because they are encased between the \L and \E special characters,
which force all characters to be lowercase. Some of these special characters are listed in Table
2.2.
Table 2.2. Special characters in Perl.
Character
Meaning
\\
Backslash.
\0ooo
\a
Beep.
\b
Backspace.
\c
\cC
\l
\L \E
\n
\r
\t
Tab.
\u
\U \E
\x##
In line 6, the script uses the back quotes (`) to execute a command and return the results in the
$date variable. The string in between the two back quotes is what you would type at the
command line, with one exception: if you use Perl variables in the command line for the back
quotes, Perl evaluates these variables before passing them off to the shell for execution. For
example, line 6 could be rewritten as this:
$parm="+%D";
$date=`$date$parm`;
The returned value in $date is printed out in line 7. Note that there is an extra carriage return in
the text for data. To remove it, use the chop command as shown in line 8.
Then in line 9 the $date output is shown to print correctly. Note how the period (.) is used to
concatenate three strings together for the output.
It's easy to construct strings in Perl with the period (.) operator. Given two strings, $first and
$last, you can construct the string $fullname like this to get "JimSmith":
$first="Jim";
$last="Smith";
$fullname=$first."".$last;
Numbers in Perl are stored as floating-point numbers; even variables used as integers are really
stored as floating point numbers. There are a set of operations you can do with numbers. These
operations are listed in Table 2.3. The table also lists Boolean operators.
Table 2.3. Numeric operations with Perl.
Operation
Description
$r=$x+$y
$r=$x$y
$r=$x*$y
$r=$x/$y
$r=$x%$y
$r=$x**$y
$r=$x<<$n
$r=$x>>$n
$r=++$x
$r=$x++
$r+=$x;
$r=$x
$r=$x
$r=$x;
$r/=$x;
$r*=$x;
$r=$x<=>$y
$r
$r=$x||$y
$r
$r=$x&&$y
$r
$r=!$x
$r
You can compare values of variables to check results of operations. Table 2.4 lists the
comparison operators for numbers and strings.
Table 2.4. Comparison operations with Perl.
Operation
Description
$x==$y
True
if $x is equal to $y
$x!=$y
True
if $x is not equal to $y
$x<$y
True
if $x is less than $y
$x<=$y
True
$x>$y
True
if $x is greater than $y
$x>=$y
True
$xeq$y
True
$xne$y
True
$xlt$y
True
$xle$y
True
$xgt$y
True
$xge$y
True
$xx$y
$x.$y
$xcmp$y
$w?$x:$y
Arrays are referred to with the @ symbol. Individual items in an array are derived with a $ and the
subscript. Therefore, the first item in an array @count would be $count[0], the second item
would be $count[1], and so on. See Listing 2.2 for usage of arrays.
6@parts=('computer','rat',"kbd");
7
8$a=1;$b=2;$c='3';
9@count=($a,$b,$c);
10
11@empty=();
12
13@spare=@parts;
14
15print'@amounts=';
16print"@amounts\n";
17
18print'@parts=';
19print"@parts\n";
20
21print'@count=';
22print"@count\n";
23
24print'@empty=';
25print"@empty\n";
26