Blame
Date:
Tue Dec 21 08:11:28 2021 UTC
Message:
Added README to install dependencies
001
2021-12-17
jrmu
================================================================================
002
2021-12-17
jrmu
003
2021-12-17
jrmu
If, Else, Elsif
004
2021-12-17
jrmu
005
2021-12-17
jrmu
In perl, we can control how a program executes by using if, elsif, and else:
006
2021-12-17
jrmu
007
2021-12-17
jrmu
if (CONDITION) {
008
2021-12-17
jrmu
STATEMENT;
009
2021-12-17
jrmu
} elsif (CONDITION) {
010
2021-12-17
jrmu
STATEMENT;
011
2021-12-17
jrmu
} else {
012
2021-12-17
jrmu
STATEMENT;
013
2021-12-17
jrmu
}
014
2021-12-17
jrmu
015
2021-12-17
jrmu
If the first CONDITION is true, we execute the statements in the first block;
016
2021-12-17
jrmu
else (otherwise) if the second CONDITION is true, we execute the statements
017
2021-12-17
jrmu
in the second block; else we execute the statements in the last block.
018
2021-12-17
jrmu
019
2021-12-17
jrmu
NOTE: In many languages, the keyword is else if. In perl, however, it's elsif
020
2021-12-17
jrmu
(there is only one letter 'e').
021
2021-12-17
jrmu
022
2021-12-17
jrmu
my @feeds;
023
2021-12-17
jrmu
...
024
2021-12-17
jrmu
if (scalar(@feeds) == 0) {
025
2021-12-17
jrmu
print "You have no feeds\n";
026
2021-12-17
jrmu
} elsif (scalar(@feeds) < 10) {
027
2021-12-17
jrmu
print "You have ".scalar(@feeds)." feeds\n";
028
2021-12-17
jrmu
} else {
029
2021-12-17
jrmu
print "You have too many feeds!\n";
030
2021-12-17
jrmu
}
031
2021-12-17
jrmu
032
2021-12-17
jrmu
The code above first checks if scalar(@feeds) (the length of the array
033
2021-12-17
jrmu
@feeds) is equal to zero. If so, it says you have no feeds. Otherwise, it
034
2021-12-17
jrmu
checks if the length of the feeds is less than 10. If so, it reports the
035
2021-12-17
jrmu
number of feeds. Otherwise, it says you have too many feeds.
036
2021-12-17
jrmu
037
2021-12-17
jrmu
================================================================================
038
2021-12-17
jrmu
039
2021-12-17
jrmu
Booleans
040
2021-12-17
jrmu
041
2021-12-17
jrmu
Perl is flexible about what it considers to be true or false. The empty
042
2021-12-17
jrmu
string "" and "0" are considered false. All other strings are true. The
043
2021-12-17
jrmu
number 0 is considered false; all other numbers are true. All references
044
2021-12-17
jrmu
are true; undef (for when a variable is undefined) is false.
045
2021-12-17
jrmu
046
2021-12-17
jrmu
This can help save some typing. For example:
047
2021-12-17
jrmu
048
2021-12-17
jrmu
if (scalar(@feeds) == 0) {
049
2021-12-17
jrmu
print "You have no feeds\n";
050
2021-12-17
jrmu
}
051
2021-12-17
jrmu
052
2021-12-17
jrmu
can now be rewritten as:
053
2021-12-17
jrmu
054
2021-12-17
jrmu
if (scalar(@feeds)) {
055
2021-12-17
jrmu
print "You have no feeds\n";
056
2021-12-17
jrmu
}
057
2021-12-17
jrmu
058
2021-12-17
jrmu
================================================================================
059
2021-12-17
jrmu
060
2021-12-17
jrmu
Comparisons
061
2021-12-17
jrmu
062
2021-12-17
jrmu
Perl has different operators for comparing strings and numbers:
063
2021-12-17
jrmu
064
2021-12-17
jrmu
Comparison | String | Numeric
065
2021-12-17
jrmu
--------------+---------------+------------------
066
2021-12-17
jrmu
equal | eq | ==
067
2021-12-17
jrmu
--------------+---------------+------------------
068
2021-12-17
jrmu
not equal | ne | !=
069
2021-12-17
jrmu
--------------+---------------+------------------
070
2021-12-17
jrmu
less than | lt | <
071
2021-12-17
jrmu
--------------+---------------+------------------
072
2021-12-17
jrmu
greater than | gt | >
073
2021-12-17
jrmu
--------------+---------------+------------------
074
2021-12-17
jrmu
less than or | le | <=
075
2021-12-17
jrmu
equal to | |
076
2021-12-17
jrmu
--------------+---------------+------------------
077
2021-12-17
jrmu
greater than | ge | >=
078
2021-12-17
jrmu
or equal to | |
079
2021-12-17
jrmu
--------------+---------------+------------------
080
2021-12-17
jrmu
comparison | cmp | <=>
081
2021-12-17
jrmu
082
2021-12-17
jrmu
For example, if you want to compare if two strings are equal:
083
2021-12-17
jrmu
084
2021-12-17
jrmu
if ($arguments->{who} eq "nickname") {
085
2021-12-17
jrmu
...
086
2021-12-17
jrmu
}
087
2021-12-17
jrmu
088
2021-12-17
jrmu
This checks if the sender's nick is the same as "nickname". If you want
089
2021-12-17
jrmu
to compare if two numbers are equal:
090
2021-12-17
jrmu
091
2021-12-17
jrmu
if ($arguments->{who} eq "nickname") {
092
2021-12-17
jrmu
...
093
2021-12-17
jrmu
}
094
2021-12-17
jrmu
095
2021-12-17
jrmu
The spaceship operator <=> and cmp compares the first scalar to the second.
096
2021-12-17
jrmu
-1 is returned if the first is less than the second, 0 if they are equal,
097
2021-12-17
jrmu
and +1 is the first is greater than the second:
098
2021-12-17
jrmu
099
2021-12-17
jrmu
print $x <=> $y;
100
2021-12-17
jrmu
101
2021-12-17
jrmu
This prints -1 if $x < $y, 0 if $x == $y, and 1 if $x > $y.
102
2021-12-17
jrmu
103
2021-12-17
jrmu
WARNING: Make sure to use the correct operator for strings and numbers.
104
2021-12-17
jrmu
"6" < "1337" is true because the number 6 is less than the number 1337,
105
2021-12-17
jrmu
but "6" lt "1337" is false because the string "1337" comes before "6"
106
2021-12-17
jrmu
when sorted alphabetically. (It's the same reason why the word "bot" comes
107
2021-12-17
jrmu
before the word "computer" in the dictionary.)
108
2021-12-17
jrmu
109
2021-12-17
jrmu
================================================================================
110
2021-12-17
jrmu
111
2021-12-17
jrmu
Logical Operators
112
2021-12-17
jrmu
113
2021-12-17
jrmu
Operator | Operator | Meaning
114
2021-12-17
jrmu
--------------+--------------+-------------------------------------------
115
2021-12-17
jrmu
&& | and | True if both operands are true,
116
2021-12-17
jrmu
| | false otherwise
117
2021-12-17
jrmu
--------------+--------------+-------------------------------------------
118
2021-12-17
jrmu
|| | or | True if either operand is true,
119
2021-12-17
jrmu
| | false otherwise
120
2021-12-17
jrmu
--------------+--------------+-------------------------------------------
121
2021-12-17
jrmu
! | not | False if operand is true,
122
2021-12-17
jrmu
| | true if operand is false
123
2021-12-17
jrmu
--------------+--------------+-------------------------------------------
124
2021-12-17
jrmu
| xor | True if the first or second operand is
125
2021-12-17
jrmu
| | true, false otherwise
126
2021-12-17
jrmu
127
2021-12-17
jrmu
We can use the || or operator for providing default values:
128
2021-12-17
jrmu
129
2021-12-17
jrmu
$var ||= 1;
130
2021-12-17
jrmu
131
2021-12-17
jrmu
This means the same as:
132
2021-12-17
jrmu
133
2021-12-17
jrmu
$var = $var || 1;
134
2021-12-17
jrmu
135
2021-12-17
jrmu
If $var is not defined, then $var is false, and undef || 1 will
136
2021-12-17
jrmu
return the second value, 1. So $var gets the default value of 1
137
2021-12-17
jrmu
if it is undefined.
138
2021-12-17
jrmu
139
2021-12-17
jrmu
================================================================================
140
2021-12-17
jrmu
141
2021-12-17
jrmu
While, Do
142
2021-12-17
jrmu
143
2021-12-17
jrmu
In a while loop, perl checks if CONDITION is true; if so, it executes
144
2021-12-17
jrmu
STATEMENT, then repeats the loop again. If CONDITION is false, it leaves
145
2021-12-17
jrmu
the loop.
146
2021-12-17
jrmu
147
2021-12-17
jrmu
while (CONDITION) {
148
2021-12-17
jrmu
STATEMENT;
149
2021-12-17
jrmu
}
150
2021-12-17
jrmu
151
2021-12-17
jrmu
Here's a sample while loop:
152
2021-12-17
jrmu
153
2021-12-17
jrmu
while (scalar(@feedURLs)) {
154
2021-12-17
jrmu
my $url = pop(@feedURLs);
155
2021-12-17
jrmu
process($url);
156
2021-12-17
jrmu
}
157
2021-12-17
jrmu
158
2021-12-17
jrmu
While there are still urls remaining, we pop the last url from
159
2021-12-17
jrmu
@feedURLs, remove it from the array, and process it.
160
2021-12-17
jrmu
161
2021-12-17
jrmu
================================================================================
162
2021-12-17
jrmu
163
2021-12-17
jrmu
For Loop
164
2021-12-17
jrmu
165
2021-12-17
jrmu
for (INITIALIZE; CONDITION; STEP) {
166
2021-12-17
jrmu
STATEMENT;
167
2021-12-17
jrmu
}
168
2021-12-17
jrmu
169
2021-12-17
jrmu
INITIALIZE is executed only once at the beginning.
170
2021-12-17
jrmu
171
2021-12-17
jrmu
Next, CONDITION is checked. If CONDITION is false, the loop is finished.
172
2021-12-17
jrmu
If CONDITION is true, then STATEMENT is executed, and then STEP.
173
2021-12-17
jrmu
Then the loop repeats itself.
174
2021-12-17
jrmu
175
2021-12-17
jrmu
for (my $i = 0; $i < scalar(@nicks); $i++) {
176
2021-12-17
jrmu
$self->say(
177
2021-12-17
jrmu
channel => "#perl103"
178
2021-12-17
jrmu
body => "Hi, $nick[$i]";
179
2021-12-17
jrmu
);
180
2021-12-17
jrmu
}
181
2021-12-17
jrmu
182
2021-12-17
jrmu
In this code, we first run INITIALIZE: set $i to zero. Then, we check
183
2021-12-17
jrmu
CONDITION: is $i less than the length of the array @nicks? If true,
184
2021-12-17
jrmu
we execute STATEMENT. If not, the loop is finished.
185
2021-12-17
jrmu
186
2021-12-17
jrmu
For STATEMENT, we send a message to the channel #perl103 and say hi to
187
2021-12-17
jrmu
$nick[$i], the nick at index $i in the array. If $i = 0, then we say hi
188
2021-12-17
jrmu
to $nick[0], the first nick. If $i = 1, then we say hi to $nick[1],
189
2021-12-17
jrmu
the second nick.
190
2021-12-17
jrmu
191
2021-12-17
jrmu
In other words, this loop sends a hello message to channel #perl103 to
192
2021-12-17
jrmu
every nick in the array @nicks.
193
2021-12-17
jrmu
194
2021-12-17
jrmu
================================================================================
195
2021-12-17
jrmu
196
2021-12-17
jrmu
Challenge
197
2021-12-17
jrmu
198
2021-12-17
jrmu
View the file ~/challenge to finish the lesson.
199
2021-12-17
jrmu
200
2021-12-17
jrmu
================================================================================
IRCNow