Presentation is loading. Please wait.

Presentation is loading. Please wait.

CSC 4630 Perl 1. Perl Practical Extraction and Support Language A glue language under UNIX Written by Larry Wall Claimed to be the most portable of scripting.

Similar presentations


Presentation on theme: "CSC 4630 Perl 1. Perl Practical Extraction and Support Language A glue language under UNIX Written by Larry Wall Claimed to be the most portable of scripting."— Presentation transcript:

1 CSC 4630 Perl 1

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; e.g. (either) #! /opt/local/bin/perl –w #! /usr/bin/perl –w Invoke by perl or Scalar variables have names that start with $ and have values that are either strings or double precision real numbers.

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

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

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

9 Exercise 4 #! /opt/local/bin/perl –w $secretword = “llama”; # the secret word print “What is your name? ”; chomp($name = ); 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 = ); while ($guess ne $secretword) { print “Wrong, try again. Secret word? ”; chomp($guess = ); }

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 #!/opt/local/bin/perl –w @swords = qw (camel llama alpaca); #set multiple secret words print “What is your name? ”; chomp($name = ); 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 = ); $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 = ); $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: $ … Assignment: = (carries value of expression) binary assignment: += *=.= etc. increment: prefix, postfix ++$i $i++ increment then assign vs. assign then increment


Download ppt "CSC 4630 Perl 1. Perl Practical Extraction and Support Language A glue language under UNIX Written by Larry Wall Claimed to be the most portable of scripting."

Similar presentations


Ads by Google