| |
Flow Control
Page history last edited by dmlond 2 yrs ago
- the evanescent quality of 'Truth', at least in Perl: Perl's way of deciding whether a statement is true may seem strange when you first start using it, but it is extremely powerful once you get the hang of it. Basically, undef and numeric zero are false. Anything else is true.
($a); # true if $a is defined, and not assigned 0. (defined($a)); # true if $a has been set to some value, even 0 ($a == 0); # true if $a numerically equals 0 ($a && $a < 5); # true if $a is defined and $a < 5 (&some_subroutine($somestuff)); # true if the return of some_subroutine is defined and not 0
Anything that is true in perl can be made false using the negation operator '!'.
(!$a); # true if $a is not defined, or if it is assigned 0 (!&fileExists($file)); # true if the return from the fileExists subroutine would be false without the negation operator, e.g. true if the file does not exist
- if, elsif, else: This is one thing that will confuse those who program in other languages. Perl uses elsif, not else if, or elif.
if ($a && ($a < b) { &do_something_with($a); }
if ($a < $b) { &do_something_with($a); } else { &throw_hands_up_in_exasperation; }
if ($a gt $b) { &do_something_with($a); } elsif ($b gt $a) { &do_something_with($b); } else { &get_really_angry; }
- unless: do something unless a statement is true:
unless(&dislikes_pain($user)) { &mess_with_bob(); }
- post assignment tests take precedence over assignment. This is useful for writing tighter, less verbose, self-documenting code.
my $a = &get_it($somevalue) if (&its_true($somevalue)); # only assigns to $a if the test is true my $a = &get_it($somevalue) unless($seen{$somevalue}++); # You will use a seen hash many times. They are quite useful. This only assigns if $somevalue is a new key in $seen (it has not been initialized and autoincremented yet).
- while: do something while a test is true (or !true). This is the best way to access the items in an iterator (such as the reads from a filehandle, or accessing each element from an object using something like a 'next' method):
my $items = 0; while ($items <= 10) { print "processing item $items\n"; $items++; }
while (my line = <INPUT>) { &process($line); }
- for: just like in c, eg takes a semi-colon separated list of 3 statements: a loop initialization statement, a continuation test, and a loop increment statement.
for (my $i = 0; $i <= $#items; $i++) { print "ITEM $i is $items[$i]\n"; }
for (my $i = 0, $n = @items; $i < $n; $i++) { print "ITEM $i is $items[$i]\n"; }
# WHICH OF THE ABOVE IS MORE MEMORY EFFICIENT?
- foreach: iterates over a list, optionally assigning the next item to a local variable, if present ($_ otherwise). Note, if the list in question is actually the return value of a subroutine (or object method), the subroutine is only run once at the beginning of the foreach loop to generate the list, and then each item is used as normal. Note, for very large lists, it may be more memory efficient to use 'while' in the context of an iterator system.
foreach (@items) { print "ITEM $_ SEEN\n"; }
foreach my $item (@items) { print "ITEM $item SEEN\n"; }
- next, last: these commands allow you to skip further processing of the current iteration of a loop, or exit the loop entirely, respectively. Note, perl allows you to give your loops names, and use these names in next and last calls. This can be very handy to document complex nested loops.
foreach my $item (@items) { next if (&disregard($item)); last if (&favorite_item_found()); &process($item); }
OUTER: foreach my $item (@items) { Â Â INNER: foreach my $tocomp (@items_to_compare) { Â Â Â Â next INNER if (!defined($item_to_comp)); Â Â Â Â next OUTER if (&items_differ($item, $to_comp)); Â Â } }
Flow Control
|
|
Tip: To turn text into a link, highlight the text, then click on a page or file from the list above.
|
|
|
Comments (0)
You don't have permission to comment on this page.