Hello Perl
Perl has become the glue that holds companies and the internet together, but it doesn't generally have the easy to learn reputation that PHP has, nor the commercial respectability that Java has.
I'm addicted, it's the only programming language I've ever loved. And I think it is easy to learn, and that its cost-effectiveness makes it a great choice for businesses. I've been programming Perl for over a decade now, and I want to try to help the world see how easy and useful it is!
Hello World!
#!/usr/bin/perl
print "Hello World!\n";
That's all you need to print Hello World in Perl.
But if you're really a beginner, you may need a lot more than that snippet to get started. If not, skip ahead, already!
Ok, so a little more information. Perl is a scripting language, which in simplest practical terms means that the code is ready to run as soon as its written. But if you've never written one before, you may need a little more setup.
Most likely, you've come to this lens because you have a desire to do more online, so what you're interested in is writing websites run by Perl CGI scripts. So you'll need a website hosted somewhere that supports Perl. I usually use ICD Soft or 1&1 Internet myself, but there are tons of good hosting companies, each offering different feature sets, and how to pick is a whole separate education.

Ok, so after you have a hosting company that allows Perl, you also have a way of adding content to your website, usually a control panel where you can either edit files in a window online, or upload files directly. The only 2 tricky bits (you may have to open support tickets with your hosting company to get the answers) are the exact path to the perl interpreter, and whether you must put the files somewhere specific.
The exact path is needed because the first line of any perl script basically says "this is perl".
#!/usr/bin/perl
The second question really has nothing to do with Perl, but most hosting companies have reasonable restrictions on where CGI can live on their servers. For example, you may not be able to put perl at the top level of your webiste's file structure with your default index.htm, you may need to create a folder called cgi-bin and put all your scripts in there.
Beyond that, Perl scripts are just flat text files, just like .htm or .html files - but your host may require that the files be named with a specific end extension (.pl and .cgi are common). If the host has no requirements, I actually recommend no ending.
Just open any text editor (even regular windows notepad) or the file editor in your control panel and start typing a perl script.
Start with the 'bash line':
#!/usr/bin/perl
Yours may look different, this is the path to the perl interpreter.
Usually, people leave a blank line after that, its not actually required, just common practice.
Then your code:
print "Hello World!\n";
Which says (obviously) print out Hello World! and (not so obviously) then a carriage return (that's what the \n is for).
But this won't work yet as a CGI.
Hello Web!
Here's where all the Perl programmers who have come before will come in really handy ... but let's do it the "hard" way first.
Basically, the problem is that printing the page content is not enough, the web server your site is hosted on is a fabulous bunch of technology that, amongst other things, knows enough to send a few text strings that say "I'm going to send a html page" before it sends the contents of any .htm or .html file itself. But a Perl script could output anything & its not going to guess.
But I will. I'll guess we're sticking with basic HTML for now.
Here's the script you'll need to say "Hello" online:
#!/usr/bin/perl
print "Content-type: text/html\n\n";
print "Hello Web!\n";
You can stop right here & just use print statements to print out html:
#!/usr/bin/perl
print "Content-type: text/html\n\n";
print "
<html>
<body>
<h1>Hello Web!</h1>
<p>I'm learning Perl and this is my first attempt at using it to takeover the web!</p>
</body>
</html>";
But my guess is you actually didn't just want to make HTML in a more difficult way. You probably showed up here in order to create a dynamic webpage.
Unfortunately, here's where I digress into a few more basics for the true newbies again. (Not new to programming? skip ahead).
Every line of code other than the bash line ends with a semi-colon. Your code will NOT work if you forget this tiny piece of punctuation.
All these print strings I've shown began and ended with double quotes (").
String: This term just means text or any set of characters.
Strings can be delimited with any character once you get into advanced Perl, but the basic ways are with quotes: 'Hello World'
or with double quotes: "Hello World"
Double quotes invoke what's called interpolation, i.e. not everything is just exactly what it seems.
That's why \n will print a carriage return each time its seen in the examples above. If those had been single quotes, the \n would have printed literally to the screen. (FYI: \n is pronounced "backslash n").
That said, if you need a double quote to print to the screen within a double quote delimited string, the term is "escaping" and you do it using a backslash.
print "Hello \"MY\" World";
NOT Reinventing the Wheel
The Comprehensive Perl Archive Network is your friend, and sadly, not mentioned in most beginning Perl books except as a footnote. To me, not knowing about CPAN until I'd been writing Perl for 2 years was a darn shame.
The little string I printed out in the Hello Web example required me to pick up a book about the HTTP protocol and figure out way more than I ever wanted to know just to simplify it down to that string for 99% of everything I wanted to do at that time. (Side note: that was in 1996, and a lot more less-technical resources are now available, but everyday there are new common challenges & new authors adding new solutions to CPAN, so your challenges won't be the same, but the lesson still applies).
CPAN houses thousands of Perl modules, that is, units of reusable Perl code. All of which is meant to be used by you to write more useful new code faster and easier than by figuring out how to develop it yourself (reinventing the wheel).
The big 2 in dynamic website development are CGI & DBI.
CGI.pm is the module that not only takes care of that HTTP protocol example from above, but more importantly it also parses all your incoming CGI variables (something I've also tried by hand & you REALLY don't want to reinvent that particular wheel).
DBI.pm is the database connection module. It'll be a while before I get to the examples using it, buut I mention it here for those experienced programmers just skimming for the useful basics of Perl -- they should be able to read the CPAN pages.
To be fair, there are newer technologies which are more exciting than just using CGI & DBI but this is for beginners, and these 2 modules will be available from almost any hosting company that supports Perl.
That's important because if you pay for hosting, you may be limited to only using the CPAN modules that compay already has loaded. But now that you know about CPAN, you can at least send in intelligent sounding tech support questions, liek "which modules do you offer?" or "How do I install the Chart::Graph module (or get it installed) on your server?".
This is the same Hello Web using the CGI module:
#!/usr/bin/perl
use CGI;
$q = new CGI;
print $q->header;
print "Hello Web!\n";
Doesn't exactly seem easier than the other way was, I know.
Here's the more lengthy Hello example from above, using CGI.pm.
#!/usr/bin/perl
use CGI;
$q = new CGI;
print $q->header,
$q->start_html(),
$q->h1('Hello Web!');
print "
I'm learning Perl and this is my first attempt at using it to takeover the web!
";print $q->end_html;
Not necessarily easier, but not harder. Plus, most agree its easier to read.
And the good news is that the code above uses the more advanced method of calling this module, called object-oriented (OO) style. OO is big in modern programming languages, and so I feel showing you this day one is the best way to go -- no use getting used to using things another way when advanced concepts will require you to understand the OO syntax.
So now I've shown you a whole bunch of code, but I realize the basics of the language or what any of the code is actually doing haven't really been covered yet.
My Favorite Reference Books
Perl Pocket Reference, 4th Edition
Amazon Price: $9.95 (as of 10/08/2008)
Perl Debugger Pocket Reference
Amazon Price: $9.95 (as of 10/08/2008)
Learning Perl, Fourth Edition
Amazon Price: $29.74 (as of 10/08/2008)
Mastering Regular Expressions
Amazon Price: $29.69 (as of 10/08/2008)
Perl Syntax
So this is for the very first timers....
Comments are self explanatory, but the computer needs a way to know they shouldn't be interpreted as code. In Perl, a # indicates the beginning of a comment and everything from there until the end of a line will be ignored.
Ex.
print "Hello World"; # This will be ignored
# by the computer so I can use it to
# explain the code as I go along
Variables are the containers for data. Perl is wonderfully unparticular about them, you don't have to declare in advance what kind of data they'll hold. The most simple variables are designated by a $.
my $x; #This is called declaring a variable
# Perl doesn't actually require it,
# but declaring your variables before using them
# is strongly recommended
$x=5; # Put the number 5 in the variable x
$x++; # Add 1 to the variable x
print "$x";
This will print out the number 6.
Way more interesting, and incredibly powerful, is a type of variable called a hash. A hash is a set of key/value pairs, like so:
key = value
red = #ff0000
yellow = #ffff00
green = #00ff00
my %color; # The % indicates that this
# variable is a hash
# individual elements of a hash are simple variables
# so each element is designated with a $
# the key value is indicated inside curly braces {}
# and the value is set like usual
$color{red}='#ff0000';
$color{yellow}='#ffff00';
$color{green}='#00ff00';
my $x;
# keys pulls out a list of all the keys in a hash
foreach $x(keys %color){
# foreach will loop through all the keys,
# storing each in $x for 1 execution of the loop
print "$x is $color{$x}\n";
}
Ok, time to speed up a bit....there's already tons of great syntax references out there, here's the Perl Intro from Perl.org, which does a good job on syntax.
From here on out, I'll just show example code withi comments to help explain what's going on.
Links
- CPAN
- CPAN is short for Comprehensive Perl Archive Network - and it IS the comprehensive list of free helpful modules (libraries of code others have written and you are free - even encouraged, to reuse).
- JAPH
- Wikipedia article on the meaning & history of the term JAPH (Just Another Perl Hacker)
- Perl.org
- Perl.org has a big directory of links to Perl community resources, events and more.
- Perl.com
- The O'Reilly Perl site, a great resource for current Perl news
- Programming & Web Design Squidoo
- Good source for HTML & usability information.
Dynamic
So, a simple example of a dynamic webpage:
#!/usr/bin/perl
use CGI;
$q = new CGI;
$params = $q->Vars;
my %color;
$color{red}='#ff0000';
$color{yellow}='#ffff00';
$color{green}='#00ff00';
if ( $params->{'name'} && $params->{'color'} ){
print $q->header,
$q->start_html();
print "<font color="$color{$params->{'color'}}">
Hello $params->{'name'}
</font>";
print $q->end_html;
}
else {
print $q->header,
$q->start_html(),
$q->h1('Hello Stranger'),
$q->start_form;
print "What's your name?";
print $q->textfield('name');
print "What's your favorite color?";
print $q->popup_menu(-name=>'color',
-values=>['red','yellow','green']),
$q->submit,
$q->end_form,
$q->end_html;
}
Stuff for after you love Perl like I do
Reader Feedback
Like this lens? Want to share your feedback, or just give a thumbs up? Be the first to submit a blurb!







Fetching new data from eBay now... please stand by