1 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).
8 8f7f2f4a 2021-12-17 jrmu my $server = "irc.example.com";
9 8f7f2f4a 2021-12-17 jrmu my $port = 6667;
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.
14 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:
21 8f7f2f4a 2021-12-17 jrmu print Welcome to my IRC channel;
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.
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.
29 8f7f2f4a 2021-12-17 jrmu my $nick = "perlmonk";
30 8f7f2f4a 2021-12-17 jrmu my $msg = "Welcome to the channel, $nick!";
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*.
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.
39 8f7f2f4a 2021-12-17 jrmu my $msg = 'Welcome to the channel, $nick!';
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:
45 8f7f2f4a 2021-12-17 jrmu my $msg = 'Just call $mrmoney and send an email to @cash.com!';
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.
51 8f7f2f4a 2021-12-17 jrmu If you need to start a new line in your string, you can use "\n":
53 8f7f2f4a 2021-12-17 jrmu print "To start, press any key.\nWhere's the 'Any' key?"
55 8f7f2f4a 2021-12-17 jrmu This will print:
57 8f7f2f4a 2021-12-17 jrmu To start, press any key.
58 8f7f2f4a 2021-12-17 jrmu Where's the 'Any' key?
60 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.
67 8f7f2f4a 2021-12-17 jrmu my $port = 6697;
68 8f7f2f4a 2021-12-17 jrmu my $delay = 3.5;
70 8f7f2f4a 2021-12-17 jrmu $port is the integer 6697 but $delay might last for three and a half
73 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.
80 8f7f2f4a 2021-12-17 jrmu my $channel = "#perl102";
81 8f7f2f4a 2021-12-17 jrmu my $channel_ref = \$channel;
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.
86 8f7f2f4a 2021-12-17 jrmu To get the value of this reference, put an extra $ in front of the
89 8f7f2f4a 2021-12-17 jrmu print "Welcome to $$channel_ref, new user!\n";
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.
94 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:
100 8f7f2f4a 2021-12-17 jrmu open(my $filehandle, '>log.txt');
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:
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);
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.
113 8f7f2f4a 2021-12-17 jrmu ================================================================================
115 8f7f2f4a 2021-12-17 jrmu Learn more about Perl
117 8f7f2f4a 2021-12-17 jrmu At any time, you can get more information by using perldoc:
119 8f7f2f4a 2021-12-17 jrmu $ perldoc perldoc
121 8f7f2f4a 2021-12-17 jrmu Websites to learn more about Perl:
123 8f7f2f4a 2021-12-17 jrmu http://learn.perl.org
125 8f7f2f4a 2021-12-17 jrmu http://www.perldoc.com
127 8f7f2f4a 2021-12-17 jrmu ================================================================================
129 8f7f2f4a 2021-12-17 jrmu Auto Greet Bot
131 8f7f2f4a 2021-12-17 jrmu Open up ~/secondbot and follow the instructions to set up your second bot.
133 8f7f2f4a 2021-12-17 jrmu ================================================================================