Skip to content

How to create a Perl script

Perl is a general-purpose programming language used for a wide range of tasks including web development. If you want to find out more about Perl take a look this introduction to Perl and the online documentation.

You can use any plain text editor to create a Perl script (e.g. Windows – notepad, Mac – textedit). Normally you would also install the Perl interpreter on your computer as well so that you can test the scripts you write. Lets take a look a very simple Perl script:

#!/usr/bin/perl

print "Content-type: text/plain\n\n";
print "Hello world!";


Line 1 – This tells the web server where and what to pass the script to for processing.

Line 3 – Because we will be requesting this script in a web browser we need to tell it what type of content we are going to send it, in this case plain text. Normally you would want to return HTML content and would use text/html instead.

Line 4 – Here we print back our line of content.

Note

Each line of Perl code is called a statement, and ends with a semi-colon to signify it is a complete statement.

Open your text editor and type the code above into it (without the line numbers), and save it to a file called hello_world.cgi.

Note

When creating the script, be sure to ASCII mode not binary as the script will not work otherwise.

Next upload your hello_world.cgi script to the web/cgi-bin on your web hosting using FTP.

Using your FTP program set the permissions for your cgi script to 755. Follow our How to change file permissions via FTP guide for instructions on how to do this.

Now load up the script in your web browser e.g. http://www.domain.com/cgi-bin/hello_world.cgi (replace domain.com with your domain name)If all goes well you should see ‘Hello world!’ at the top of your screen. If not check out the Perl troubleshooting guide.

Was this article helpful?

Check out some of our related guides

Need a hand? Search over a hundred step-by-step support guides