Blame


1 8f7f2f4a 2021-12-17 jrmu ================================================================================
2 8f7f2f4a 2021-12-17 jrmu
3 8f7f2f4a 2021-12-17 jrmu Scalars
4 8f7f2f4a 2021-12-17 jrmu
5 8f7f2f4a 2021-12-17 jrmu A scalar can store a string, number, reference (which points to another
6 8f7f2f4a 2021-12-17 jrmu variable), or a file handle (which lets you read and write to a file).
7 8f7f2f4a 2021-12-17 jrmu
8 8f7f2f4a 2021-12-17 jrmu my $server = "irc.example.com";
9 8f7f2f4a 2021-12-17 jrmu my $port = 6667;
10 8f7f2f4a 2021-12-17 jrmu
11 8f7f2f4a 2021-12-17 jrmu The first scalar $server contains the string "irc.example.com". A string
12 8f7f2f4a 2021-12-17 jrmu is made up of letters. The second scalar $port contains the number 6667.
13 8f7f2f4a 2021-12-17 jrmu
14 8f7f2f4a 2021-12-17 jrmu ================================================================================
15 8f7f2f4a 2021-12-17 jrmu
16 8f7f2f4a 2021-12-17 jrmu Strings
17 8f7f2f4a 2021-12-17 jrmu
18 8f7f2f4a 2021-12-17 jrmu If you want to use a literal string, make sure to put it around 'single
19 8f7f2f4a 2021-12-17 jrmu quotes' or "double quotes" -- if you do not, perl will report an error:
20 8f7f2f4a 2021-12-17 jrmu
21 8f7f2f4a 2021-12-17 jrmu print Welcome to my IRC channel;
22 8f7f2f4a 2021-12-17 jrmu
23 8f7f2f4a 2021-12-17 jrmu syntax error at - line 1, near "my IRC channel"
24 8f7f2f4a 2021-12-17 jrmu Execution of - aborted due to compilation errors.
25 8f7f2f4a 2021-12-17 jrmu
26 8f7f2f4a 2021-12-17 jrmu When you use "double quotes", perl will evaluate any variables inside the
27 8f7f2f4a 2021-12-17 jrmu string and replace them with their values.
28 8f7f2f4a 2021-12-17 jrmu
29 8f7f2f4a 2021-12-17 jrmu my $nick = "perlmonk";
30 8f7f2f4a 2021-12-17 jrmu my $msg = "Welcome to the channel, $nick!";
31 8f7f2f4a 2021-12-17 jrmu
32 8f7f2f4a 2021-12-17 jrmu The perl interpreter replaces $nick with the string "perlmonk", so that
33 8f7f2f4a 2021-12-17 jrmu $msg will contain the string "Welcome to the channel, perlmonk!" This
34 8f7f2f4a 2021-12-17 jrmu is called *string interpolation*.
35 8f7f2f4a 2021-12-17 jrmu
36 8f7f2f4a 2021-12-17 jrmu When you use 'single quotes', no string interpolation takes place. The
37 8f7f2f4a 2021-12-17 jrmu string is identical to what you type.
38 8f7f2f4a 2021-12-17 jrmu
39 8f7f2f4a 2021-12-17 jrmu my $msg = 'Welcome to the channel, $nick!';
40 8f7f2f4a 2021-12-17 jrmu
41 8f7f2f4a 2021-12-17 jrmu The scalar $msg will literally contain 'Welcome to the channel, $nick!'
42 8f7f2f4a 2021-12-17 jrmu and not 'Welcome to the channel, perlmonk!' This can be useful to
43 8f7f2f4a 2021-12-17 jrmu avoid accidental string interpolation:
44 8f7f2f4a 2021-12-17 jrmu
45 8f7f2f4a 2021-12-17 jrmu my $msg = 'Just call $mrmoney and send an email to @cash.com!';
46 8f7f2f4a 2021-12-17 jrmu
47 8f7f2f4a 2021-12-17 jrmu If we had used double quotes above, perl would try to replace $mrmoney
48 8f7f2f4a 2021-12-17 jrmu and @cash with the value of those variables. We use single quotes
49 8f7f2f4a 2021-12-17 jrmu to keep them literal.
50 8f7f2f4a 2021-12-17 jrmu
51 8f7f2f4a 2021-12-17 jrmu If you need to start a new line in your string, you can use "\n":
52 8f7f2f4a 2021-12-17 jrmu
53 8f7f2f4a 2021-12-17 jrmu print "To start, press any key.\nWhere's the 'Any' key?"
54 8f7f2f4a 2021-12-17 jrmu
55 8f7f2f4a 2021-12-17 jrmu This will print:
56 8f7f2f4a 2021-12-17 jrmu
57 8f7f2f4a 2021-12-17 jrmu To start, press any key.
58 8f7f2f4a 2021-12-17 jrmu Where's the 'Any' key?
59 8f7f2f4a 2021-12-17 jrmu
60 8f7f2f4a 2021-12-17 jrmu ================================================================================
61 8f7f2f4a 2021-12-17 jrmu
62 8f7f2f4a 2021-12-17 jrmu Numbers
63 8f7f2f4a 2021-12-17 jrmu
64 8f7f2f4a 2021-12-17 jrmu There are two number types: integers and floats. An integer is a number
65 8f7f2f4a 2021-12-17 jrmu with no decimals or fractions. A float is stored as a decimal or fraction.
66 8f7f2f4a 2021-12-17 jrmu
67 8f7f2f4a 2021-12-17 jrmu my $port = 6697;
68 8f7f2f4a 2021-12-17 jrmu my $delay = 3.5;
69 8f7f2f4a 2021-12-17 jrmu
70 8f7f2f4a 2021-12-17 jrmu $port is the integer 6697 but $delay might last for three and a half
71 8f7f2f4a 2021-12-17 jrmu seconds.
72 8f7f2f4a 2021-12-17 jrmu
73 8f7f2f4a 2021-12-17 jrmu ================================================================================
74 8f7f2f4a 2021-12-17 jrmu
75 8f7f2f4a 2021-12-17 jrmu References
76 8f7f2f4a 2021-12-17 jrmu
77 8f7f2f4a 2021-12-17 jrmu A reference points to another variable, like a pointer in C. The reference
78 8f7f2f4a 2021-12-17 jrmu does not store the data itself, but stores the address for the data you want.
79 8f7f2f4a 2021-12-17 jrmu
80 8f7f2f4a 2021-12-17 jrmu my $channel = "#perl102";
81 8f7f2f4a 2021-12-17 jrmu my $channel_ref = \$channel;
82 8f7f2f4a 2021-12-17 jrmu
83 8f7f2f4a 2021-12-17 jrmu $channel_ref now points to $channel, but does not contain the string
84 8f7f2f4a 2021-12-17 jrmu "#perl102" itself.
85 8f7f2f4a 2021-12-17 jrmu
86 8f7f2f4a 2021-12-17 jrmu To get the value of this reference, put an extra $ in front of the
87 8f7f2f4a 2021-12-17 jrmu reference:
88 8f7f2f4a 2021-12-17 jrmu
89 8f7f2f4a 2021-12-17 jrmu print "Welcome to $$channel_ref, new user!\n";
90 8f7f2f4a 2021-12-17 jrmu
91 8f7f2f4a 2021-12-17 jrmu References may not seem very useful now, but we will use them frequently
92 8f7f2f4a 2021-12-17 jrmu when programming in Perl.
93 8f7f2f4a 2021-12-17 jrmu
94 8f7f2f4a 2021-12-17 jrmu ================================================================================
95 8f7f2f4a 2021-12-17 jrmu
96 8f7f2f4a 2021-12-17 jrmu Filehandles
97 8f7f2f4a 2021-12-17 jrmu
98 8f7f2f4a 2021-12-17 jrmu We can call open and pass it a scalar and a filename to open a file:
99 8f7f2f4a 2021-12-17 jrmu
100 8f7f2f4a 2021-12-17 jrmu open(my $filehandle, '>log.txt');
101 8f7f2f4a 2021-12-17 jrmu
102 8f7f2f4a 2021-12-17 jrmu The greater than > sign means we want to redirect output to log.txt.
103 8f7f2f4a 2021-12-17 jrmu Perl will store the handle to the file in the scalar. We can then print
104 8f7f2f4a 2021-12-17 jrmu to $filehandle to write to the text file:
105 8f7f2f4a 2021-12-17 jrmu
106 8f7f2f4a 2021-12-17 jrmu print $filehandle "12:56 -!- nickname [nick@10.0.0.1] has joined #channel";
107 8f7f2f4a 2021-12-17 jrmu print $filehandle "21:28 -!- mode/#channel [+o nickname] by you";
108 8f7f2f4a 2021-12-17 jrmu close($filehandle);
109 8f7f2f4a 2021-12-17 jrmu
110 8f7f2f4a 2021-12-17 jrmu Once you are done with a filehandle, make sure to close it. We now have IRC
111 8f7f2f4a 2021-12-17 jrmu logs written to log.txt which we can read later.
112 8f7f2f4a 2021-12-17 jrmu
113 8f7f2f4a 2021-12-17 jrmu ================================================================================
114 8f7f2f4a 2021-12-17 jrmu
115 8f7f2f4a 2021-12-17 jrmu Learn more about Perl
116 8f7f2f4a 2021-12-17 jrmu
117 8f7f2f4a 2021-12-17 jrmu At any time, you can get more information by using perldoc:
118 8f7f2f4a 2021-12-17 jrmu
119 8f7f2f4a 2021-12-17 jrmu $ perldoc perldoc
120 8f7f2f4a 2021-12-17 jrmu
121 8f7f2f4a 2021-12-17 jrmu Websites to learn more about Perl:
122 8f7f2f4a 2021-12-17 jrmu
123 8f7f2f4a 2021-12-17 jrmu http://learn.perl.org
124 8f7f2f4a 2021-12-17 jrmu
125 8f7f2f4a 2021-12-17 jrmu http://www.perldoc.com
126 8f7f2f4a 2021-12-17 jrmu
127 8f7f2f4a 2021-12-17 jrmu ================================================================================
128 8f7f2f4a 2021-12-17 jrmu
129 8f7f2f4a 2021-12-17 jrmu Auto Greet Bot
130 8f7f2f4a 2021-12-17 jrmu
131 8f7f2f4a 2021-12-17 jrmu Open up ~/secondbot and follow the instructions to set up your second bot.
132 8f7f2f4a 2021-12-17 jrmu
133 8f7f2f4a 2021-12-17 jrmu ================================================================================