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
Scalars
004
2021-12-17
jrmu
005
2021-12-17
jrmu
A scalar can store a string, number, reference (which points to another
006
2021-12-17
jrmu
variable), or a file handle (which lets you read and write to a file).
007
2021-12-17
jrmu
008
2021-12-17
jrmu
my $server = "irc.example.com";
009
2021-12-17
jrmu
my $port = 6667;
010
2021-12-17
jrmu
011
2021-12-17
jrmu
The first scalar $server contains the string "irc.example.com". A string
012
2021-12-17
jrmu
is made up of letters. The second scalar $port contains the number 6667.
013
2021-12-17
jrmu
014
2021-12-17
jrmu
================================================================================
015
2021-12-17
jrmu
016
2021-12-17
jrmu
Strings
017
2021-12-17
jrmu
018
2021-12-17
jrmu
If you want to use a literal string, make sure to put it around 'single
019
2021-12-17
jrmu
quotes' or "double quotes" -- if you do not, perl will report an error:
020
2021-12-17
jrmu
021
2021-12-17
jrmu
print Welcome to my IRC channel;
022
2021-12-17
jrmu
023
2021-12-17
jrmu
syntax error at - line 1, near "my IRC channel"
024
2021-12-17
jrmu
Execution of - aborted due to compilation errors.
025
2021-12-17
jrmu
026
2021-12-17
jrmu
When you use "double quotes", perl will evaluate any variables inside the
027
2021-12-17
jrmu
string and replace them with their values.
028
2021-12-17
jrmu
029
2021-12-17
jrmu
my $nick = "perlmonk";
030
2021-12-17
jrmu
my $msg = "Welcome to the channel, $nick!";
031
2021-12-17
jrmu
032
2021-12-17
jrmu
The perl interpreter replaces $nick with the string "perlmonk", so that
033
2021-12-17
jrmu
$msg will contain the string "Welcome to the channel, perlmonk!" This
034
2021-12-17
jrmu
is called *string interpolation*.
035
2021-12-17
jrmu
036
2021-12-17
jrmu
When you use 'single quotes', no string interpolation takes place. The
037
2021-12-17
jrmu
string is identical to what you type.
038
2021-12-17
jrmu
039
2021-12-17
jrmu
my $msg = 'Welcome to the channel, $nick!';
040
2021-12-17
jrmu
041
2021-12-17
jrmu
The scalar $msg will literally contain 'Welcome to the channel, $nick!'
042
2021-12-17
jrmu
and not 'Welcome to the channel, perlmonk!' This can be useful to
043
2021-12-17
jrmu
avoid accidental string interpolation:
044
2021-12-17
jrmu
045
2021-12-17
jrmu
my $msg = 'Just call $mrmoney and send an email to @cash.com!';
046
2021-12-17
jrmu
047
2021-12-17
jrmu
If we had used double quotes above, perl would try to replace $mrmoney
048
2021-12-17
jrmu
and @cash with the value of those variables. We use single quotes
049
2021-12-17
jrmu
to keep them literal.
050
2021-12-17
jrmu
051
2021-12-17
jrmu
If you need to start a new line in your string, you can use "\n":
052
2021-12-17
jrmu
053
2021-12-17
jrmu
print "To start, press any key.\nWhere's the 'Any' key?"
054
2021-12-17
jrmu
055
2021-12-17
jrmu
This will print:
056
2021-12-17
jrmu
057
2021-12-17
jrmu
To start, press any key.
058
2021-12-17
jrmu
Where's the 'Any' key?
059
2021-12-17
jrmu
060
2021-12-17
jrmu
================================================================================
061
2021-12-17
jrmu
062
2021-12-17
jrmu
Numbers
063
2021-12-17
jrmu
064
2021-12-17
jrmu
There are two number types: integers and floats. An integer is a number
065
2021-12-17
jrmu
with no decimals or fractions. A float is stored as a decimal or fraction.
066
2021-12-17
jrmu
067
2021-12-17
jrmu
my $port = 6697;
068
2021-12-17
jrmu
my $delay = 3.5;
069
2021-12-17
jrmu
070
2021-12-17
jrmu
$port is the integer 6697 but $delay might last for three and a half
071
2021-12-17
jrmu
seconds.
072
2021-12-17
jrmu
073
2021-12-17
jrmu
================================================================================
074
2021-12-17
jrmu
075
2021-12-17
jrmu
References
076
2021-12-17
jrmu
077
2021-12-17
jrmu
A reference points to another variable, like a pointer in C. The reference
078
2021-12-17
jrmu
does not store the data itself, but stores the address for the data you want.
079
2021-12-17
jrmu
080
2021-12-17
jrmu
my $channel = "#perl102";
081
2021-12-17
jrmu
my $channel_ref = \$channel;
082
2021-12-17
jrmu
083
2021-12-17
jrmu
$channel_ref now points to $channel, but does not contain the string
084
2021-12-17
jrmu
"#perl102" itself.
085
2021-12-17
jrmu
086
2021-12-17
jrmu
To get the value of this reference, put an extra $ in front of the
087
2021-12-17
jrmu
reference:
088
2021-12-17
jrmu
089
2021-12-17
jrmu
print "Welcome to $$channel_ref, new user!\n";
090
2021-12-17
jrmu
091
2021-12-17
jrmu
References may not seem very useful now, but we will use them frequently
092
2021-12-17
jrmu
when programming in Perl.
093
2021-12-17
jrmu
094
2021-12-17
jrmu
================================================================================
095
2021-12-17
jrmu
096
2021-12-17
jrmu
Filehandles
097
2021-12-17
jrmu
098
2021-12-17
jrmu
We can call open and pass it a scalar and a filename to open a file:
099
2021-12-17
jrmu
100
2021-12-17
jrmu
open(my $filehandle, '>log.txt');
101
2021-12-17
jrmu
102
2021-12-17
jrmu
The greater than > sign means we want to redirect output to log.txt.
103
2021-12-17
jrmu
Perl will store the handle to the file in the scalar. We can then print
104
2021-12-17
jrmu
to $filehandle to write to the text file:
105
2021-12-17
jrmu
106
2021-12-17
jrmu
print $filehandle "12:56 -!- nickname [nick@10.0.0.1] has joined #channel";
107
2021-12-17
jrmu
print $filehandle "21:28 -!- mode/#channel [+o nickname] by you";
108
2021-12-17
jrmu
close($filehandle);
109
2021-12-17
jrmu
110
2021-12-17
jrmu
Once you are done with a filehandle, make sure to close it. We now have IRC
111
2021-12-17
jrmu
logs written to log.txt which we can read later.
112
2021-12-17
jrmu
113
2021-12-17
jrmu
================================================================================
114
2021-12-17
jrmu
115
2021-12-17
jrmu
Learn more about Perl
116
2021-12-17
jrmu
117
2021-12-17
jrmu
At any time, you can get more information by using perldoc:
118
2021-12-17
jrmu
119
2021-12-17
jrmu
$ perldoc perldoc
120
2021-12-17
jrmu
121
2021-12-17
jrmu
Websites to learn more about Perl:
122
2021-12-17
jrmu
123
2021-12-17
jrmu
http://learn.perl.org
124
2021-12-17
jrmu
125
2021-12-17
jrmu
http://www.perldoc.com
126
2021-12-17
jrmu
127
2021-12-17
jrmu
================================================================================
128
2021-12-17
jrmu
129
2021-12-17
jrmu
Auto Greet Bot
130
2021-12-17
jrmu
131
2021-12-17
jrmu
Open up ~/secondbot and follow the instructions to set up your second bot.
132
2021-12-17
jrmu
133
2021-12-17
jrmu
================================================================================
IRCNow