Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Newer
Older
100644 130 lines (86 sloc) 2.67 kb
d5f3b1b @petdance Importing from Andy original repo
authored
1 =head1 Flow control
2
3 =head2 postfix controls
4
5 A simple C<if> or C<unless> block might look like this:
6
7 if ($is_frobnitz) {
8 print "FROBNITZ DETECTED!\n";
9 }
10
11 In these cases, simple statements can have the C<if>
12 or C<unless> appended to the end.
13
14 print "FROBNITZ DETECTED!\n" if $is_frobnitz;
15 die "BAILING ON FROBNITZ!\n" unless $deal_with_frobnitz;
16
17 This also works for C<while> and C<for>.
18
19 print $i++ . "\n" while $i < 10;
20
21 =head2 For loops (for and foreach are really synonyms)
22
23 There are three styles of for loops.
24
25 my @array;
26
27 # Old style C for loops
28 for (my $i = 0; $i < 10; $i++) {
29 $array[$i] = $i;
30 }
31
32 # Iterating loops
33 for my $i (@array) {
34 print "$i\n";
35 }
36
37 # Postfix for loops
38 print "$_\n" for @array;
39
40 You may see C<foreach> used in place of C<for>. The two
41 are interchangable. Most people use C<foreach> for the
42 last two styles of loops above.
43
44 =head2 do
45
46 C<do> allows Perl to use a block where a statement is expected.
47
48 open( my $file, '<', $filename ) or die "Can't open $filename: $!"
49
50 But if you need to do something else:
51
52 open( my $file, '<', $filename ) or do {
53 close_open_data_source();
54 die "Aborting: Can't open $filename: $!\n";
55 };
56
57 The following are also equivalent:
58
59 if ($condition) { action(); }
60 do { action(); } if $condition;
61
62 As a special case, C<while> runs the block at least once.
63
64 do { action(); } while action_needed;
65
66 =head2 if/elsif/else BUT NO case
67
68 If you're coming from another language, you might be used
69 to C<case> statements. Perl doesn't have them.
70
71 The closest we have is C<elsif>:
72
73 if ($condition_one) {
74 action_one();
75 }
76 elsif ($condition_two) {
77 action_two();
78 }
79 ...
80 else {
81 action_n();
82 }
83
84 There is no way to fall through cases cleanly.
85
86 =head2 Four values of false
87
88 There are four ways to have a false value in Perl:
89
90 my $false = undef;
91 $false = "";
92 $false = 0;
93 $false = "0";
94
95 The last one is false because "0" becomes 0 in numeric
96 context, which is false by the third rule.
97
98 =head2 next/last/continue/redo
99
100 Consider the following loop:
101
102 $i = 0;
103 while ( 1 ) {
104 last if $i > 3;
105 $i++;
106 next if $i == 1;
107 redo if $i == 2;
108 }
109 continue {
110 print "$i\n";
111 }
112
113 prints
114
115 1
116 3
117 4
118
119 =over
120
121 =item * C<next> skips to the end of the block and continues or restarts
122
123 =item * C<redo> jumps back to the beginning of the loop immediately
124
125 =item * C<last> skips to the end of the block and stops the loop from executing again
126
127 =item * C<continue> is run at the end of the block
128
129 =back
Something went wrong with that request. Please try again.