Perl Lesson 5 by bzs12927

VIEWS: 60 PAGES: 4

									        Perl                            Lesson 5


              courses@xfzhou.homedns.org



                                          1




                Special Variables 2
•   $.      Current line number of the file handle last read
•   $"      separator for print "@array";
•   $!      Error number or error string
•   In map( ) function: $_
    @upper_case_list = map(uc($_), @list);
• In sort( ) function: $a, $b
    # same thing, but with explicit sort routine
    @articles = sort {$a cmp $b} @files;

    # now case-insensitively
    @articles = sort {uc($a) cmp uc($b)} @files;

    # same thing in reversed order
    @articles = sort {$b cmp $a} @files;
• perldoc perlvar
                                          2




                                                               1
                        Statements
• Statement block and local var
  Using my($var, @arr, %hash) is safer
  $var = "B"; {my($var) = "A"; print $var x 5; } print $var;
  ==> AAAAAB

• Modified statement
  Expression modifier (Condition); if, unless, while, until
  !!Always evaluates Condition before execute Expression!!

  print " \$A is negitive!" if $A < 0;
  die "SOS!" if ($fail);
  $B=1/$A unless $A == 0;

   `\HN ^KVDK                            #avoid over-write
                `\HN ^KVDK GHQLIHG VVHOQX Y
  $I=1; $J=0; $J+=$I++ until ($I > 10); print $J; # ==> 55

  perl -e "print while <>;" < readme.txt
                                                 3




                    Control Flow 1
                                          #   Multi-choice:
 • False conditions:                      #   not "else if"
    0.000, 0, undef, "0", ""              #   not "elseif"
                                          #   but "elsif"
                       if (…) {
 • If statement:         …                if (…) {
                       }                    …
                       else {             }
  if (…) {
                         …                elsif (…) {
    …
                       }                    …
  }
                                          }
                                          elsif (…) {
                                            …
  # compare modified statement
                                          }
                                          else {
  expression if …;
                                            …
  expression unless …;
                                          }
                                                 4




                                                               2
                Control Flow 2
• while loop                       until loop
# loop before   while (…) {           # loop before
# check           …                   # check
do {            }                     do {
  …             continue {              …
} while (…);      # run before        } until (…);
                  # next
                  # iteration
# check                               # check
                  …
# before loop                         # before loop
                }
while (…) {                           until (…) {
  …                                     …
}                                     }


                                     5




                Control Flow 3
             for (start_exp, condition_exp, step_exp) {
• for loop     …
             }

for ($n=1, $sum=0; $sum<=1000; $n++) {
  $sum += $n;
}
print "n=$n; sum=$sum\n";
$sum -= $n--;
print "n=$n; sum=$sum\n";

# n=46; sum=1035
# n=45; sum=989
                                     6




                                                          3
                  Control Flow 4
• foreach loop           foreach $var (@list) {
                           …
                         }
 # !!! Side effect of foreach !!!
 @array = (1..5, 5..10);
 print("@array\n");
 foreach (@array) {
     $_ = "Five" if ($_ == 5);
 }
 print("@array\n");

 # 1 2 3 4 5 5 6 7 8 9 10
 # 1 2 3 4 Five Five 6 7 8 9 10
 foreach $var (0..10) { $var *= $var; } #wrong
                                             7




                  Control Flow 5
• Jump keywords: last, next, redo, goto
   – Avoid goto



  Keywords Description
  last      Jumps out of the current statement block.
            Skips the rest of the statement block and continues
  next
            with the next iteration of the loop.
  redo      Restarts the statement block.
  goto      Jumps to a specified label.




                                             8




                                                                  4

								
To top