Presentation is loading. Please wait.

Presentation is loading. Please wait.

Perl Basics Software Tools. Slide 2 Control Flow l Perl has several control flow statements: n if n while n for n unless n until n do while n do until.

Similar presentations


Presentation on theme: "Perl Basics Software Tools. Slide 2 Control Flow l Perl has several control flow statements: n if n while n for n unless n until n do while n do until."— Presentation transcript:

1 Perl Basics Software Tools

2 Slide 2 Control Flow l Perl has several control flow statements: n if n while n for n unless n until n do while n do until n foreach

3 Slide 3 if The Perl if statement works almost the same as in C++: #!/usr/local/bin/perl5 -w $user = `whoami`; chomp($user); if($user eq "gates"){ 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.

4 Slide 4 if else The if else statement is similar: #!/usr/local/bin/perl5 -w $user = `whoami`; chomp($user); if ($user eq "gates") { print "Hi Bill!\n"; } else { print "Hi $user!\n"; }

5 Slide 5 if elsif else l 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"; }

6 Slide 6 Relational Operators l 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

7 Slide 7 Truth in Perl l Truth is flexible in Perl: n Expressions that evaluate to false 0# traditional false value ""# the null string "0"# only non-zero length false string n 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

8 Slide 8 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 "gates"){ print "Someone else is logged in!\n"; } else{ print "All is well!\n"; }

9 Slide 9 while 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 $

10 Slide 10 for 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

11 Slide 11 for 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 $

12 Slide 12 last 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 $

13 Slide 13 String Operators 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 $

14 Slide 14 String Operators 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"; $ string1 BillBillBill BillBillGatesGatesGatesGates 5 5555 $

15 Slide 15 Variable Interpolation l Putting variables inside double quotes is called variable interpolation. We have seen many examples of this. l 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.

16 Slide 16 Variable Interpolation $ 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 $

17 Slide 17 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 $

18 Slide 18 Operator Precedence l Operator precedence is basically the same as in C++. l 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 2 plus one times 3 is 9


Download ppt "Perl Basics Software Tools. Slide 2 Control Flow l Perl has several control flow statements: n if n while n for n unless n until n do while n do until."

Similar presentations


Ads by Google