As, in all programming languages, time to make a “Hello World” to consolidate our knowledge in the Perl language.
Installing Perl
First, we will need to install the Perl language.
If you're using Linux or Mac OS X, don't worry, the Perl interpreter is installed in almost every distro by default.
If you are using Windows, you will have to install the language, so let's go. Go to the language's official website, and click Downloads. There you will see two download links (the Strawberry and ActiveState versions). Download the Strawberry version, which is free and open source.
Now just run as administrator, install it on the system, and that's it, you have the most up-to-date version of the Perl language on your Windows machine.
Your first Perl program
Open your favorite editor. It may be the notepad that comes with Windows, but I recommend one that has syntax highlight, like Notepad++ or [Sublime Text](https:/ /www.sublimetext.com/). I, for example, use VS Code. Inside the editor, write the following command:
print "Hello, world \n";
And save the file with the name hello.pl
. It is important that it has the .pl
extension, as this will make the computer understand that it is a Perl script. Be careful, as some editors may want to save as hello.pl.txt
.
For standardization purposes, for the rest of the tutorial I will assume that the script is saved in C:\perl
on Windows and ~/perl
on Linux / Mac OS X.
Running your script
Open your command prompt or terminal and go to the folder where you saved the script. If on Windows, type:
cd C:\perl
If you are on Linux or Mac OS X, type:
cd ~/perl
And now just run the script:
perl hello.pl
And ready. The result will be similar to this:
On Windows, the script may not run with the default command. In that case, try with the following command:
perl -w hello.pl
Hash-Bang
If you are using a Unix system, you might find it an interesting idea to turn your Perl script into an executable. Lets do this?
Start by giving your script hello.pl
execute permission:
chmod +x hello.pl
Now run:
./hello.pl
You will see that the script failed. This is because the terminal will always try to run scripts with the default shell (usually Bash or ZSH). To get around this, we can put a special comment at the beginning of the file, which will say which interpreter will be used. In our case, the script will look like this:
#!/usr/bin/perl
print "Hello, World!\n";
This initial line says that the interpreter used is saved in the /usr/bin/perl
folder. There are other options like /bin/perl
(in case the user's Perl version is not the same as the global version). I, personally, prefer to do it this way, so I don't have to always type the command to run the program with the interpreter I want.
So, did you find it useful? Did I miss something? Tell me in the comments 😊