Blame


1 8f7f2f4a 2021-12-17 jrmu ================================================================================
2 8f7f2f4a 2021-12-17 jrmu
3 8f7f2f4a 2021-12-17 jrmu If, Else, Elsif
4 8f7f2f4a 2021-12-17 jrmu
5 8f7f2f4a 2021-12-17 jrmu In perl, we can control how a program executes by using if, elsif, and else:
6 8f7f2f4a 2021-12-17 jrmu
7 8f7f2f4a 2021-12-17 jrmu if (CONDITION) {
8 8f7f2f4a 2021-12-17 jrmu STATEMENT;
9 8f7f2f4a 2021-12-17 jrmu } elsif (CONDITION) {
10 8f7f2f4a 2021-12-17 jrmu STATEMENT;
11 8f7f2f4a 2021-12-17 jrmu } else {
12 8f7f2f4a 2021-12-17 jrmu STATEMENT;
13 8f7f2f4a 2021-12-17 jrmu }
14 8f7f2f4a 2021-12-17 jrmu
15 8f7f2f4a 2021-12-17 jrmu If the first CONDITION is true, we execute the statements in the first block;
16 8f7f2f4a 2021-12-17 jrmu else (otherwise) if the second CONDITION is true, we execute the statements
17 8f7f2f4a 2021-12-17 jrmu in the second block; else we execute the statements in the last block.
18 8f7f2f4a 2021-12-17 jrmu
19 8f7f2f4a 2021-12-17 jrmu NOTE: In many languages, the keyword is else if. In perl, however, it's elsif
20 8f7f2f4a 2021-12-17 jrmu (there is only one letter 'e').
21 8f7f2f4a 2021-12-17 jrmu
22 8f7f2f4a 2021-12-17 jrmu my @feeds;
23 8f7f2f4a 2021-12-17 jrmu ...
24 8f7f2f4a 2021-12-17 jrmu if (scalar(@feeds) == 0) {
25 8f7f2f4a 2021-12-17 jrmu print "You have no feeds\n";
26 8f7f2f4a 2021-12-17 jrmu } elsif (scalar(@feeds) < 10) {
27 8f7f2f4a 2021-12-17 jrmu print "You have ".scalar(@feeds)." feeds\n";
28 8f7f2f4a 2021-12-17 jrmu } else {
29 8f7f2f4a 2021-12-17 jrmu print "You have too many feeds!\n";
30 8f7f2f4a 2021-12-17 jrmu }
31 8f7f2f4a 2021-12-17 jrmu
32 8f7f2f4a 2021-12-17 jrmu The code above first checks if scalar(@feeds) (the length of the array
33 8f7f2f4a 2021-12-17 jrmu @feeds) is equal to zero. If so, it says you have no feeds. Otherwise, it
34 8f7f2f4a 2021-12-17 jrmu checks if the length of the feeds is less than 10. If so, it reports the
35 8f7f2f4a 2021-12-17 jrmu number of feeds. Otherwise, it says you have too many feeds.
36 8f7f2f4a 2021-12-17 jrmu
37 8f7f2f4a 2021-12-17 jrmu ================================================================================
38 8f7f2f4a 2021-12-17 jrmu
39 8f7f2f4a 2021-12-17 jrmu Booleans
40 8f7f2f4a 2021-12-17 jrmu
41 8f7f2f4a 2021-12-17 jrmu Perl is flexible about what it considers to be true or false. The empty
42 8f7f2f4a 2021-12-17 jrmu string "" and "0" are considered false. All other strings are true. The
43 8f7f2f4a 2021-12-17 jrmu number 0 is considered false; all other numbers are true. All references
44 8f7f2f4a 2021-12-17 jrmu are true; undef (for when a variable is undefined) is false.
45 8f7f2f4a 2021-12-17 jrmu
46 8f7f2f4a 2021-12-17 jrmu This can help save some typing. For example:
47 8f7f2f4a 2021-12-17 jrmu
48 8f7f2f4a 2021-12-17 jrmu if (scalar(@feeds) == 0) {
49 8f7f2f4a 2021-12-17 jrmu print "You have no feeds\n";
50 8f7f2f4a 2021-12-17 jrmu }
51 8f7f2f4a 2021-12-17 jrmu
52 8f7f2f4a 2021-12-17 jrmu can now be rewritten as:
53 8f7f2f4a 2021-12-17 jrmu
54 8f7f2f4a 2021-12-17 jrmu if (scalar(@feeds)) {
55 8f7f2f4a 2021-12-17 jrmu print "You have no feeds\n";
56 8f7f2f4a 2021-12-17 jrmu }
57 8f7f2f4a 2021-12-17 jrmu
58 8f7f2f4a 2021-12-17 jrmu ================================================================================
59 8f7f2f4a 2021-12-17 jrmu
60 8f7f2f4a 2021-12-17 jrmu Comparisons
61 8f7f2f4a 2021-12-17 jrmu
62 8f7f2f4a 2021-12-17 jrmu Perl has different operators for comparing strings and numbers:
63 8f7f2f4a 2021-12-17 jrmu
64 8f7f2f4a 2021-12-17 jrmu Comparison | String | Numeric
65 8f7f2f4a 2021-12-17 jrmu --------------+---------------+------------------
66 8f7f2f4a 2021-12-17 jrmu equal | eq | ==
67 8f7f2f4a 2021-12-17 jrmu --------------+---------------+------------------
68 8f7f2f4a 2021-12-17 jrmu not equal | ne | !=
69 8f7f2f4a 2021-12-17 jrmu --------------+---------------+------------------
70 8f7f2f4a 2021-12-17 jrmu less than | lt | <
71 8f7f2f4a 2021-12-17 jrmu --------------+---------------+------------------
72 8f7f2f4a 2021-12-17 jrmu greater than | gt | >
73 8f7f2f4a 2021-12-17 jrmu --------------+---------------+------------------
74 8f7f2f4a 2021-12-17 jrmu less than or | le | <=
75 8f7f2f4a 2021-12-17 jrmu equal to | |
76 8f7f2f4a 2021-12-17 jrmu --------------+---------------+------------------
77 8f7f2f4a 2021-12-17 jrmu greater than | ge | >=
78 8f7f2f4a 2021-12-17 jrmu or equal to | |
79 8f7f2f4a 2021-12-17 jrmu --------------+---------------+------------------
80 8f7f2f4a 2021-12-17 jrmu comparison | cmp | <=>
81 8f7f2f4a 2021-12-17 jrmu
82 8f7f2f4a 2021-12-17 jrmu For example, if you want to compare if two strings are equal:
83 8f7f2f4a 2021-12-17 jrmu
84 8f7f2f4a 2021-12-17 jrmu if ($arguments->{who} eq "nickname") {
85 8f7f2f4a 2021-12-17 jrmu ...
86 8f7f2f4a 2021-12-17 jrmu }
87 8f7f2f4a 2021-12-17 jrmu
88 8f7f2f4a 2021-12-17 jrmu This checks if the sender's nick is the same as "nickname". If you want
89 8f7f2f4a 2021-12-17 jrmu to compare if two numbers are equal:
90 8f7f2f4a 2021-12-17 jrmu
91 8f7f2f4a 2021-12-17 jrmu if ($arguments->{who} eq "nickname") {
92 8f7f2f4a 2021-12-17 jrmu ...
93 8f7f2f4a 2021-12-17 jrmu }
94 8f7f2f4a 2021-12-17 jrmu
95 8f7f2f4a 2021-12-17 jrmu The spaceship operator <=> and cmp compares the first scalar to the second.
96 8f7f2f4a 2021-12-17 jrmu -1 is returned if the first is less than the second, 0 if they are equal,
97 8f7f2f4a 2021-12-17 jrmu and +1 is the first is greater than the second:
98 8f7f2f4a 2021-12-17 jrmu
99 8f7f2f4a 2021-12-17 jrmu print $x <=> $y;
100 8f7f2f4a 2021-12-17 jrmu
101 8f7f2f4a 2021-12-17 jrmu This prints -1 if $x < $y, 0 if $x == $y, and 1 if $x > $y.
102 8f7f2f4a 2021-12-17 jrmu
103 8f7f2f4a 2021-12-17 jrmu WARNING: Make sure to use the correct operator for strings and numbers.
104 8f7f2f4a 2021-12-17 jrmu "6" < "1337" is true because the number 6 is less than the number 1337,
105 8f7f2f4a 2021-12-17 jrmu but "6" lt "1337" is false because the string "1337" comes before "6"
106 8f7f2f4a 2021-12-17 jrmu when sorted alphabetically. (It's the same reason why the word "bot" comes
107 8f7f2f4a 2021-12-17 jrmu before the word "computer" in the dictionary.)
108 8f7f2f4a 2021-12-17 jrmu
109 8f7f2f4a 2021-12-17 jrmu ================================================================================
110 8f7f2f4a 2021-12-17 jrmu
111 8f7f2f4a 2021-12-17 jrmu Logical Operators
112 8f7f2f4a 2021-12-17 jrmu
113 8f7f2f4a 2021-12-17 jrmu Operator | Operator | Meaning
114 8f7f2f4a 2021-12-17 jrmu --------------+--------------+-------------------------------------------
115 8f7f2f4a 2021-12-17 jrmu && | and | True if both operands are true,
116 8f7f2f4a 2021-12-17 jrmu | | false otherwise
117 8f7f2f4a 2021-12-17 jrmu --------------+--------------+-------------------------------------------
118 8f7f2f4a 2021-12-17 jrmu || | or | True if either operand is true,
119 8f7f2f4a 2021-12-17 jrmu | | false otherwise
120 8f7f2f4a 2021-12-17 jrmu --------------+--------------+-------------------------------------------
121 8f7f2f4a 2021-12-17 jrmu ! | not | False if operand is true,
122 8f7f2f4a 2021-12-17 jrmu | | true if operand is false
123 8f7f2f4a 2021-12-17 jrmu --------------+--------------+-------------------------------------------
124 8f7f2f4a 2021-12-17 jrmu | xor | True if the first or second operand is
125 8f7f2f4a 2021-12-17 jrmu | | true, false otherwise
126 8f7f2f4a 2021-12-17 jrmu
127 8f7f2f4a 2021-12-17 jrmu We can use the || or operator for providing default values:
128 8f7f2f4a 2021-12-17 jrmu
129 8f7f2f4a 2021-12-17 jrmu $var ||= 1;
130 8f7f2f4a 2021-12-17 jrmu
131 8f7f2f4a 2021-12-17 jrmu This means the same as:
132 8f7f2f4a 2021-12-17 jrmu
133 8f7f2f4a 2021-12-17 jrmu $var = $var || 1;
134 8f7f2f4a 2021-12-17 jrmu
135 8f7f2f4a 2021-12-17 jrmu If $var is not defined, then $var is false, and undef || 1 will
136 8f7f2f4a 2021-12-17 jrmu return the second value, 1. So $var gets the default value of 1
137 8f7f2f4a 2021-12-17 jrmu if it is undefined.
138 8f7f2f4a 2021-12-17 jrmu
139 8f7f2f4a 2021-12-17 jrmu ================================================================================
140 8f7f2f4a 2021-12-17 jrmu
141 8f7f2f4a 2021-12-17 jrmu While, Do
142 8f7f2f4a 2021-12-17 jrmu
143 8f7f2f4a 2021-12-17 jrmu In a while loop, perl checks if CONDITION is true; if so, it executes
144 8f7f2f4a 2021-12-17 jrmu STATEMENT, then repeats the loop again. If CONDITION is false, it leaves
145 8f7f2f4a 2021-12-17 jrmu the loop.
146 8f7f2f4a 2021-12-17 jrmu
147 8f7f2f4a 2021-12-17 jrmu while (CONDITION) {
148 8f7f2f4a 2021-12-17 jrmu STATEMENT;
149 8f7f2f4a 2021-12-17 jrmu }
150 8f7f2f4a 2021-12-17 jrmu
151 8f7f2f4a 2021-12-17 jrmu Here's a sample while loop:
152 8f7f2f4a 2021-12-17 jrmu
153 8f7f2f4a 2021-12-17 jrmu while (scalar(@feedURLs)) {
154 8f7f2f4a 2021-12-17 jrmu my $url = pop(@feedURLs);
155 8f7f2f4a 2021-12-17 jrmu process($url);
156 8f7f2f4a 2021-12-17 jrmu }
157 8f7f2f4a 2021-12-17 jrmu
158 8f7f2f4a 2021-12-17 jrmu While there are still urls remaining, we pop the last url from
159 8f7f2f4a 2021-12-17 jrmu @feedURLs, remove it from the array, and process it.
160 8f7f2f4a 2021-12-17 jrmu
161 8f7f2f4a 2021-12-17 jrmu ================================================================================
162 8f7f2f4a 2021-12-17 jrmu
163 8f7f2f4a 2021-12-17 jrmu For Loop
164 8f7f2f4a 2021-12-17 jrmu
165 8f7f2f4a 2021-12-17 jrmu for (INITIALIZE; CONDITION; STEP) {
166 8f7f2f4a 2021-12-17 jrmu STATEMENT;
167 8f7f2f4a 2021-12-17 jrmu }
168 8f7f2f4a 2021-12-17 jrmu
169 8f7f2f4a 2021-12-17 jrmu INITIALIZE is executed only once at the beginning.
170 8f7f2f4a 2021-12-17 jrmu
171 8f7f2f4a 2021-12-17 jrmu Next, CONDITION is checked. If CONDITION is false, the loop is finished.
172 8f7f2f4a 2021-12-17 jrmu If CONDITION is true, then STATEMENT is executed, and then STEP.
173 8f7f2f4a 2021-12-17 jrmu Then the loop repeats itself.
174 8f7f2f4a 2021-12-17 jrmu
175 8f7f2f4a 2021-12-17 jrmu for (my $i = 0; $i < scalar(@nicks); $i++) {
176 8f7f2f4a 2021-12-17 jrmu $self->say(
177 8f7f2f4a 2021-12-17 jrmu channel => "#perl103"
178 8f7f2f4a 2021-12-17 jrmu body => "Hi, $nick[$i]";
179 8f7f2f4a 2021-12-17 jrmu );
180 8f7f2f4a 2021-12-17 jrmu }
181 8f7f2f4a 2021-12-17 jrmu
182 8f7f2f4a 2021-12-17 jrmu In this code, we first run INITIALIZE: set $i to zero. Then, we check
183 8f7f2f4a 2021-12-17 jrmu CONDITION: is $i less than the length of the array @nicks? If true,
184 8f7f2f4a 2021-12-17 jrmu we execute STATEMENT. If not, the loop is finished.
185 8f7f2f4a 2021-12-17 jrmu
186 8f7f2f4a 2021-12-17 jrmu For STATEMENT, we send a message to the channel #perl103 and say hi to
187 8f7f2f4a 2021-12-17 jrmu $nick[$i], the nick at index $i in the array. If $i = 0, then we say hi
188 8f7f2f4a 2021-12-17 jrmu to $nick[0], the first nick. If $i = 1, then we say hi to $nick[1],
189 8f7f2f4a 2021-12-17 jrmu the second nick.
190 8f7f2f4a 2021-12-17 jrmu
191 8f7f2f4a 2021-12-17 jrmu In other words, this loop sends a hello message to channel #perl103 to
192 8f7f2f4a 2021-12-17 jrmu every nick in the array @nicks.
193 8f7f2f4a 2021-12-17 jrmu
194 8f7f2f4a 2021-12-17 jrmu ================================================================================
195 8f7f2f4a 2021-12-17 jrmu
196 8f7f2f4a 2021-12-17 jrmu Challenge
197 8f7f2f4a 2021-12-17 jrmu
198 8f7f2f4a 2021-12-17 jrmu View the file ~/challenge to finish the lesson.
199 8f7f2f4a 2021-12-17 jrmu
200 8f7f2f4a 2021-12-17 jrmu ================================================================================