Blob


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