Running Perl

There are a lot of ways of running Perl, it depends on what you intend to do and the platform on which Perl is installed. Before playing with Perl, you must check if you have a proper Perl version installed. For this, you may run the following command from a command prompt:
which will tell you if Perl is installed and if so, what version it is.
Now let’s see a few ways of running Perl and what possibilities we have in view.

1. The simplest way for running Perl is from a command line as in the following example:

>perl -e "print qq[Hello world!\n]"

This simple command will print on the screen the classic welcome message “Hello world!”. As you see in our example, the entire program code was included between double quotes. We can do this only with very small programs which we can include into a command line. It is very useful if we want to verify how a short sample of Perl code works.

2. Another way for running Perl is to use for the program line of code the standard input stream like in the example below:

>perl
print "Hello World!\n";
$pi=3.14159;
print $pi;
^Z

Note that the last character of the program is Ctrl/Z, which is the indicator for the End of File. This example has the inconvenience that your program code is saved only temporally and will be lost after the execution. You can use this method if you want to verify some code samples or play a bit with Perl.

3. If you want to do some serious work with Perl, you must use an editor (such as Emacs for Linux and UltraEdit for Windows) to edit and save your Perl code in a file. Don’t forget to use the .pl extension for your Perl file.

The file Hello.pl shown below is a very simple example of a Perl script:

Hello.pl
#!/usr/bin/perl
print "Hello World";

The program has only 2 lines, the first one tells where Perl is located and the second one prints the message “Hello World”. We will run this program from a command prompt window.

In Linux we must first make our program executable:

>chmod u+x Hello.pl

and the next step is to run the program:

>Hello.pl

or

>perl Hello.pl

In Windows running Perl script means typing in the following command in a command prompt window:

>perl Hello.pl

If you want to see the warnings of the program, you can use the command:

>perl –w Hello.pl

or the command:

>perl –d Hello.pl

if you want to run Perl with the debugger support.

4. After verifying your program on your local computer and everything looks ok and if this program is written for a web site, you must upload your Perl file on the web site and then run it from your browser. We’ll show you how to do this by looking at the following Perl file example:

Hello.pl
#!/usr/bin/perl
print "Content-Type: text/html \n\n";
print "Hello World";

The program included in the above file has only 3 lines;

  • the first line tells the Web server where Perl is located, that is /usr/bin/perl
  • the second line tells the Web browser that will follow some text or HTML code
  • the third line is a print command and it will print the words “Hello World” in the window browser

The next steps include the uploading (generally through a FTP client) of your script on your web server in the cgi-bin directory and the setting up of the script permissions. After that, you can create a simple web page to run the script, by creating the file Hello.html:

Running Perl

 

and running it from your favorite browser.

 

Leave a Reply

Your email address will not be published. Required fields are marked *