
If you've been programming for any length of time, you may have decided or been asked to develop a web site for someone.
Typically this involves the production of one or several web pages, which consist of HTML. You can easily do this with the authoring tools available today, or you can 'roll your own' as I do - writing code in a text editor.
If the site you are building requires a form to be filled out (a login form, a purchase order, search a database), then you will need to use a language like Perl, PHP, or Python so you can perform a server-side operation. This is known as CGI (Common Gateway Interface) but CGI is NOT a programming language. It is a standard technology used as the interface between web servers (like Apache) and your browser (Safari, Firefox, Internet Explorer, etc.) using HTTP (HyperText Transfer Protocol).
What this means is you can use any suitable programming language you like, as long as it can generate HTML and execute programs on the web server.
I've been programming web-based stuff for a couple of decades and use Perl as my language of choice for it. I hope to show you why in these pages and maybe even convince you to give it a whirl.
This document doesn't get into 'programming' with Perl (that's what the rest of this site is for), but shows how easy it is to produce HTML documents, which you will have to do if you do web development.
Perhaps the first question should be 'Why use ANY programming language to create simple HTML files?'
As mentioned above, if a user needs to fill out any kind of form, you can create the form in a text editor and save it as an HTML file. But what happens when the user hits the 'Submit' button? Don't forget, all this code resides on a computer somewhere else (your host).
Ah, this is where you need something on the server to do something with the contents of that form. And THIS is where Perl comes in (or your language of choice).
Since all web pages have to produce HTML in order for your browser to render it, any language you use has to be able to produce HTML.
PHP and Python are popular choices, but I am familiar with Perl and like the control and power it gives me. So I use Perl.
One thing to note about producing HTML pages:
Whatever language you use to produce the page, it must tell the browser what kind of input to expect.
In Perl this is done with a single line:
print "Content-type: text/html; charset=utf-8\n\n";
Note the 2 line-ending characters at the end.
THIS IS CRUCIAL
Now to add the rest of the HTML ...
As you may determine, your Perl script may alternate between writing HTML and writing Perl. Just make sure the HTML sections are bookmarked by the here document tags.
Writing HTML with Perl is very easy using something called a 'here document'. PHP and other languages also have this ability.
A 'here document' is basically a 'print' statement bookended by an opening tag and a closing tag. It's based on the Unix syntax for here documents, and is line oriented - the delimiters are lines instead of characters.
As a novice you might use a print statement for every line of HTML you wanted to display. So writing the above couple of paragraphs would be done something like this:
print "<p>Writing HTML with Perl is very easy using something called a 'here document'. PHP and other languages also have this ability.</p>";
print "<p>A 'here document' is basically a 'print' statement bookended by an opening tag and a closing tag. It's based on the Unix syntax for here documents, and is line oriented - the delimiters are lines instead of characters.</p>";
print "<p>As a novice you might use a print statement for every line of HTML you wanted to display. So writing the above couple of paragraphs would be done something like this:</p>";
Can you imagine writing several hundred web pages like that!
A note to the observant: since most 'print' statements begin with a double-quote, it must end with a double-quote. However, inside the quoted string may be another double-quoted string. And don't forget the 'print' statement needs to end with a semi-colon.
This can present a problem - but don't worry. Perl has ways of dealing with any kind of weird quoting you can throw at it.
This is where the here document shines - as mentioned above it is similar to an HTML tag in that it begins with an opening tag and ends with a closing tag:
print <<EndHTML; <p>Writing HTML with Perl is very easy using something called a 'here document'. PHP and other languages also have this ability.</p> <p>A 'here document' is basically a 'print' statement bookended by an opening tag and a closing tag. Hmm. Sort of like HTML.</p> <p>As a novice I would use a print statement for every line of HTML I wanted to display. So writing the above couple of paragraphs would be done something like this:</p> EndHTML
In this case I've used EndHTML as the opening string, so the closing string must ALSO be EndHTML.
The terminating string must be identical to the initiating string;
must be the only string on that line, and must begin at the left margin.
Note the double 'less than' angles (<<) in the first print statement. Also required.
How hard is that? There are a few caveats when doing this. The main caveat is the print statement requires an ending semi-colon (because, well, it's a print statement).
The beginning delimiter is the current line in a here document.
The terminating delimiter is a line consisting of that string.
Recall we mentioned Perl has several ways of dealing with nested quotes?. Depending on whether (and how) the beginning terminator is quoted, determines how the text is treated.
There are other things you can do with here documents, but for that refer to the main Perl site.
DB Access