Download presentation
Presentation is loading. Please wait.
1
Perl Basics Learning Objectives: 1. To understand the commands available for control flow in Perl 2. To learn some useful operators in Perl
2
COMP111 Lecture 10 / Slide 2 Perl Basics Table of Content Control flow “if” statement “if … else “ statement “if … elsif … else “ statement Relational Operator Truth in Perl And, Or, Not “while” statement Looping using for “last” command String Operator Variable Interpolation Exponentiation Operator Precedence
3
COMP111 Lecture 10 / Slide 3 Control Flow Perl has several control flow statements: if while for unless until do while do until foreach
4
COMP111 Lecture 10 / Slide 4 “ if “ statement The Perl if statement works almost the same as in C++: #!/usr/local/bin/perl5 -w $user = `whoami`; chomp($user); if($user eq "clinton"){ print "Hi Bill!\n"; } The eq operator compares two strings, and returns true if they are equal (use == for numeric comparisons). The curly braces { } are always required in Perl (even if only one statement inside, unlike C++). This avoids the “ dangling else ” problem.
5
COMP111 Lecture 10 / Slide 5 “ if … else “ statement The if else statement is similar: #!/usr/local/bin/perl5 -w $user = `whoami`; chomp($user); if ($user eq "clinton") { print "Hi Bill!\n"; } else { print "Hi $user!\n"; }
6
COMP111 Lecture 10 / Slide 6 “ if … elsif … else “ statement You can also handle a list of cases: #!/usr/local/bin/perl5 -w $users = `who | wc -l`; chomp($users); if ($users > 4){ print "Heavy load!\n"; } elsif ($users > 1){ print "Medium load\n"; } else { print "Just me!\n"; }
7
COMP111 Lecture 10 / Slide 7 Relational Operators Perl ’ s numeric and string comparison operators: ComparisonNumericString Equal ==eq Not equal !=ne Less than <lt Greater than >gt Less than or equal to <=le Greater than or equal to >=ge
8
COMP111 Lecture 10 / Slide 8 Truth in Perl Truth is flexible in Perl: Expressions that evaluate to false 0# traditional false value ""# the null string "0"# only non-zero length false string Some examples of truth: 1 # traditional true value 684 # non-zero numerical values are true " " # whitespace is true "hello" # strings are true "00" # a string
9
COMP111 Lecture 10 / Slide 9 And, Or, Not 1 represents true, and 0 false (as in C++). You can also combine and negate expressions with logical and ( && ), logical or ( || ), and not ( ! ) just like in C++: #!/usr/local/bin/perl5 -w chomp($user = `whoami`); chomp($nme = `who | grep $user | wc -l`); chomp($nusers = `who | wc -l`); if($nusers - $nme && $user ne "clinton"){ print "Someone else is logged in!\n"; } else{ print "All is well!\n"; }
10
COMP111 Lecture 10 / Slide 10 “ while “ statement The while statement loops indefinitely, while the condition is true, such as a user-controlled condition: #!/usr/local/bin/perl5 -w $resp = "no"; while($resp ne "yes"){ print "Wakeup [yes/no]? "; chomp($resp = ); } $ test11 Wakeup [yes/no]? no Wakeup [yes/no]? y Wakeup [yes/no]? yes $
11
COMP111 Lecture 10 / Slide 11 Looping using for (1) for can be used as in C++ to do incrementing loops: $ cat fac #!/usr/local/bin/perl5 -w print "Enter number: "; $n = ; $fac = 1; for($i=1; $i<=$n; $i++){ $fac *= $i; } print "The factorial of $n is $fac\n"; $ fac Enter number: 5 The factorial of 5 is 120 $ Don’t forget to chomp $n
12
COMP111 Lecture 10 / Slide 12 With chomp(): $ cat fac #!/usr/local/bin/perl5 -w print "Enter number: "; chomp($n = ); $fac = 1; for($i=1; $i<=$n; $i++){ $fac *= $i; } print "The factorial of $n is $fac\n"; $ fac Enter number: 5 The factorial of 5 is 120 $ Looping using for (2)
13
COMP111 Lecture 10 / Slide 13 “last “ command The last command works like the C++ break command, breaking out of the innermost loop : $ cat test12 #!/usr/local/bin/perl5 -w while(1){ print "Wakeup [yes/no]? "; chomp($resp = ); if($resp eq "yes"){ last; } $ test12 Wakeup [yes/no]? no Wakeup [yes/no]? y Wakeup [yes/no]? yes $
14
COMP111 Lecture 10 / Slide 14 String Operators (1) Concatenate strings with the “. ” operator (a period). $ cat string #!/usr/local/bin/perl5 -w $name = "Bill". "Clinton"; print "$name\n"; print "Bill"."Gates"."\n"; $ string BillClinton BillGates $
15
COMP111 Lecture 10 / Slide 15 String Operators (2) The string repetition operator x allows you to repeat a string several times: $ cat string1 #!/usr/local/bin/perl5 -w $name = "Bill"x3; print "$name\n"; $n = 4; print "Bill" x 2. "Gates" x $n. "\n"; print 5; print "\n"; $test = ($n+1) x 4; print "$test\n"; $ string2 BillBillBill BillBillGatesGatesGatesGates 5 5555 $
16
COMP111 Lecture 10 / Slide 16 Variable Interpolation (1) Putting variables inside double quotes is called variable interpolation. We have seen many examples of this. The variable name will be the longest possible variable name that makes sense at that part of the string. Enclose the variable in a pair of curly braces if needed to override this.
17
COMP111 Lecture 10 / Slide 17 Variable Interpolation (2) $ cat bill1 #!/usr/local/bin/perl5 -w $bill = "trouble"; $billgates = "cheap"; print "Bill is $bill\n"; print "Bill is $billgates\n"; print "Bill is ${bill}gates\n"; print "Bill is "."$bill\n"; print "Bill is "."$bill"."\n"; $ bill1 Bill is trouble Bill is cheap Bill is troublegates Bill is trouble $
18
COMP111 Lecture 10 / Slide 18 Exponentiation Perl has an exponentiation operator ** unlike C++: $ cat exp #!/usr/local/bin/perl5 -w $n = 2; $m = 3; $result = $n ** $m; print "$n raised to the $m power is $result\n"; $ exp 2 raised to the 3 power is 8 $
19
COMP111 Lecture 10 / Slide 19 Operator Precedence Operator precedence is basically the same as in C++. As in C++, you can use parentheses to override precedence, and to clarify the grouping. $ cat prec #!/usr/local/bin/perl5 -w $n = 2; $m = 3; $result = $n + 1 * $m; print "$n plus one times $m is $result\n"; $result = ($n + 1) * $m; print "$n plus one times $m is $result\n"; $ prec 2 plus one times 3 is 5 $ prec 2 plus one times 3 is 9
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.