PERL Saying Hello World (Im Just Getting Started!)

Last Edited: 2013-11-29 21:00:32

PERL programming - A story you can skip..

I have just started programming with the language PERL and let me tell you it is fantastic! There's times in my experimentation that I have wondered why not more websites are programmed with PERL and how PHP has seemed to take the lead on more current web programming choices. In my albeit brief research, I found that PERL was kind of a derivative of PHP, hacks made for PERL programmers. But the performance that you lose in the process is astounding.

I started looking into some other languages with a side project I have been working on here and there. A website that I used to work on had been posting their professional photographs on Flickr. About 13,000 pictures later, they decided to turn that traffic that goes to flickr back to their site. But, they really liked the look, feel and a choice few functions of Flickr; so I started building them Flicker. It actually has turned out pretty nice IMO. You can check it out at

I ran across a few challenges along the way. The first was transferring the 13,000 images from Flickr over to their server. I'll talk about that in a different post, but basically I set up a PHP script to pull them over via the Flickr API, and then set up a cronjob to fire the script every 5 minutes - because I could guarantee it's failure due to timing out. And that's a good cheat when you're in a pickle.

In a different personal project, which will be explained in detail in this blog (you will be able to subscribe soon!), I was working with the openCV library and C++ to look at images fed from an IP camera on my network and find faces. There is an external plug in that I found for PERL for finding faces, but decided to go with C++ on that. However, I was aware that both the C++ program once compiled and the PERL script will run until it is finished or something in the server kills the process for whatever reason.

Since I am a combination of cheap and poor, I have my server at home that I have to work around my ISP blocking port 80 and 443 on me; and my shared linux hosting at the godaddy. I found that gcc, the compiler I have been using isn't available through that shared hosting, PERL was the way to go for future scripts. Not to mention, PERL, though still an interpretive language (doesn't need compiling ahead of time), is easily 3 to 4 times faster than PHP. In comparison, PHP is darn right slow. How foolish, Tom.

With this, I've decided to get heavier into PERL as an addition to my PHP web applications to run background processes, large database handling, image processing and a few other things. As I learn, you can learn to with my easy to follow tutorials. Llllllllet's go!

PERL programming - Begin here if you skipped the story

Setting up an environment

If you skipped the story above, you do not know that these tutorials should work in most places, but were originally done on godaddy's shared linux hosting. There are some modules that are not useable, but I will not write any tutorials on those - for now. We're just learning. The good news is that PERL scripts are executable in all the folders in your hosting plan. That means you do not need to hold your scripts in the /cgi-bin/ folder and reference them there. That's extremely helpful.

When starting a PERL program, you could use the extension .pl, but I read a long time ago that if take a couple steps ahead of time and hide your choice in server-side scripting, you can give yourself an edge on security as an attacker would not know which language you are using. I did read that PERL has some security flaws, but haven't looked into them yet, but keep that in mind if you are writing a website for a bank or something with sensitive data. To do this, you can choose a different extension and create an .htaccess file. While testing especially, you should set up a folder from your root directory to play in since an .htaccess file in the root may affect everything else on your server. I made a folder /perl for my testing. Then I created an .htaccess file and put this in the contents:

AddHandler cgi-script .freddy .do .html

This will tell Apache (the platform sending stuff from your server out to the interweb) to run anything with these extensions through the interpreter before sending it out to the web. I started by making it .html because that would be neat to process stuff server-side on pages that don't look dynamic. This is a trick I have done many time in PHP with a similar but different .htaccess directive. I also added the Java .do. I got that idea from a few bigger retailer's websites I've seen. And then for fun I added .freddy as my own custom extension.

In the interest of including as much as I can, you can make all of the files, no matter the extension, or even if it doesn't have an extension using this as your .htaccess file:

SetHandler cgi-script

But, I do not advise that for security reasons. I would for sure pick an extensions and stay with that; just figured I'd let you know that it is possible.

PERL programming - Hello World!

Now that should be set up and ready to go, let's write our first PERL program and see if it even works! Create a new file in your /perl directory called test.extension. For this tutorial, I will continue to use what I chose before, .do. So I will be calling mine You may call yours I'm not sure; I can't see your screen from here. When that is created, put this in it:

#! /usr/bin/perl

print "Content-type: text/html\n\n";

print "<h1>Hello World!! (From Perl)";

First thing is the shebang - this tells the interpreter what language to handle this puppy with. If you've ever done a bash script, or programmed a cronjob for PHP, you've run across this before. If you have permission to do so, you could navigate over to that location and you would see a perl binary ready to be executed by anyone:

randomuser@freddythunder:/usr/bin$ ls -l | grep perl

-rwxr-xr-x 1 root root 545 Apr 10 2013 cpanp-run-perl

-rwxr-xr-x 1 root root 3955 Sep 10 2012 dh_perl

-rwxr-xr-x 1 root root 1065 Jun 7 2012 dh_perl_dbi

-rwxr-xr-x 1 root root 23599 Apr 10 2013 find2perl

-rwxr-xr-x 1 root root 76576 Jun 11 2012 foomatic-perl-data

-rwxr-xr-x 2 root root 10456 Apr 10 2013 perl

-rwxr-xr-x 2 root root 10456 Apr 10 2013 perl5.14.2

-rwxr-xr-x 2 root root 45183 Apr 10 2013 perlbug

-rwxr-xr-x 1 root root 125 Apr 10 2013 perldoc

-rwxr-xr-x 1 root root 12318 Apr 10 2013 perlivp

-rwxr-xr-x 2 root root 45183 Apr 10 2013 perlthanks


Then you set the contet type so Apache knows what's leaving. This could also say image/jpeg or application/octet-stream to say what's on its way out. In our case, it's just HTML. Technically speaking, you are sending out a document and that is the header that defines the document. Then the last line echos out your entire HTML string. Last step, open up your browser and go to your website, the perl folder and your new program name, and it should display your Hello World message. Easy.

You may get an Internal Server Error which seems to be one of the only errors you get with PERL, but it could mean something as terrible as PERL is not installed to something easy that you missed or put in a wrong character. Only takes one. It's like that joke about cars, 10,000 bolts hold the car together, but it only takes one to fall out and spread all the car pieces across the freeway. Don't get discouraged, check your code and if you have access check your error logs if you have access.

Any questions? Post them below.



Recent Posts