CSET4100 – Fall 2009 Perl Introduction Scalar Data, Operators & Control Blocks Acknowledgements:...
-
date post
19-Dec-2015 -
Category
Documents
-
view
218 -
download
1
Transcript of CSET4100 – Fall 2009 Perl Introduction Scalar Data, Operators & Control Blocks Acknowledgements:...
![Page 1: CSET4100 – Fall 2009 Perl Introduction Scalar Data, Operators & Control Blocks Acknowledgements: Slides adapted from NYU Computer Science course on UNIX.](https://reader036.fdocuments.in/reader036/viewer/2022062516/56649d2d5503460f94a03ce7/html5/thumbnails/1.jpg)
CSET4100 – Fall 2009
Perl IntroductionScalar Data, Operators & Control
Blocks
Acknowledgements: Slides adapted from NYU Computer Science course on UNIX Tools
![Page 2: CSET4100 – Fall 2009 Perl Introduction Scalar Data, Operators & Control Blocks Acknowledgements: Slides adapted from NYU Computer Science course on UNIX.](https://reader036.fdocuments.in/reader036/viewer/2022062516/56649d2d5503460f94a03ce7/html5/thumbnails/2.jpg)
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
![Page 3: CSET4100 – Fall 2009 Perl Introduction Scalar Data, Operators & Control Blocks Acknowledgements: Slides adapted from NYU Computer Science course on UNIX.](https://reader036.fdocuments.in/reader036/viewer/2022062516/56649d2d5503460f94a03ce7/html5/thumbnails/3.jpg)
CSET4100: Server-Side Programming - Perl & CGI 3
Perl Textbook
![Page 4: CSET4100 – Fall 2009 Perl Introduction Scalar Data, Operators & Control Blocks Acknowledgements: Slides adapted from NYU Computer Science course on UNIX.](https://reader036.fdocuments.in/reader036/viewer/2022062516/56649d2d5503460f94a03ce7/html5/thumbnails/4.jpg)
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
![Page 5: CSET4100 – Fall 2009 Perl Introduction Scalar Data, Operators & Control Blocks Acknowledgements: Slides adapted from NYU Computer Science course on UNIX.](https://reader036.fdocuments.in/reader036/viewer/2022062516/56649d2d5503460f94a03ce7/html5/thumbnails/5.jpg)
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
![Page 6: CSET4100 – Fall 2009 Perl Introduction Scalar Data, Operators & Control Blocks Acknowledgements: Slides adapted from NYU Computer Science course on UNIX.](https://reader036.fdocuments.in/reader036/viewer/2022062516/56649d2d5503460f94a03ce7/html5/thumbnails/6.jpg)
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
![Page 7: CSET4100 – Fall 2009 Perl Introduction Scalar Data, Operators & Control Blocks Acknowledgements: Slides adapted from NYU Computer Science course on UNIX.](https://reader036.fdocuments.in/reader036/viewer/2022062516/56649d2d5503460f94a03ce7/html5/thumbnails/7.jpg)
CSET4100: Server-Side Programming - Perl & CGI 7
Data Types
• Basic types: scalar, lists, hashes• Support OO programming and user-defined
types
![Page 8: CSET4100 – Fall 2009 Perl Introduction Scalar Data, Operators & Control Blocks Acknowledgements: Slides adapted from NYU Computer Science course on UNIX.](https://reader036.fdocuments.in/reader036/viewer/2022062516/56649d2d5503460f94a03ce7/html5/thumbnails/8.jpg)
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
![Page 9: CSET4100 – Fall 2009 Perl Introduction Scalar Data, Operators & Control Blocks Acknowledgements: Slides adapted from NYU Computer Science course on UNIX.](https://reader036.fdocuments.in/reader036/viewer/2022062516/56649d2d5503460f94a03ce7/html5/thumbnails/9.jpg)
CSET4100: Server-Side Programming - Perl & CGI 9
Scalars
• Can be numbers$num = 100;$num = 223.45;$num = -1.3e38;
![Page 10: CSET4100 – Fall 2009 Perl Introduction Scalar Data, Operators & Control Blocks Acknowledgements: Slides adapted from NYU Computer Science course on UNIX.](https://reader036.fdocuments.in/reader036/viewer/2022062516/56649d2d5503460f94a03ce7/html5/thumbnails/10.jpg)
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
![Page 11: CSET4100 – Fall 2009 Perl Introduction Scalar Data, Operators & Control Blocks Acknowledgements: Slides adapted from NYU Computer Science course on UNIX.](https://reader036.fdocuments.in/reader036/viewer/2022062516/56649d2d5503460f94a03ce7/html5/thumbnails/11.jpg)
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
![Page 12: CSET4100 – Fall 2009 Perl Introduction Scalar Data, Operators & Control Blocks Acknowledgements: Slides adapted from NYU Computer Science course on UNIX.](https://reader036.fdocuments.in/reader036/viewer/2022062516/56649d2d5503460f94a03ce7/html5/thumbnails/12.jpg)
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)) { … }
![Page 13: CSET4100 – Fall 2009 Perl Introduction Scalar Data, Operators & Control Blocks Acknowledgements: Slides adapted from NYU Computer Science course on UNIX.](https://reader036.fdocuments.in/reader036/viewer/2022062516/56649d2d5503460f94a03ce7/html5/thumbnails/13.jpg)
CSET4100: Server-Side Programming - Perl & CGI 13
Operators
• Numeric: + - * / % **• String concatenation: .
$state = “New” . “York”;
#Prints “NewYork”
![Page 14: CSET4100 – Fall 2009 Perl Introduction Scalar Data, Operators & Control Blocks Acknowledgements: Slides adapted from NYU Computer Science course on UNIX.](https://reader036.fdocuments.in/reader036/viewer/2022062516/56649d2d5503460f94a03ce7/html5/thumbnails/14.jpg)
CSET4100: Server-Side Programming - Perl & CGI 14
Operators Cont.
• String repetition: xprint “bla” x 3;
#Prints blablabla
![Page 15: CSET4100 – Fall 2009 Perl Introduction Scalar Data, Operators & Control Blocks Acknowledgements: Slides adapted from NYU Computer Science course on UNIX.](https://reader036.fdocuments.in/reader036/viewer/2022062516/56649d2d5503460f94a03ce7/html5/thumbnails/15.jpg)
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”
![Page 16: CSET4100 – Fall 2009 Perl Introduction Scalar Data, Operators & Control Blocks Acknowledgements: Slides adapted from NYU Computer Science course on UNIX.](https://reader036.fdocuments.in/reader036/viewer/2022062516/56649d2d5503460f94a03ce7/html5/thumbnails/16.jpg)
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
![Page 17: CSET4100 – Fall 2009 Perl Introduction Scalar Data, Operators & Control Blocks Acknowledgements: Slides adapted from NYU Computer Science course on UNIX.](https://reader036.fdocuments.in/reader036/viewer/2022062516/56649d2d5503460f94a03ce7/html5/thumbnails/17.jpg)
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
![Page 18: CSET4100 – Fall 2009 Perl Introduction Scalar Data, Operators & Control Blocks Acknowledgements: Slides adapted from NYU Computer Science course on UNIX.](https://reader036.fdocuments.in/reader036/viewer/2022062516/56649d2d5503460f94a03ce7/html5/thumbnails/18.jpg)
Conditional Blocks & Loops
![Page 19: CSET4100 – Fall 2009 Perl Introduction Scalar Data, Operators & Control Blocks Acknowledgements: Slides adapted from NYU Computer Science course on UNIX.](https://reader036.fdocuments.in/reader036/viewer/2022062516/56649d2d5503460f94a03ce7/html5/thumbnails/19.jpg)
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”;
}
![Page 20: CSET4100 – Fall 2009 Perl Introduction Scalar Data, Operators & Control Blocks Acknowledgements: Slides adapted from NYU Computer Science course on UNIX.](https://reader036.fdocuments.in/reader036/viewer/2022062516/56649d2d5503460f94a03ce7/html5/thumbnails/20.jpg)
CSET4100: Server-Side Programming - Perl & CGI 20
Examples
$a = 3;
if ($a) {print "foo1\n";
} else {print "bar1\n"
}
#prints “foo1”
![Page 21: CSET4100 – Fall 2009 Perl Introduction Scalar Data, Operators & Control Blocks Acknowledgements: Slides adapted from NYU Computer Science course on UNIX.](https://reader036.fdocuments.in/reader036/viewer/2022062516/56649d2d5503460f94a03ce7/html5/thumbnails/21.jpg)
CSET4100: Server-Side Programming - Perl & CGI 21
Examples
$a = 0;
if ($a) {print "foo2\n";
} else {print "bar2\n"
}
#prints “bar2”
![Page 22: CSET4100 – Fall 2009 Perl Introduction Scalar Data, Operators & Control Blocks Acknowledgements: Slides adapted from NYU Computer Science course on UNIX.](https://reader036.fdocuments.in/reader036/viewer/2022062516/56649d2d5503460f94a03ce7/html5/thumbnails/22.jpg)
CSET4100: Server-Side Programming - Perl & CGI 22
Examples
$a = -1;
if ($a) {print "foo3\n";
} else {print "bar3\n"
}
#prints “foo3”
![Page 23: CSET4100 – Fall 2009 Perl Introduction Scalar Data, Operators & Control Blocks Acknowledgements: Slides adapted from NYU Computer Science course on UNIX.](https://reader036.fdocuments.in/reader036/viewer/2022062516/56649d2d5503460f94a03ce7/html5/thumbnails/23.jpg)
CSET4100: Server-Side Programming - Perl & CGI 23
Examples
$a = 3;
if ($b) {print "foo4\n";
} else {print "bar4\n"
}
#prints “bar4”
![Page 24: CSET4100 – Fall 2009 Perl Introduction Scalar Data, Operators & Control Blocks Acknowledgements: Slides adapted from NYU Computer Science course on UNIX.](https://reader036.fdocuments.in/reader036/viewer/2022062516/56649d2d5503460f94a03ce7/html5/thumbnails/24.jpg)
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;
![Page 25: CSET4100 – Fall 2009 Perl Introduction Scalar Data, Operators & Control Blocks Acknowledgements: Slides adapted from NYU Computer Science course on UNIX.](https://reader036.fdocuments.in/reader036/viewer/2022062516/56649d2d5503460f94a03ce7/html5/thumbnails/25.jpg)
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++;
}
![Page 26: CSET4100 – Fall 2009 Perl Introduction Scalar Data, Operators & Control Blocks Acknowledgements: Slides adapted from NYU Computer Science course on UNIX.](https://reader036.fdocuments.in/reader036/viewer/2022062516/56649d2d5503460f94a03ce7/html5/thumbnails/26.jpg)
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;
}
![Page 27: CSET4100 – Fall 2009 Perl Introduction Scalar Data, Operators & Control Blocks Acknowledgements: Slides adapted from NYU Computer Science course on UNIX.](https://reader036.fdocuments.in/reader036/viewer/2022062516/56649d2d5503460f94a03ce7/html5/thumbnails/27.jpg)
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; }
}
![Page 28: CSET4100 – Fall 2009 Perl Introduction Scalar Data, Operators & Control Blocks Acknowledgements: Slides adapted from NYU Computer Science course on UNIX.](https://reader036.fdocuments.in/reader036/viewer/2022062516/56649d2d5503460f94a03ce7/html5/thumbnails/28.jpg)
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; }
}
![Page 29: CSET4100 – Fall 2009 Perl Introduction Scalar Data, Operators & Control Blocks Acknowledgements: Slides adapted from NYU Computer Science course on UNIX.](https://reader036.fdocuments.in/reader036/viewer/2022062516/56649d2d5503460f94a03ce7/html5/thumbnails/29.jpg)
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
![Page 30: CSET4100 – Fall 2009 Perl Introduction Scalar Data, Operators & Control Blocks Acknowledgements: Slides adapted from NYU Computer Science course on UNIX.](https://reader036.fdocuments.in/reader036/viewer/2022062516/56649d2d5503460f94a03ce7/html5/thumbnails/30.jpg)
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”;
![Page 31: CSET4100 – Fall 2009 Perl Introduction Scalar Data, Operators & Control Blocks Acknowledgements: Slides adapted from NYU Computer Science course on UNIX.](https://reader036.fdocuments.in/reader036/viewer/2022062516/56649d2d5503460f94a03ce7/html5/thumbnails/31.jpg)
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…