View
218
Download
1
Embed Size (px)
Citation preview
CSET4100 – Fall 2009
Perl IntroductionScalar Data, Operators & Control
Blocks
Acknowledgements: Slides adapted from NYU Computer Science course on UNIX Tools
CSET4100: Server-Side Programming - Perl & CGI 2
What is Perl?
• Practical Extraction and Report Language• Scripting language created by Larry Wall in the
mid-80s• Functionality and speed somewhere between
low-level languages (like C) and high-level ones (like shell)
• Influence from awk, sed, and C Shell• Easy to write (after you learn it), but sometimes
hard to read• Widely used in CGI scripting
CSET4100: Server-Side Programming - Perl & CGI 3
Perl Textbook
CSET4100: Server-Side Programming - Perl & CGI 4
Good Ways to Learn Perl
• Textbook– Example programs
• perldoc– Online Perl documentation$ perldoc perldoc perldoc man page
$ perldoc perlintro Perl introduction
$ perldoc -f sort Perl sort function man page
$ perldoc CGI CGI module man page
CSET4100: Server-Side Programming - Perl & CGI 5
Modules
• Perl modules are libraries of reusable code with specific functionalities
• Standard modules are distributed with Perl, others can be obtained from
• Include modules in your program with use, e.g. use CGI incorporates the CGI module
CSET4100: Server-Side Programming - Perl & CGI 6
A Simple Perl Script
hello:#!/usr/bin/perl -wprint “Hello, world!\n”;
$ chmod a+x hello$ ./helloHello, world!$ perl -e ‘print “Hello, world!\n”;’
Hello, world!
turns on warnings
CSET4100: Server-Side Programming - Perl & CGI 7
Data Types
• Basic types: scalar, lists, hashes• Support OO programming and user-defined
types
CSET4100: Server-Side Programming - Perl & CGI 8
Data Types
• Type of variable determined by special leading character
$foo scalar
@foo list
%foo hash
&foo function
CSET4100: Server-Side Programming - Perl & CGI 9
Scalars
• Can be numbers$num = 100;$num = 223.45;$num = -1.3e38;
CSET4100: Server-Side Programming - Perl & CGI 10
Scalars Cont.
• Can be strings$str = ’unix tools’;$str = ’Who\’s there?’;$str = ”good evening\n”;$str = ”one\ttwo”;
• Backslash escapes and variable names are interpreted inside double quotes
CSET4100: Server-Side Programming - Perl & CGI 11
Special Scalar Variables
$0 Name of script
$_ Default variable
$$ Current PID
$? Status of last pipe or system call
$! System error message
$/ Input record separator
$. Input record number
undef Acts like 0 or empty string
CSET4100: Server-Side Programming - Perl & CGI 12
undef and defined
$f = 1;while ($n < 10) { # $n is undef at 1st iteration $f *= ++$n;}
• Use defined to check if a value is undefif (defined($val)) { … }
CSET4100: Server-Side Programming - Perl & CGI 13
Operators
• Numeric: + - * / % **• String concatenation: .
$state = “New” . “York”;
#Prints “NewYork”
CSET4100: Server-Side Programming - Perl & CGI 14
Operators Cont.
• String repetition: xprint “bla” x 3;
#Prints blablabla
CSET4100: Server-Side Programming - Perl & CGI 15
Operators Cont.
• Binary assignments:$val = 2; $val *= 3;# $val is now 6
$state .= “City”;# $state is now “NewYorkCity”
CSET4100: Server-Side Programming - Perl & CGI 16
Comparison Operators
Comparison Numeric String
Equal == eq
Not Equal != ne
Greater than > gt
Less than < lt
Less than or equal to <= le
Greater than or equal to >= ge
CSET4100: Server-Side Programming - Perl & CGI 17
Boolean “Values”
if ($ostype eq “unix”) { … }if ($val) { … }• No boolean data type• undef is false• 0 is false; Non-zero numbers are true• ‘’ and ‘0’ are false; other strings are true• The unary not (!) negates the boolean value
Conditional Blocks & Loops
CSET4100: Server-Side Programming - Perl & CGI 19
if - elsif - else
• if … elsif … else …if ( $x > 0 ) {print “x is positive\n”;
}elsif ( $x < 0 ) {print “x is negative\n”;
}else {print “x is zero\n”;
}
CSET4100: Server-Side Programming - Perl & CGI 20
Examples
$a = 3;
if ($a) {print "foo1\n";
} else {print "bar1\n"
}
#prints “foo1”
CSET4100: Server-Side Programming - Perl & CGI 21
Examples
$a = 0;
if ($a) {print "foo2\n";
} else {print "bar2\n"
}
#prints “bar2”
CSET4100: Server-Side Programming - Perl & CGI 22
Examples
$a = -1;
if ($a) {print "foo3\n";
} else {print "bar3\n"
}
#prints “foo3”
CSET4100: Server-Side Programming - Perl & CGI 23
Examples
$a = 3;
if ($b) {print "foo4\n";
} else {print "bar4\n"
}
#prints “bar4”
CSET4100: Server-Side Programming - Perl & CGI 24
unless
• Like the opposite of if
unless ($x < 0) {print “$x is non-negative\n”;
}
unlink $file unless -A $file < 100;
CSET4100: Server-Side Programming - Perl & CGI 25
while and until
while ($x < 100) {$y += $x++;
}
• until is like the opposite of whileuntil ($x >= 100) {$y += $x++;
}
CSET4100: Server-Side Programming - Perl & CGI 26
for
• for (init; test; incr) { … }
# sum of squares of 1 to 5for ($i = 1; $i <= 5; $i++) {$sum += $i*$i;
}
CSET4100: Server-Side Programming - Perl & CGI 27
next
• next skips the remaining of the current iteration (like continue in C)
# only print non-blank lineswhile (<>) {if ( $_ eq “\n”) { next; }else { print; }
}
CSET4100: Server-Side Programming - Perl & CGI 28
last
• last exits loop immediately (like break in C)
# print up to first blank linewhile (<>) {if ( $_ eq “\n”) { last; }else { print; }
}
CSET4100: Server-Side Programming - Perl & CGI 29
Logical AND/OR
• Logical AND : &&if (($x > 0) && ($x < 10)) { … }
• Logical OR : ||if ($x < 0) || ($x > 0)) { … }
• Both are short-circuit — second expression evaluated only if necessary
CSET4100: Server-Side Programming - Perl & CGI 30
Ternary Operator
• Same as the ternary operator (?:) in C• expr1 ? expr2 : expr3• Like if-then-else: If expr1 is true, expr2 is used;
otherwise expr3 is used
$weather=($temp>50)?“warm”:“cold”;
CSET4100: Server-Side Programming - Perl & CGI 31
Assignment
• Learning Perl– Review Ch. 1 & 2 – Read Ch. 3 (Lists and Arrays) & Ch. 6 (Hashes)
• Homework– Check the website…