Testing your Perl installation

Your first Perl program

Format
mla apa chicago
Your Citation
Brown, Kirk. "Testing your Perl installation." ThoughtCo, Apr. 4, 2016, thoughtco.com/testing-your-perl-installation-2641099. Brown, Kirk. (2016, April 4). Testing your Perl installation. Retrieved from https://www.thoughtco.com/testing-your-perl-installation-2641099 Brown, Kirk. "Testing your Perl installation." ThoughtCo. https://www.thoughtco.com/testing-your-perl-installation-2641099 (accessed September 21, 2017).

In order to test our fresh installation of Perl, we'll need a simple Perl program. The first thing most new programmers learn is how to make the script say 'Hello World'. Let's look at a simple Perl script that does just that.

 #!/usr/bin/perl
 print "Hello World.\n";
 
The first line is there to tell the computer where the Perl interpreter is located. Perl is an interpreted language, which means that rather than compiling our programs, we use the Perl interpreter to run them.
This first line is usually #!/usr/bin/perl or #!/usr/local/bin/perl, but depends on how Perl was installed on your system.

The second line tells the Perl interpreter to print the words 'Hello World.' followed by a newline (a carriage return). If our Perl installation is working correctly, then when we run the program, we should see the following output:

 Hello World.
 
Testing your Perl installation is different depending on the type of system you are using, but we'll take a look at the two most common situations:
  1. Testing Perl on Windows (ActivePerl)
  2. Testing Perl on *nix Systems

The first thing you'll want to do is make sure you've followed the ActivePerl Installation tutorial and installed ActivePerl and the Perl Package Manager on your machine. Next, create a folder on your C: drive to store your scripts in - for the sake of the tutorial, we'll call this folder perlscripts. Copy the 'Hello World' program into C:\perlscripts\ and make sure the filename is hello.pl.

Now we need to get to a Windows command prompt.

Do this by clicking on the Start menu and selecting the item Run.... This will pop up the run screen that contains the Open: line. From here, just type cmd into the Open: field and press the Enter key. This will open (yet another) window which is our Windows command prompt. You should see something like this:

 Microsoft Windows XP [Version 5.1.2600]
 (C) Copyright 1985-2001 Microsoft Corp.
 
 C:\Documents and Settings\perlguide\Desktop>
 

We need to change to the directory (cd) that contains our Perl scripts by typing in the following command:

 cd c:\perlscripts 

That should make our prompt reflect the change in path like so:

 C:\perlscripts> 

Now that we're in the same directory as the script, we can run it simply by typing it's name at the command prompt:

 hello.pl 

If Perl is installed and running correctly, it should output the phrase 'Hello World.', and then return you to the Windows command prompt.

An alternate method of testing your Perl installation is by running the interpreter itself with the -v flag:

 perl -v 

If the Perl interpreter is working correctly, this should output quite a bit of information, including the current version of Perl you are running.

If you are using a school or work unix / linux server, chances are Perl is already installed and running - when in doubt, just ask your system administrator or technical staff. There are a few ways we can test our installation, but first you will need to complete two preliminary steps.

First, you must copy your 'Hello World' program to your home directory. This is usually accomplished via FTP. You can read more about FTP in some of these FTP articles from your Networking Guide.

Once your script has been copied to your server, you will need to get to a shell prompt on the machine, usually via SSH. You can read more about SSH in this article from your Linux Guide. When you have reached the command prompt, you can change into your home directory by typing the following command:


cd ~
Once there, testing your perl installation is very similar to testing on a windows system with one extra step. In order to execute the program, you must first tell the operating system that the file is OK to execute. This is done by setting the permissions on the script so that anyone can execute it. You can do this by using the chmod command:

chmod 755 hello.pl
Once you've set the permissions, you can then execute the script by simply typing it's name.

hello.pl
If that doesn't work, you might not have your home directory in your current path. As long as you are in the same directory as the script, you can tell the operating system to run the program (in the current directory) like so:

./hello.pl
If Perl is installed and running correctly, it should output the phrase 'Hello World.', and then return you to the Windows command prompt.

An alternate method of testing your Perl installation is by running the interpreter itself with the -v flag:


perl -v
If the Perl interpreter is working correctly, this should output quite a bit of information, including the current version of Perl you are running.

To find out more about using the unix shell, I would suggest going through the Linux Guide's in-depth tutorial.

Format
mla apa chicago
Your Citation
Brown, Kirk. "Testing your Perl installation." ThoughtCo, Apr. 4, 2016, thoughtco.com/testing-your-perl-installation-2641099. Brown, Kirk. (2016, April 4). Testing your Perl installation. Retrieved from https://www.thoughtco.com/testing-your-perl-installation-2641099 Brown, Kirk. "Testing your Perl installation." ThoughtCo. https://www.thoughtco.com/testing-your-perl-installation-2641099 (accessed September 21, 2017).