120 likes | 254 Vues
Dive into the world of Perl, a portable and versatile scripting language developed by Larry Wall. This session on March 15, 2007, celebrates Pi Day while covering the basics of Perl programming. Learn about simple syntax, including scalar and non-scalar variables, control structures, and built-in functions. Engage in hands-on exercises, such as printing messages, gathering user input, and working with arrays. Discover how Perl combines the power of C with the ease of use, making it a strong choice for developers under UNIX systems.
E N D
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 • 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.
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
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.
Exercise 1 #!/usr/local/bin/perl –w print (“Hello, world!\n”);
Exercise 2 #!/usr/local/bin/perl –w print “What is your name? “; $name = <STDIN>; chomp ($name); print “Hello, $name!\n”;
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 }
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>); } }
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
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
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