Outline
1
#!/usr/bin/perl
2
# Fig. 2.1: fig02_01.pl
3
# A first program in Perl
4 5
print "Welcome to Perl!\n";
Welcome to Perl!
# print greeting
The “shebang” construct (#!) indicates The Perl comment character (#) indicates that everything the path to the Perl the interpreter onpositions Unix The escape \n following represents newline on thesequence current line # is a and comment. and Linux systems. print outputs the string “Welcome theFunction screen cursor at the beginning of the next line. to Perl!\n” to the screen.
2001 Prentice Hall, Inc. All rights reserved.
1
2
Escape Description Sequence \n \t \r
\$ \\ \" \' Fig. 2.2
2001 Prentice Hall, Inc. All rights reserved.
Newline. Position the screen cursor at the beginning of the next line. Horizontal tab. Move the screen cursor to the next tab stop. Carriage return. Position the screen cursor at the beginning of the current line; do not advance to the next line. Dollar sign. Used to insert a dollar-sign character in a string. Backslash. Used to insert a backslash character in a string. Double quote. Inserts a double-quote character in a double-quoted string. Single quote. Inserts a single-quote character in a single-quoted string. Some common escape sequences.
Outline
1
#!/usr/bin/perl
2
# Fig. 2.3: fig02_03.pl
3
# Prints a welcome statement in a variety of ways
4 5
print ( "1. Welcome to Perl!\n" );
6
print
7
print "3. Welcome ", "to ", "Perl!\n";
8
print "4. Welcome ";
9
print "to Perl!\n";
"2. Welcome to Perl!\n"
10 print "5. Welcome to 11 print "6. Welcome\n
1. 2. 3. 4. 5. 6.
Welcome Welcome Welcome Welcome Welcome Welcome to
to to to to to
Perl! Perl! Perl! Perl! Perl!
;
Although these statements are on separate lines, the string Welcome to functions Perl! islike displayed Perl!\n"; Arguments to built-in print on one line. Whitespace characters are ignored by Perl. to\n\n Perl!\n"; can be placed in parentheses. More than one argument may be passed to functioncharacters print. may be used to print multiple lines Newline of text using one line of code.
Perl!
2001 Prentice Hall, Inc. All rights reserved.
3
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
Outline
#!/usr/bin/perl # Fig. 2.4: fig02_04.pl # A simple addition program # prompt user for first number print "Please enter first number:\n"; $number1 = <STDIN>; chomp $number1; # remove "\n" from input
Prompt to tell the user to enter the
print "Please enter second number:\n"; first number. $number2 = <STDIN>; chomp $number2; # remove "\n" from input $sum = $number1 + $number2; print "The sum is $sum.\n";
Please enter first number: 45 Please enter second number: 72 The sum is 117.
The expression <STDIN> causes program Declare scalar variable $number1. Assignment operator =. Function chomp removes the newline execution to pause while the computercharacter waits for from the end of the line. the user to enter data.
The assignment statement calculates the sum of the variables $number1 and $number2 and assigns the result to variable $sum.
2001 Prentice Hall, Inc. All rights reserved.
4
5
$number1
45\n
$number1
45
2001 Prentice Hall, Inc. All rights reserved.
6
$number1
45
$number2
72
2001 Prentice Hall, Inc. All rights reserved.
7
$number1
45
$number2
72
$sum
117
2001 Prentice Hall, Inc. All rights reserved.
8
Perl expression
Addition
Arithmet Algebraic ic expression operator + x + y
Subtraction Multiplication
*
x Ð y xy
$x - $y $x * $y
Division
/
x / y
$x / $y
Modulus Exponentiation
% **
x mod y x y
$x % $y
Operation
Fig. 2.9 Arithmetic operators.
2001 Prentice Hall, Inc. All rights reserved.
$x + $y
$x ** $y
9
Operator(s)
Operation(s)
( )
Parentheses
Order of evaluation (precedence)
Evaluated first. If the parentheses are nested, the expression in the innermost pair is evaluated first. If there are several pairs of parentheses Òon the same levelÓ (i.e., not nested), they are evaluated left to right. ** Exponentiation Evaluated second. If there is more than one, the operators are evaluated from right to left. *, / or % Multiplication Evaluated third. If there is more than Division one, the operators are evaluated from Modulus left to right. + or Addition Evaluated last. If there is more than Subtraction one, the operators are evaluated from left to right. Fig. 2.10 Precedence of arithmetic operators.
2001 Prentice Hall, Inc. All rights reserved.
10
Assignment operator
Sample expression
Explanation Assigns
Assume that: $c = 3, $d = 5, $e = 4, $f = 6, $g = 12 and $h = 5. += $c += 7
$c = $c + 7 10 to $c
-= *= /= %=
$d $e $f $g
$d $e $f $g
**=
$h **= 2
-= *= /= %=
4 5 3 9
Fig. 2.12 Arithmetic assignment operators.
2001 Prentice Hall, Inc. All rights reserved.
= = = =
$d $e $f $g
* / %
4 5 3 9
$h = $h ** 2
1 to $d 20 to $e 2 to $f 3 to $g 25 to $h
11
Operat Name or ++ preincrement
Sample Explanation expression ++$c Increment $c by 1, then use the new value of $c in the expression in which $c resides. ++ postincrement $c++ Use the current value of $c in the expression in which $c resides, then increment $c by 1. -predecrement --$d Decrement $d by 1, then use the new value of $d in the expression in which $d resides. -postdecrement $d-Use the current value of $d in the expression in which $d resides, then decrement $d by 1. Fig. 2.13 Increment and decrement operators.
2001 Prentice Hall, Inc. All rights reserved.
Outline
1
#!/usr/bin/perl
2
# Fig. 2.14: fig02_14.pl
3
# Demonstrates the difference between pre- and postincrement
4 5
$c = 5;
6
$d = 5;
Print Use Print thethenew variable postincrement value$c. of variable operator $c.to use the current value of $c in the expression and then increment $c by 1.
7 8
print $c,
9
print $c++, " ";
10 print $c,
" ";
# print 5 # print 5 then postincrement
"\n";
# print 6
" ";
# print 5
11 12 print $d,
13 print ++$d, " ";
# preincrement then print 6
14 print $d,
# print 6
5 5 6 5 6 6
"\n";
Use the preincrement operator to increment $c by 1 and then use the new value of $c in the expression.
2001 Prentice Hall, Inc. All rights reserved.
12
13
Operator(s)
Associativity
Called
( )
left to right
parentheses
++ or --
none
increment and decrement
** right to left exponentiation *, / or % left to right multiplicative + or left to right additive =, +=, -=, *=, right to left assignment /=, %= or **= Fig. 2.15 Precedence and associativity of operators discussed so far.
2001 Prentice Hall, Inc. All rights reserved.