_learning perl_, ch9, 5.I.2002, nh MISCELLANEOUS CONTROL STRUCTURES last; --> like "break" in C -- breaks out of innermost loop (excluding do{} while/until) --> only counts looping blocks, not other blocks that are needed to make up some syntactic construct. `-> for, foreach, while, until, or "naked" blocks ^^^^^ a block not part of a larger construct while (){ if (/^From: /){ if (/somebody/){ print "email from somebody!\n"; } last; # somebody found; no need to keep looking } if (/^$/) { # blank line, i.e., end of headers last; # headers are over; stop looking } } next; --> next causes execution to skip over rest of block, instead of terminating it while (x) { do_this; and_that; if (y) { do_other_thing; next; # if y is true, do_last_things are } # skipped do_last_things; # next jumps to here } redo; --> causes a jump to the beginning of the current block: { # this is a naked block -- no "while (1){}, just "{}" # redo jumps to here do_this; and_that; if (y) { do_other_thing; redo; # jumps back to beginning of looping block } do_more_things; } labeled blocks ============== * can label each block for ease of jumping around in or out of (but not into) * labels in UPPERCASE LETTERS with no prefix OUTER_BLOCK: for ($i=1; $i<10; $i++) { INNER_BLOCK: for ($j=1; $j<10; $j++) { if ($i * $j == 63) { print "aaa! 63! terminating!!\n" # when found, exit alles last OUTER_BLOCK; } if ($j > $i) { # ensures second num will always be bigger next OUTER; } } } expression modifiers: ==================== you can condense if (control_exp) { then_this; } to: then_this if control_exp; * but it has to be one statement, not a block of statements * and the expression reads backwards, but oh well exiting from a loop if a certain condition arises: LINE: while (){ last LINE if /^From: /; # exits if the line starts with "From: " } also: exp2 unless ctrl_exp; exp2 while ctrl_exp; exp2 until ctrl_exp; * these forms DO NOT NEST. && and || as control structures =============================== like shell scripting: this && that; # = that if this; => if (this){that}; if "this" works and evaluates as true, then the logical-and op executes "that". otherwise, not. truth tables!! :) AND: -------------------- if | this | 1 | 1 | 0 | # if "this" evaluates to true, then the |------|---|---|---| # value of entire exp is still not and | that | 1 | 0 | x | # known, so "that" must be eval'd. |======|===|===|===| truth => | 1 | 0 | 0 | # if "this" is false, so's the whole thing! -------------------- also, can evaluate "unless"es with ||s: this || that # = this unless that; => unless (this){that}; OR: ------------------- | this: | 1 1 0 0 | # if "this" is true, then "that" is skipped. | that: | 1 0 1 0 | |=======|=========| # if "this" is false, then "that" is used. |truth: | 0 1 1 0 | -------------------