|
This is the first part of the Perl tutorial You are going to learn how to use the Perl 5 programming language to get your job done. You will learn both general language features and extensions or libraries or as the Perl programmers call them "modules". We will see both standard modules, that come with perl and 3rd-party modules, that we install from CPAN. When it is possible I'll try to teach things in a very task oriented way. I'll draw up tasks and then we'll learn the necessary tools to solve them. Where possible I'll also direct you to some exercises you can do to practice what you have learned.
WindowsI'll start by showing how to install Padre, the Perl IDE and Strawberry Perl on your Windows system. Later I'll also give instructions to Linux and Apple users. In order to get started visit the website of Padre and follow the link to the download page. Here you'll find a link to download an installer for Windows that contains both the Perl interpreter and the editor we are going to use. So 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. So for now let's have one single version of Perl installed on your system.
LinuxMost 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 of the editors mentioned later.
AppleI believe Macs also come with Perl or you can easily install it from via the standard installation tools.
Editor and IDEOf course you don't have to use the Padre IDE to write Perl code. In the next lecture I list a couple of editors and IDEs 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 package. It has lots of Perl extensions bundled so it will save you a lot of time down the road.
VideoIf you prefer, you can also watch the Hello world with Perl video I uploaded to YouTube. In that case you might also want to check out the Beginner Perl Maven online video course.
First programYour first program will look like this: use 5.010; use strict; use warnings; say "Hello World"; Let me explain it step-by-step.
Hello worldOnce you installed this Strawberry derivative you can click on "Start -> All programs -> Strawberry Perl -> Padre" which will open the editor with and empty file. Type in print "Hello World\n"; As you can see statements in perl end with a semi-colon. The \n is the sign we used to denote a newline. So 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.
Perl on the command line for non-Padre usersIf you are not using Padre or one of the other IDEs 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: perl hello.pl That's how you can run your script on the command line.
say() instead of print()First of all let's state the minimum version of Perl we would like to use: use 5.010; say "Hello World"; Once you typed this in, you can run the script by selecting "Run -> Run Script" or by pressing F5. 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 say keyword. say is like print but it is shorter and it automatically adds a newline at the end. The current installation you are using is probably version 5.12.3. 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 say() keyword and might need some adjustments to the examples later. I'll point out when I am actully using feature that require version 5.10.
Safety netIn addition in any script I'd strongly recommend to make some further modifications to how perl behaves. For this we add 2 so called pragmas that are very similar to compiler flags in other languages: use 5.010; use strict; use warnings; say "Hello World"; strict and warnings 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.
User InputNow let's further improve our example by asking the user her name and including it in the response. use 5.010; use strict; use warnings; say "What is your name? "; my $name = <STDIN>; say "Hello $name, how are you?"; $name is called a scalar variable. Variables are declared using the my keyword. (actually that's one of the requirements strict adds.) Scalar variables always start with a $ 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 it 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 whan typing in your name, got into the $name variable.
Getting rid of newlinesuse 5.010; use strict; use warnings; say "What is your name? "; my $name = <STDIN>; chomp $name; say "Hello $name, how are you?"; The special function chomp of Perl is there to remove the trailing newlines from strings.
ConclusionIn every script you write you should always add use strict; and use warnings; as the first two statements. It is also very recommended to add use 5.010;.
ExercisesI promised exercises. Try the following script: use strict; use warnings; use 5.010; say "Hello "; say "World"; It did not show on one line. Why? How to fix it?
Exercise 2Write a script that asks the user for two numbers, one after the other. Then print out the sum of the two numbers.
What's next?The next part of the free tutorial about editors, IDEs and development environment for Perl.
Perl tutorial and video courseFor further articles see the Beginner Perl Maven tutorial book and video course.In the comments, please wrap your code snippets within <pre> </pre> tags and use spaces for indentation. blog comments powered by Disqus |
Follow me: