forked from szabgab/perlmaven.com
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathinstalling-perl-and-getting-started.tt
260 lines (178 loc) · 7.59 KB
/
installing-perl-and-getting-started.tt
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
=title Installing and getting started with Perl
=timestamp 2012-08-08T10:45:56
=indexes strict, warnings, say, print, chomp, scalar, $
=status show
=books beginner_book
=author szabgab
=index 1
=archive 1
=feed 1
=comments 1
=social 1
=abstract start
This is the first part of the <a href="/perl-tutorial">Perl tutorial</a>.
In this part you will learn how to install Perl on Microsoft Windows and how to start
using it on Windows, Linux or on a Mac.
You'll get directions to set up your development environment, or in less grandious words:
which editor or IDE to use for writing Perl?
We will also see the standard "Hello World" example.
=abstract end
<h2>Windows</h2>
For Windows we are going to use <a href="http://dwimperl.com/">DWIM Perl</a>. It is a package
that contains the Perl compiler/interpreter, <a href="http://padre.perlide.org/">Padre, the Perl IDE</a>
and a number of extensions from CPAN.
In order to get started visit the website of <a href="http://dwimperl.com/">DWIM Perl</a>
and follow the link to download <b>DWIM Perl for Windows</b>.
Go ahead, download the exe file and install it on your system. Before doing so,
please make sure you don't have any other Perl installated.
They could work together but that would require some more explanations.
For now let's have one single version of Perl installed on your system.
<h2>Linux</h2>
Most Linux modern Linux distributions come with a recent version of Perl.
For now we are going to use that version of Perl. For editor,
you can install Padre - most Linux distribution offer it from their
official package management system. Otherwise you can pick any regular text editor.
If you are familiar with vim or Emacs, use the one you like. Otherwise
Gedit might be a good simple editor.
<h2>Apple</h2>
I believe Macs also come with Perl or you can easily install it via
the standard installation tools.
<h2>Editor and IDE</h2>
Even though it is recommended, you don't have to use the Padre IDE to write Perl code.
In the next part I'll list a couple of <a href="/perl-editor">editors and IDEs</a> you
can use for your Perl programming. Even if you select another editor
I'd recommend - for the Windows users - to install the above mentioned DWIM Perl package.
It has lots of Perl extensions bundled so it will save you a lot of time down the road.
<h2>Video</h2>
If you prefer, you can also watch the
<a href="http://www.youtube.com/watch?v=c3qzmJsR2H0">Hello world with Perl</a>
video I uploaded to YouTube. In that case you might also want to check
out the <a href="/beginner-perl-maven-video-course">Beginner Perl Maven video course</a>.
<h2>First program</h2>
Your first program will look like this:
<code lang="perl">
use 5.010;
use strict;
use warnings;
say "Hello World";
</code>
Let me explain it step-by-step.
<h2>Hello world</h2>
Once you installed DWIM Perl you can click on
"Start -> All programs -> DWIM Perl -> Padre" which will open the editor
with and empty file.
Type in
<code lang="perl">
print "Hello World\n";
</code>
As you can see statements in perl end with a semi-colon <hl>;</hl>.
The <hl>\n</hl> is the sign we used to denote a newline.
Strings are enclosed in double-quotes <hl>"</hl>.
The <hl>print</hl> function prints to the screen.
When this is executed perl will print the text and at the end it will print a newline.
Save the file as hello.pl and then you can run the code by selecting "Run -> Run Script"
You will see a separate window showing up with the output.
That's it, you wrote your first perl script.
Let's enhance it a bit.
<h2>Perl on the command line for non-Padre users</h2>
If you are not using Padre or one of the other <a href="/perl-editor">IDEs</a>
you won't be able to run your script from the editor itself.
At least not by default. You will need to open a shell
(or cmd in Windows), change to the directory where you saved the hello.pl
file and type in:
<hl>perl hello.pl</hl>
That's how you can run your script on the command line.
<h2>say() instead of print()</h2>
Let's improve our one-line Perl script a bit:
First of all let's state the minimum version of Perl we would like to use:
<code lang="perl">
use 5.010;
print "Hello World\n";
</code>
Once you typed this in, you can run the script again by selecting
"Run -> Run Script" or by pressing <b>F5</b>.
That will automatically save the file before running it.
It is generally a good practice to tell what is the minimum version of perl your code requires.
In this case it also adds a few new features to perl including the <hl>say</hl> keyword.
<hl>say</hl> is like <hl>print</hl> but it is shorter and it
automatically adds a newline at the end.
You can change your code like this:
<code lang="perl">
use 5.010;
say "Hello World";
</code>
We replaced <hl>print</hl> by <hl>say</hl> and remove the <hl>\n</hl> from the end of the string.
The current installation you are using is probably version 5.12.3 or 5.14.
Most modern Linux distributions come with version 5.10 or newer.
Unfortunately there are still places using older versions of perl.
Those won't be able to use the <hl>say()</hl> keyword and might need some adjustments
to the examples later. I'll point out when I am actully using features
that require version 5.10.
<h2>Safety net</h2>
In addition in every script I'd strongly recommend to make some further modifications to
the behavior of Perl. For this we add 2, so called pragmatas, that are very similar to compiler flags
in other languages:
<code lang="perl">
use 5.010;
use strict;
use warnings;
say "Hello World";
</code>
In this case the <hl>use</hl> keyword tells perl to load and enable each pragma.
<hl>strict</hl> and <hl>warnings</hl> will help you catch some common bugs
in your code or sometimes even prevent you from making them in the first place.
They are very handy.
<h2>User Input</h2>
Now let's improve our example by asking the user her name and including
it in the response.
<code lang="perl">
use 5.010;
use strict;
use warnings;
say "What is your name? ";
my $name = <STDIN>;
say "Hello $name, how are you?";
</code>
<hl>$name</hl> is called a scalar variable.
Variables are declared using the <b>my</b> keyword.
(actually that's one of the requirements <hl>strict</hl> adds.)
Scalar variables always start with a <hl>$</hl> sign.
The <STDIN> is the tool to read a line from the keyboard.
Type in the above and run it by pressing F5.
It will ask for your name. Type in your name and press ENTER to let perl know
you have finished typing in your name.
You will notice that the output is a bit broken: The comma after
the name appears on a newline. That's because the ENTER you pressed, when typing in your name,
got into the <hl>$name</hl> variable.
<h2>Getting rid of newlines</h2>
<code lang="perl">
use 5.010;
use strict;
use warnings;
say "What is your name? ";
my $name = <STDIN>;
chomp $name;
say "Hello $name, how are you?";
</code>
It is such a common task in Perl, that there is a special function called <hl>chomp</hl>
to remove the trailing newlines from strings.
<h2>Conclusion</h2>
In every script you write you should <b>always</b> add <hl>use strict;</hl> and <hl>use warnings;</hl>
as the first two statements. It is also very recommended to add <hl>use 5.010;</hl>.
<h2>Exercises</h2>
I promised exercises.
Try the following script:
<code lang="perl">
use strict;
use warnings;
use 5.010;
say "Hello ";
say "World";
</code>
It did not show on one line. Why? How to fix it?
<h2>Exercise 2</h2>
Write a script that asks the user for two numbers, one after the other.
Then prints out the sum of the two numbers.
<h2>What's next?</h2>
The next part of the tutorial is about
<a href="/perl-editor">editors, IDEs and development environment for Perl</a>.