1 / 31

CSET4100 – Fall 2009

CSET4100 – Fall 2009. Perl Introduction Scalar Data, Operators & Control Blocks. Acknowledgements: Slides adapted from NYU Computer Science course on UNIX Tools. What is Perl?. Practical Extraction and Report Language Scripting language created by Larry Wall in the mid-80s

keena
Télécharger la présentation

CSET4100 – Fall 2009

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

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

  3. Perl Textbook CSET4100: Server-Side Programming - Perl & CGI

  4. Good Ways to Learn Perl CSET4100: Server-Side Programming - Perl & CGI • Textbook • Example programs • perldoc • Online Perl documentation • perldocperldocperldoc man page • perldocperlintro Perl introduction • perldoc -f sort Perl sort function man page • perldoc CGI CGI module man page

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

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

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

  8. Data Types CSET4100: Server-Side Programming - Perl & CGI Type of variable determined by special leading character

  9. Scalars CSET4100: Server-Side Programming - Perl & CGI • Can be numbers $num = 100; $num = 223.45; $num = -1.3e38;

  10. Scalars Cont. CSET4100: Server-Side Programming - Perl & CGI • 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

  11. Special Scalar Variables CSET4100: Server-Side Programming - Perl & CGI

  12. undef and defined CSET4100: Server-Side Programming - Perl & CGI $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)) { … }

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

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

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

  16. Comparison Operators CSET4100: Server-Side Programming - Perl & CGI

  17. Boolean “Values” CSET4100: Server-Side Programming - Perl & CGI if ($ostypeeq “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

  18. Conditional Blocks & Loops

  19. if - elsif - else CSET4100: Server-Side Programming - Perl & CGI • 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”; }

  20. Examples CSET4100: Server-Side Programming - Perl & CGI $a = 3; if ($a) { print "foo1\n"; } else { print "bar1\n" } #prints “foo1”

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

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

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

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

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

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

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

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

  29. Logical AND/OR CSET4100: Server-Side Programming - Perl & CGI • Logical AND : && if (($x > 0) && ($x < 10)) { … } • Logical OR : || if ($x < 0) || ($x > 0)) { … } • Both are short-circuit — second expression evaluated only if necessary

  30. Ternary Operator CSET4100: Server-Side Programming - Perl & CGI • 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”;

  31. Assignment CSET4100: Server-Side Programming - Perl & CGI • Learning Perl • Review Ch. 1 & 2 • Read Ch. 3 (Lists and Arrays) & Ch. 6 (Hashes) • Homework • Check the website…

More Related