Summary: in this tutorial, you’ll learn how to develop the simple but famous Perl program called Hello, World!
First, create a new directory called helloworld
and launch the code editor.
Second, create a new file called hello.pl
in helloworld
directory and copy&paste the following code:
#!/usr/bin/perl
use warnings;
print("Hello, World!\n");
Code language: Perl (perl)
Third, save the file. Note that the extension of a Perl source code file is pl
.
Finally, launch the Command Prompt on Windows or Terminal on macOS and Linux, and type the following command to execute the Perl program:
perl path_to_perl_file
Code language: Shell Session (shell)
For example, on Windows, you can type the following command:
D:\Perl\helloworld>perl hello.pl
Code language: Shell Session (shell)
If you see the output of the program like this:
Hello, World!
Code language: Shell Session (shell)
… then congratulation, you’ve successfully developed the first Perl program.
Hello, World! program in detail
Let’s examine the program line by line, one at a time, to understand what it does.
The first line of the program is:
#!/usr/bin/perl
Code language: Perl (perl)
The first line begins with a pair of characters #!
. It tells the shell to execute the script with the Perl interpreter.
In this case, the file should be transferred to the Perl interpreter that resides in /usr/bin/perl
folder.
The second line of the program is:
use warnings;
Code language: Perl (perl)
It is called pragma in Perl. This pragma instructs Perl to turn on additional warning reporting.
In the next tutorial, you will see another pragma such as use stricts
which is highly recommended to use.
The third line of the program is:
print("Hello, World!\n");
Code language: Perl (perl)
We used print()
function to output a string.
A string in Perl must be placed inside quotes. You will learn more about Perl strings in the upcoming tutorial.
The \n
character at the end of the string is an escape sequence which is known as a new line. It instructs Perl to start a new line.
You’ve taken the first step in Perl programming by developing the first simple Perl Hello World program.
Hopefully, you have had it executed successfully. If you haven’t, please get through it.
As always, learning by practicing is the best way to learn, especially in programming.