Blob


1 ================================================================================
3 Arrays
5 An array is a list of scalars. The list can include numbers, strings,
6 references, and file handles. Arrays start with the sigil @. For example:
8 my @langs = ("perl", "C", "ksh");
10 Here's the array @langs in table format:
12 Index | Value
13 ------+-------
14 0 | "perl"
15 1 | "C"
16 2 | "ksh"
18 The first element in the array, "perl", has index 0. The second element
19 "C" has index 1, and the third element "ksh" has index 2. Notice that
20 the first element starts at index 0, not 1.
22 To get the first element "perl" in @langs, we write: $langs[0].
23 To get the second element "C" in @langs, we write: $langs[1].
24 To get the third element "ksh" in @langs, we write: $langs[2].
26 Notice that when we retrieve a scalar from an array, the sigil changes to $.
27 If we want to get the entire array, we use the sigil @: @langs.
29 To find the length of an array, use scalar(). scalar(@langs) is equal to 3.
31 ================================================================================
33 Push and Pop
35 push(@array, LIST) lets you add LIST to the end of @array.
37 my @feedURLs;
38 push(@feedURLs, "http://example.com/rss.xml");
40 We add the URL to the end of @feedURLs and increase its length by 1.
42 pop(@array, LIST) gives you the last element and removes it from @array.
44 my $url = pop(@feedURLs);
45 process($url);
47 We grab the last URL from @feedURLs, remove it from the array, and
48 then assign it to $url. Then, we process ($url).
50 ================================================================================
52 foreach Loops
54 foreach my $lang (@langs) {
55 print "Learn $lang, ";
56 }
58 The foreach loop will *iterate* through each element in the array.
59 In this loop, it will get each string in the array @langs, replace
60 $lang with the string, then print "Learn $lang, "
62 The above code will output: Learn perl, Learn C, Learn ksh,
64 ================================================================================
66 Hashes
68 A hash is like an array, except the index is called a key, and this key
69 is a string instead of a number.
71 my %feedURLs = (
72 "undeadly" => "http://undeadly.org/cgi?action=rss",
73 "eff" => "https://www.eff.org/rss/updates.xml",
74 "hackernews" => "https://news.ycombinator.com/rss",
75 );
77 Key | Value
78 -------------+-------------------------------------
79 "undeadly" | "http://undeadly.org/cgi?action=rss"
80 "eff" | "https://www.eff.org/rss/updates.xml"
81 "hackernews" | "https://news.ycombinator.com/rss"
83 A hash contains *key-value pairs* because each key stores a value.
84 A hash is sometimes called a dictionary in other languages, because
85 the key-value pairs are similar to how a dictionary contains
86 terms and their definitions.
88 Unlike an array, the keys of a hash are not numbers, so there is no real
89 order to the key-value pairs.
91 To refer to a hash itself, we use the sigil %. But if we lookup the
92 value of a key, we use a $ because we're referring to a scalar.
93 $feedURLs{undeadly} will give us the value of the key "undeadly"
95 ================================================================================
97 Dumping Data
99 When working with arrays and hashes, you may want to view the data stored
100 inside to help with debugging. We recommend using the module Data::Dumper
101 to dump the contents of arrays and hashes in a structured format:
103 use Data::Dumper <data::Dumper>;
105 Any time you want to dump an array or hash, pass a reference to a
106 hash or array:
108 warn Dumper \@array;
109 warn Dumper \%feedURLs;
111 For example:
113 warn Dumper \%feedURLs;
115 $VAR1 = {
116 'undeadly' => 'http://undeadly.org/cgi?action=rss',
117 'eff' => 'https://www.eff.org/rss/updates.xml',
118 'hackernews' => 'https://news.ycombinator.com/rss',
121 ================================================================================
123 Hashes: Keys and Values
125 We use keys() to get a list of all the keys in a hash.
126 keys(%feedURLs) will give us an array with 3 elements:
127 ('undeadly', 'eff', 'hackernews')
129 Note: keys in the hash have no predictable order to them.
131 We use values() to get a list of all the values in a hash.
132 The values will match the same order as the keys. So
133 values(%feedURLs) will give us an array with 3 elements:
134 ('http://undeadly.org/cgi?action=rss',
135 'https://www.eff.org/rss/updates.xml',
136 'https://news.ycombinator.com/rss')
138 ================================================================================
140 News Bot
142 Open up ~/thirdbot and follow the instructions to set up your third bot.
144 ================================================================================