Presentation is loading. Please wait.

Presentation is loading. Please wait.

CSET4100 – Fall 2009 Perl Introduction Scalar Data, Operators & Control Blocks Acknowledgements: Slides adapted from NYU Computer Science course on UNIX.

Similar presentations


Presentation on theme: "CSET4100 – Fall 2009 Perl Introduction Scalar Data, Operators & Control Blocks Acknowledgements: Slides adapted from NYU Computer Science course on UNIX."— Presentation transcript:

1 CSET4100 – Fall 2009 Perl Introduction Scalar Data, Operators & Control Blocks Acknowledgements: Slides adapted from NYU Computer Science course on UNIX Tools

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 2CSET4100: Server-Side Programming - Perl & CGI

3 Perl Textbook CSET4100: Server-Side Programming - Perl & CGI3

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 4CSET4100: 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 5CSET4100: Server-Side Programming - Perl & CGI

6 A Simple Perl Script hello: #!/usr/bin/perl -w print “Hello, world!\n”; $chmod a+x hello $./hello Hello, world! $perl -e ‘print “Hello, world!\n”;’ Hello, world! turns on warnings 6CSET4100: Server-Side Programming - Perl & CGI

7 Data Types Basic types: scalar, lists, hashes Support OO programming and user-defined types 7CSET4100: Server-Side Programming - Perl & CGI

8 Data Types Type of variable determined by special leading character $foo scalar @foo list %foo hash &foo function 8CSET4100: Server-Side Programming - Perl & CGI

9 Scalars Can be numbers $num = 100; $num = 223.45; $num = -1.3e38; 9CSET4100: 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 10CSET4100: 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 11CSET4100: 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 undef if (defined($val)) { … } 12CSET4100: Server-Side Programming - Perl & CGI

13 Operators Numeric: + - * / % ** String concatenation:. $state = “New”. “York”; #Prints “NewYork” 13CSET4100: Server-Side Programming - Perl & CGI

14 Operators Cont. String repetition: x print “bla” x 3; #Prints blablabla 14CSET4100: Server-Side Programming - Perl & CGI

15 Operators Cont. Binary assignments: $val = 2; $val *= 3; # $val is now 6 $state.= “City”; # $state is now “NewYorkCity” 15CSET4100: Server-Side Programming - Perl & CGI

16 Comparison Operators ComparisonNumericString Equal ==eq Not Equal !=ne Greater than >gt Less than <lt Less than or equal to <=le Greater than or equal to >=ge 16CSET4100: 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 17CSET4100: Server-Side Programming - Perl & CGI

18 Conditional Blocks & Loops

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”; } 19CSET4100: 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 & CGI20

21 Examples $a = 0; if ($a) { print "foo2\n"; } else { print "bar2\n" } #prints “bar2” CSET4100: Server-Side Programming - Perl & CGI21

22 Examples $a = -1; if ($a) { print "foo3\n"; } else { print "bar3\n" } #prints “foo3” CSET4100: Server-Side Programming - Perl & CGI22

23 Examples $a = 3; if ($b) { print "foo4\n"; } else { print "bar4\n" } #prints “bar4” CSET4100: Server-Side Programming - Perl & CGI23

24 unless Like the opposite of if unless ($x < 0) { print “$x is non-negative\n”; } unlink $file unless -A $file < 100; 24CSET4100: Server-Side Programming - Perl & CGI

25 while and until while ($x < 100) { $y += $x++; } until is like the opposite of while until ($x >= 100) { $y += $x++; } 25CSET4100: Server-Side Programming - Perl & CGI

26 for for (init; test; incr) { … } # sum of squares of 1 to 5 for ($i = 1; $i <= 5; $i++) { $sum += $i*$i; } 26CSET4100: Server-Side Programming - Perl & CGI

27 next next skips the remaining of the current iteration (like continue in C) # only print non-blank lines while (<>) { if ( $_ eq “\n”) { next; } else { print; } } 27CSET4100: Server-Side Programming - Perl & CGI

28 last last exits loop immediately (like break in C) # print up to first blank line while (<>) { if ( $_ eq “\n”) { last; } else { print; } } 28CSET4100: Server-Side Programming - Perl & CGI

29 Logical AND/OR Logical AND : && if (($x > 0) && ($x < 10)) { … } Logical OR : || if ($x 0)) { … } Both are short-circuit — second expression evaluated only if necessary 29CSET4100: 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”; 30CSET4100: 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… CSET4100: Server-Side Programming - Perl & CGI31


Download ppt "CSET4100 – Fall 2009 Perl Introduction Scalar Data, Operators & Control Blocks Acknowledgements: Slides adapted from NYU Computer Science course on UNIX."

Similar presentations


Ads by Google