By Randal L. Schwartz, Tom Christiansen
Foreword by Larry燱all
Cover | Table of Contents | Colophon
perl: not found
http://www.perl.com/CPAN for one of the many mirrors. If you're absolutely stumped, write bookquestions@oreilly.com and say "Where can I get Perl?!?!"% echo date >somescript % echo who >>somescript % cat somescript date who % chmod +x somescript % somescript [output of date followed by who] %
#!/usr/bin/perl
#! line, you'll have a little more work to do. Check with your Perl installer about this. The examples in this book assume that you use this common mechanism.print or +) is optional, unless two tokens put together can be mistaken for another token, in which case whitespace of some kind is mandatory. (Whitespace consists of spaces, tabs, newlines, returns, or formfeeds.) There are a few constructs that require a certain kind of whitespace in a certain place, but they'll be pointed out when we get to them. You can assume that the kind and amount of whitespace between tokens is otherwise arbitrary.#!/usr/bin/perl -w
print ("Hello, world!\n");
print function. The built-in function print starts it off, and in this case has just one argument, a C-like text string. Within this string, the character combination \n stands for a newline character. The print statement is terminated by a semicolon (;). As in C, all simple statements in Perl are terminated by a semicolon.
print function, which sends its arguments to the output. After the program has completed, the Perl process exits, returning back a successful exit code to the parent shell.hello or the Gettysburg Address). Although you may think of numbers and strings as very different things, Perl uses them nearly interchangeably, so we'll describe them together.hello or the Gettysburg Address). Although you may think of numbers and strings as very different things, Perl uses them nearly interchangeably, so we'll describe them together.E notation. For example:1.25 # about 1 and a quarter
7.25e45 # 7.25 times 10 to the 45th power (a big number)
-6.5e24 # negative 6.5 times 10 to the 24th
# (a "big" negative number)
-12e-24 # negative 12 times 10 to the -24th
# (a very small negative number)
-1.2E-23 # another way to say that
12 15 -2004 3485
hello). Each character is an 8-bit value from the entire 256 character set (there's nothing special about the NUL character as in some languages).'hello' # five characters: h, e, l, l, o 'don\'t' # five characters: d, o, n, single-quote, t '' # the null string (no characters) 'silly\\me' # silly, followed by backslash, followed by me 'hello\n' # hello followed by backslash followed by n 'hello there' # hello, newline, there (11 characters total)
+ is an operator because it takes two numbers (the operands, like 5 and 6), and produces a new value (11, the result).2 + 3 # 2 plus 3, or 5 5.1 - 2.4 # 5.1 minus 2.4, or approximately 2.7 3 * 12 # 3 times 12 = 36 14 / 2 # 14 divided by 2, or 7 10.2 / 0.3 # 10.2 divided by 0.3, or approximately 34 10 / 3 # always floating point divide, so approximately 3.3333333...
2**3, which is two to the third power, or eight. (If the result can't fit into a double-precision floating-point number, such as a negative number to a noninteger exponent, or a large number to a large exponent, you'll get a fatal error.)10
%
3 is the remainder when 10 is divided by 3, which is 1. Both values are first reduced to their integer values, so 10.5
%
3.2 is computed as 10
%
3.$A is a different variable from $a. And all of the letters, digits, and underscores are significant, so:$a_very_long_variable_that_ends_in_1is different from:
$a_very_long_variable_that_ends_in_2
$xyz123 is probably not very descriptive but $line_length is.$a = 17; # give $a the value of 17 $b = $a + 3; # give $b the current value of $a plus 3 (20) $b = $b * 2; # give $b the value of $b multiplied by 2 (40)
$b variable twice: once to get its value (on the right side of the =), and once to define where to put the computed expression (on the left side of the =). This is legal, safe, and in fact, rather common. In fact, it's so common that we'll see in a minute that we can write this using a convenient shorthand.$. In shell programming, you use $ to get the value, but leave the $ off to assign a new value. In Java or C, you leave the $ off entirely. If you bounce back and forth a lot, you'll find yourself typing the wrong things occasionally. This is expected. (Our solution was to stop writing shell, awk, and C programs, but that may not work for you.)$a=3 has a value, just as $a+3 has a value. The value is the value assigned, so the value of $a=3 is 3. Although this may seem odd at first glance, using an assignment as a value is useful if you wish to assign an intermediate value in an expression to a variable, or if you simply wish to copy the same value to more than one variable. For example:$b = 4 + ($a = 3); # assign 3 to $a, then add 4 to that
# resulting in $b getting 7
$d = ($c = 5); # copy 5 into $c, and then also into $d
$d = $c = 5; # the same thing without parentheses
<STDIN> in a place where a scalar value is expected, Perl reads the next complete text line from
standard input (up to the first newline), and uses that string as the value of <STDIN>. Standard input can mean many things, but unless you do something odd, it means the terminal of the user who invoked your program (probably you). If there's nothing waiting to be read (typically the case, unless you type ahead a complete line), the Perl program will stop and wait for you to enter some characters followed by a newline (return).<STDIN> typically has a newline on the end of it. Most often, you'll want to get rid of that newline right away (there's a big difference between hello and hello\n). This is where our friend, the chomp function, comes to the rescue. A typical input sequence goes something like this:$a = <STDIN>; # get the text chomp($a); # get rid of that pesky newline
chomp($a = <STDIN>);
$a, even after it has been given a value with <STDIN>. Thus, the chomp function is working on $a. (This is true in general about the assignment operator; an assignment expression can be used wherever a variable is needed, and the actions refer to the variable on the left side of the equal sign.)<STDIN>. How do we get things out? With the print
function. This function takes the values within its parentheses and puts them out without any embellishment onto standard output. Once again, unless you've done something odd, this will be your terminal. For example:print("hello world\n"); # say hello world, followed by newline
print "hello world\n"; # same thing
print without parentheses. Whether or not to use the parentheses is mostly a matter of style and typing agility, although there are a few cases where you'll need the parentheses to remove ambiguity.print a list of values, in Section 6.3.1, but we haven't talked about lists yet, so we'll put that off for later.undef value before they are first assigned. This value looks like a zero when used as a number, or the zero-length empty string when used as a string. You will get a warning under Perl's -w switch, though, which is a good way to catch programming errors.undef when the arguments are out of range or don't make sense. If you don't do anything special, you'll get a zero or a null string without major consequences. In practice, this is hardly a problem.undef under certain circumstances is <STDIN>
. Normally, this returns the next line that was read; however, if there are no more lines to read (such as when you type CTRL-D at the terminal, or when a file has no more data), <STDIN>
returns undef as a value. In Chapter 6, we'll see how to test for this and take special action when there is no more data available to read.12.5. The circumference is 2蟺 times the radius, or about 2 times 3.141592654 times the radius.x" operator.)(1,2,3) # array of three values 1, 2, and 3
("fred",4.5) # two values, "fred" and 4.5
($a,17); # two values: the current value of $a, and 17 ($b+$c,$d+$e) # two values
() # the empty list (zero elements)
(1 .. 5) # same as (1, 2, 3, 4, 5) (1.2 .. 5.2) # same as (1.2, 2.2, 3.2, 4.2, 5.2) (2 .. 6,10,12) # same as (2,3,4,5,6,10,12) ($a .. $b) # range determined by current values of $a and $b
(1,2,3) # array of three values 1, 2, and 3
("fred",4.5) # two values, "fred" and 4.5
($a,17); # two values: the current value of $a, and 17 ($b+$c,$d+$e) # two values
() # the empty list (zero elements)
(1 .. 5) # same as (1, 2, 3, 4, 5) (1.2 .. 5.2) # same as (1.2, 2.2, 3.2, 4.2, 5.2) (2 .. 6,10,12) # same as (2,3,4,5,6,10,12) ($a .. $b) # range determined by current values of $a and $b
(1.3 .. 6.1) # same as (1.3,2.3,3.3,4.3,5.3)
@a = ("fred","barney","betty","wilma"); # ugh!
@a = qw(fred barney betty wilma); # better!
@a = qw(
fred
barney
betty
wilma
); # same thing@) rather than a dollar sign ($). For example:@fred # the array variable @fred @A_Very_Long_Array_Variable_Name @A_Very_Long_Array_Variable_Name_that_is_different
@fred is unrelated to the scalar variable $fred. Perl maintains separate namespaces for different types of things.(), the
empty list.@fred = (1,2,3); # The fred array gets a three-element literal @barney = @fred; # now that is copied to @barney
@huh = 1; # 1 is promoted to the list (1) automatically
@fred = qw(one two);
@barney = (4,5,@fred,6,7); # @barney becomes
# (4,5,"one","two",6,7)
@barney = (8,@barney); # puts 8 in front of @barney
@barney = (@barney,"last");# and a "last" at the end
# @barney is now (8,4,5,"one","two",6,7,"last")
($a,$b,$c) = (1,2,3); # give 1 to $a, 2 to $b, 3 to $c
($a,$b) = ($b,$a); # swap $a and $b
($d,@fred) = ($a,$b,$c); # give $a to $d, and ($b,$c) to @fred
($e,@fred) = @fred; # remove first element of @fred to $e
# this makes @fred = ($c) and $e = $b@fred returns the contents of the @fred array in a list context, but the length of the same array in a scalar context. These subtleties are mentioned when each operator and function is described.<STDIN>
. As described earlier, <STDIN> returns the next line of input in a scalar context. However, in a list context, it returns all remaining lines up to end of file. Each line is returned as a separate element of the list. For example:@a = <STDIN>; # read standard input in a list context
@fred = ("hello","dolly");
$y = 2;
$x = "This is $fred[1]'s place"; # "This is dolly's place"
$x = "This is $fred[$y-1]'s place"; # same thing
@fred = ("hello","dolly"); # give value to @fred for testing
$fred = "right";
# we are trying to say "this is right[1]"
$x = "this is $fred[1]"; # wrong, gives "this is dolly"
$x = "this is ${fred}[1]"; # right (protected by braces)
$x = "this is $fred"."[1]"; # right (different string)
$x = "this is $fred\[1]"; # right (backslash hides it)
@ character). In this case, the elements are interpolated in sequence with a space character between them, as in:@fred = ("a","bb","ccc",1,2,3);
$all = "Now for @fred here!";
# $all gets "Now for a bb ccc 1 2 3 here!"
@fred = ("a","bb","ccc",1,2,3);
$all = "Now for @fred[2,3] here!";
# $all gets "Now for ccc 1 here!"
$all = "Now for @fred[@fred[4,5]] here!"; # same thing
@somearray, put
srand;
rand(@somearray)
@somearray.{
first_statement;
second_statement;
third_statement;
...
last_statement;
}
if blocks for examples of the two styles:if ($ready) { $hungry++ }
if ($tired) {
$sleepy = ($hungry + 1) * 2;
}
if statement. This construct takes a control expression (evaluated for its truth) and a block. It may optionally have an else followed by a block as well. In other words, it looks like this:if (some_expression) { true_statement_1; true_statement_2; true_statement_3; } else { false_statement_1; false_statement_2; false_statement_3; }
{
first_statement;
second_statement;
third_statement;
...
last_statement;
}
if blocks for examples of the two styles:if ($ready) { $hungry++ }
if ($tired) {
$sleepy = ($hungry + 1) * 2;
}
if statement. This construct takes a control expression (evaluated for its truth) and a block. It may optionally have an else followed by a block as well. In other words, it looks like this:if (some_expression) { true_statement_1; true_statement_2; true_statement_3; } else { false_statement_1; false_statement_2; false_statement_3; }
"0" (the digit zero), then the value of the expression is false. Anything else is true automatically. Why such funny rules? Because it makes it easy to branch on an emptyish versus nonempty string, as well as a zero versus nonzero number, without having to create two versions of interpreting true and false values. Here are some examples of true and false interpretations:0 # converts to "0", so false 1-1 # computes to 0, then converts to "0", so false 1 # converts to "1", so true "" # empty string, so false "1" # not "" or "0", so true "00" # not "" or "0", so true (this is weird, watch out) "0.000" # also true for the same reason and warning undef # evaluates to "", so false
while statement:while (some_expression) { statement_1; statement_2; statement_3; }
while statement, Perl evaluates the control expression (some_expression in the example). If its value is true (using Perl's notion of truth), the body of the while statement is evaluated once. This is repeated until the control expression becomes false, at which point Perl goes on to the next statement after the while
loop. For example:print "how old are you? ";
$a = <STDIN>;
chomp($a);
while ($a > 0) {
print "At one time, you were $a years old.\n";
$a--;
}
while with
until yields the desired effect:until (some_expression) { statement_1; statement_2; statement_3; }
while and the until form, the body statements will be skipped entirely if the control expression is the termination value to begin with. For example, if a user enters an age less than zero for the program fragment above, Perl skips over the body of the loop.while/until statement you saw in the previous section tests its condition at the top of every loop, before the loop is entered. If the condition was already false to begin with, the loop won't be executed at all.for statement, which looks suspiciously like C or Java's for statement and works roughly the same way. Here it is:for ( initial_exp; test_exp; re-init_exp ) { statement_1; statement_2; statement_3; }
initial_exp;
while (test_exp) {
statement_1;
statement_2;
statement_3;
re-init_exp;
}