1 / 12

CSC 4630

CSC 4630. Meeting 15 March 14, 2007 Happy Pi Day. Perl. Practical Extraction and Support Language A glue language under UNIX Written by Larry Wall Claimed to be the most portable of scripting languages See www.perl.com. Perl by Example.

neviah
Download Presentation

CSC 4630

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. CSC 4630 Meeting 15 March 14, 2007 Happy Pi Day

  2. Perl • Practical Extraction and Support Language • A glue language under UNIX • Written by Larry Wall • Claimed to be the most portable of scripting languages • See www.perl.com

  3. Perl by Example • Perl is more powerful than awk, but looks somewhat the same. • Perl is more flexible than shell scripts, but uses some of the same constructs. • Perl is based on C, and in fact the interpreter is written in C, but is easier to use than C.

  4. Perl by Example (2) Beginning assumptions and notations • Statements are terminated by ; (semicolon) • Comments start anywhere on a line with # and end at the end of the line. Thus, you can write explanations on the line for the line. But they can’t be two line explanations. • Built-in functions neither require nor forbid ( ) around their arguments

  5. Perl by Example (3) • First line of Perl program should be a directive to the interpreter: #!/usr/local/bin/perl –w • Scalar variables have names that start with $ and have values that are either strings or double precision real numbers.

  6. Exercise 1 #!/usr/local/bin/perl –w print (“Hello, world!\n”);

  7. Exercise 2 #!/usr/local/bin/perl –w print “What is your name? “; $name = <STDIN>; chomp ($name); print “Hello, $name!\n”;

  8. Exercise 3 #!/usr/local/bin/perl –w print “What is your name? “; $name = <STDIN>; chomp ($name); if ($name eq “Randal”) { print “Hello, $name! Glad you’re here\n” } else { print “Hello, $name!\n”; # ordinary greeting }

  9. Exercise 4 #!/usr/local/bin/perl –w $secretword = “llama”; # the secret word print “What is your name?”; chomp($name = <STDIN>); if ($name eq “Randal”) { print “Hello, $name! Glad you’re here\n” } else { print “Hello, $name!\n”; print “What is the secret word?”; chomp($guess = <STDIN>); while ($guess ne $secretword) { print “Wrong, try again. Secret word?”; chomp($guess = <STDIN>); } }

  10. Non-scalar Variables For example, arrays: • Identifier starts with an @ • Can be initialized with a list of strings: @words = (“camel”, “llama”, “alpaca”); • Or with the qw operator (which saves keystrokes) @words = qw (camel llama alpaca); • Elements accessed as scalars, so $words[0] is camel, $words[1] is llama NB: Index origin is 0 • $i = 2 ; $words[$i] has value alpaca

  11. Exercise 5 #!/usr/local/bin/perl –w @swords = qw (camel llama alpaca); #set multiple secret words print “What is your name?”; chomp($name = <STDIN>); if ($name eq “Randal”) { print “Hello, $name! Glad you’re here\n” } else { print “Hello, $name!\n”; print “What is the secret word?”; chomp($guess = <STDIN>); $i = 0; #index in swords array $correct = “maybe”; #status of search while ($correct eq “maybe”) { if($guess eq $swords[$i]) { $correct = “yes”} # set flag on success elsif ($i < 2) {++$i} # not success, try next secret word else { print “Wrong, try again. Secret word?”; # no word matches, ask again chomp($guess = <STDIN>); $i=0; # reset index in secret word array } # end if sequence } # end while not correct } # end if not Randal

  12. Scalar operators • Numeric: + - * / ** % • Numeric comparison: < <= == >= > != • String: . (for concat) x (for multiple concat) • String comparison: lt le eq ge gt ne • Identifier: $ <letter> <letter | digit | under > … • Assignment: = (carries value of expression) binary assignment: += *= .= etc. increment: prefix, postfix ++$i $i++ increment then assign vs. assign then increment

More Related