Home » Perl Basics » 02 - Perl Basics
2

First Step: Hello world!

Get a headstart on learning about Perl programming and know how to display "Hello World" using this language.

Displaying “Hello World!” is the first thing most, if not all the programmers in the world learned to do. Now it is time to see how Perl does it.

If you have a Linux/Unix platform, then you have Perl. A lot of the scripts in a Unix/Linux system rely heavily on Perl to function. If you have windows you will need to download and install Perl from Perl.org.

First if you are on a Linux/Unix system, check your Perl version. It is particularly important if you have an older server where Perl might not be up to date. If your version is less than 5.X then you will need to upgrade. Even for Perl 5.0 to 5.7 we suggest you to go to 5.8.

Now we need to create a file. For Windows, you can use notepad, however we strongly suggest you to use notepad2. Designed for programmers, the code will be a lot more readable than it is with notepad www.flos-freeware.ch/notepad2.html. Do not use Word or the like to code in Perl. Perl does not require an extension, however for the purpose of clarity we suggest you to use the .PL extension on all your Perl scripts.

Create helloworld.pl

On Linux, since extensions do not have any meaning you need to tell the script where to find the interpreter. The header of the script does that. You need to include that information on every script you create in order for it to run.

  #!/usr/bin/perl  -w

/use/bin/perl is a common path of Perl on many systems. You may want to create symbolic links to that location if your Perl is not in that directory. This action allows your scripts to run on other systems more easily or to allow other scripts to run on yours more easily. The –w is here to tell Perl to return all problems found in your script. It is important because Perl, while interpretive does not run your script line by line like other interpretive languages. It will actually compile the script before running it. –w allows you to see the error output of that compilation.

On Windows, the automatic install will link the .pl extension to Perl automatically, which you will have to do by yourself if you mnaually install Perl after compiling it. You do not have to include the line above in your script.

  #!/usr/bin/Perl  -w
# author:
#modified by:
# date:
#Purpose: Hello World
print "hello world";
#END


The print function will echo anything you put between the commas. The semi-colon (;) at the end isn’t necessary for the print function as Perl is very permissive on the syntax. However, you should force yourself to put it at the end of each line because some functions will not run properly.

Putting the semi-colon (;) at the end of the line, indenting the code section, writing a meaningful header- all of these actions are optional, but necessary. Chances are you or someone else will get back to that code to modify or reuse it. The good or bad habit will follow you the rest of your life and a well written and clean code is what you should strive to achieve.

On Windows, the .pl extension should be associated with Perl and running it like any other executable. On Linux/unix you will have 2 options to run the script. The first one is simply using the executable Perl.

  [user@Perltutorial]$Perl helloworld.pl
The second method to run the script is the make the script executable. If you do an ls –l on the file you should see –rw-r—r—which mean that the file is not executable. Run chmod 755 helloworld.pl to render it executable. Then use the normal ./ to run the script

  [user@Perltutorial]$./helloworld.pl
On Linux/Unix you will notice that the hello world is displayed before the prompt. It is so because the print function does not echo a new line.

Hello wolrd[user@Perltutorial]$

To echo a new line you must add WITHIN the quotes “\n” to echo a new line.

  #!/usr/bin/Perl  -w
# author:
#modified by:
# date:
#Purpose: Hello World
print “hello world\n”;
#END
If you execute the script again you will get a single line with “hello world” and a new prompt on the next line.

  [user@Perltutorial]$./helloworld.pl
Hello world
[user@Perltutorial]$
Your first script is running. Now a little variation of that script.

If you put single quotes instead of double quotes you will see a totally different output.

  #!/usr/bin/Perl  -w
# author:
#modified by:
# date:
#Purpose: Hello World
print ‘hello world\n’;
#END
Run the script.

Hello world\n[user@Perltutorial]$

As you can see the \n is also echoed. It is because the single quotes will not interpret the text inside them; it will print it as is.

Single and double quotes can be mixed and matched. Single quotes will be echoed if put between double quote and double quote will be echoed if put between single quote. The print function also accepts several statements on a line. Simply put a coma and the other statement in quotes.

 

Example :

  print  ‘hello world \n’,” \n”             would echoed
Hello world \n
[user@Perltutorial]$