Probably the easiest way to get started with Perl is to download a package containing both the perl interpreter and an Integrated Development Environment (IDE) called Padre from the Padre website.
To avoid any confusion, let me point out that Padre stands for "Perl Application Development And Refactoring Environment". In reality, it is just a text editor with an over-sized ego.
Padre is just one way to create and edit your Perl scripts. You can use any text editor. If you prefer to use vim, emacs, Notepad++, Eclipse or any other text editor that's fine as well. In that case though you will likely need to run your perl script from the command line.
If you are using Windows you can download the Padre-on-Strawberry package, which contains Strawberry Perl, a free distribution of the Perl compiler; Padre, the Perl IDE; and a number of other libraries (called modules in Perl-speak) that will be useful.
Once you've downloaded the installer, do the regular installation process by clicking through a few screens.
If you are a Linux user, you most likely will already have Perl installed. In most modern Linux distributions you can install Padre as well from the repository of the distribution:
sudo apt-get install padre
yum install perl-Padre
aptitude install padre
Then you should be able to run Padre by just typing "padre" on the command line.
After this the instructions are the same as in Windows.
For Mac OSX the Padre package is still experimental. Visit the Padre download page and follow the instructions listed there.
At the end you'll have a menu item in the Start menu of Windows called "Strawberry Perl". In that menu you will have an entry "Padre, the Perl IDE".
That's what you need to launch.
In the editor that opens type in the following simple code:
print "Hello I-Programmer\n";
(Click to expand)
Save the file with .pl extension, e.g. call it hw.pl.
Then you can run the program using the "Run -> Run Script" menu option which is mapped to the F5 key.
You have written your first Perl program:
(Click to expand)
In the above code you can see the "print" statement that is used to print on the screen. It is followed by a string - a bunch of characters surrounded by quotation marks. This is the parameter of the print statement. The whole thing is followed by a semi-colon. Statements in Perl are always separated by semi-colons.
The \n tells perl to print a newline. Add \n just after the word "Hello" and run the script again to see how that works.
Hello World with a safety net
While the previous example is fully functional, let me show you the full code I'd write if I was trying to show you modern Perl code:
say "Hello I-Programmer";
Here we have four extra lines there and instead of the keyword "print" we are using "say".
The first line ( #!/usr/bin/perl ) is called the she-bang or sh-bang. It is important mostly on Linux and Unix systems and is a nice inclusion but not a critical one
use strict turns on various compiler flags that will catch a number of common programmer errors. Some of them will be caught even before your script starts to run.
use warnings turns on further compiler flags that will give you various warnings during the run-time of your script for another set of common programmer errors.
These two I call a "safety net". I would not write any code without them.
You might ask, if they are so important then why are they not "always on" and the answer is simple. Let's just say "for historical reasons".
The line use v5.10 declares that you need to run this code with a perl which has a version number 5.10 or higher.
In fact on most modern Linux systems you will already find such perl and if you followed my advice on Windows you will have installed version 5.12.
In general it is a good practice to declare the minimum version number of perl your script needs. That will return a helpful error message if someone is trying to run your script on an older version of perl instead of some cryptic error when the old perl does not understand a newer construct.
In our case we have another reason to require at least version 5.10. The above statement includes new features that are only available in 5.10 or later, specifically the "say" keyword.
It does the same as "print" but it also prints a newline at the end of the output. This is shorter and neater, especially when you include simple debugging statements. So now you understand why I left out the \n in this example.
In the next example we try to be a bit more personal and greet the visitor by name.
my $name = "Foo";
say "Hello $name, welcome to I-Programmer";
In line 6 we declared a scalar variable $name and assigned a string to it containing the word "Foo".
In Perl there are basically three variable types.
The scalar variables always start with a $ sign and they can hold either numbers or strings. We declare the variables with the "my" keyword indicating that they are my private variables.
We could display the content of the variable by writing
but here we want to put the name within the greeting message and so we've embedded the variable in a string.
People coming from many other languages might find this strange but it is very useful and can make the code a lot more readable.
In the above code perl will print the content of the $name variable.
Getting input from the user
Unfortunately - for the programmer - not everyone is called Foo.
We had better ask the user for their name before saying hi.
print "Enter your name please: ";
my $name = <STDIN>;
say "Hello $name, welcome to I-Programmer";
In line 5 we print out our question. This time we are using the "print" statement as we would like to allow the user to type in the answer on the same line where the question was printed.
Then we declare the variable and use the <STDIN> operator to read from the STanDard INput (normally your keyboard) until you press ENTER.
Everything you typed, including the newline will land in the $name variable.
As we don't want that trailing newline we use the "chomp" function of Perl to get rid of it.
Then we can print the greeting as previously.
Take the above code, copy it to the editor and run it. Then comment out the chomp line by putting a # mark at the beginning:
# chomp $name;
and run it again.
You will see that while previously the result was:
Hello Bar, welcome to I-Programmer
after eliminating the chomp, and thus keeping the newline, you get:
, welcome to I-Programmer
which, you have to admit, is unsatisfactory and makes it worth the effort of including the chomp function.
If you can't wait for the next article by Gábor Szabó you can follow the Perl Tutorial on his website.
Gábor has recently lauched video courses:
Beginner Perl Maven - 2 parts
Advanced Perl Maven