PERL PRIMER 4.
TOPICS COVERED IN THIS PRIMER:
- A Hello World example Perl script.
- Line by line breakdown/explanation of the script.
Ok, lets look at the worlds most basic HTML generating perl script. every tutorial for a language starts with the "hello world" program, why should I be any different? :-)
This script however, prints to your browser to display as a web page.
I saved this file as hello-world.pl and put it in my cgi-bin. if your hosting server doesn't seem to have a cgi-bin, you should ask your host if they allow perl scripts, and if so where to put them (some servers allow CGI scripts to run from anywhere, but generally that's a bad idea.) anyway, here is the little script.
------snip below this point----------- #!/usr/bin/perl -w # Tell perl to send a html header. # So your browser gets the output # rather then <stdout>(command line # on the server.) print "Content-type: text/html\n\n"; # print your basic html tags. # and the content of them. print "<html><head><title>Hello World!! </title></head>\n"; print "<body><h1>Hello world</h1></body></html>\n"; -----snip above this point------------
If you put that in your hosting servers cgi-bin directory, make it executable, and point your browser
(in my case, I point my browser at http://127.0.0.1/cgi-bin/tute/hello-world.pl to run it.) You should see a big bold Hello World displayed.
As you would expect from reading the code. all it does is write a very basic HTML page to your browser.
LINE BY LINE BREAKDOWN/EXPLANATION OF THE SCRIPT
Now lets break it down line by line.
This line was explained in the second primer, and it just tells the server where to find perl..
# Tell perl to send a HTML header
(And all other lines beginning with a # (hash) except for the first "shebang" line.)
Perl will ignore any lines that begin with a # character, so we use these as comments. Comments mean nothing to perl, but they are great for leaving notes in your script for yourself or others. Good commenting is an art in itself, and one many people fail miserably at (myself included).
print "Content-type: text/html\n\n";
This line tells perl to print a HTML header so that the output goes to the
browser instead of the command console of the server..
(which you won't see with the browser.)
We will eventually come back to headers again, as text/html is not the only type, headers are also how you set cookies in Perl/CGI and redirect the users browser among other things.
The other thing of note here is the \n's at the end of that line.. \n represents a "new line" so in general terms, it means that when you look at the output, whatever you see \n in a script, the output will appear on a new line. (note: New lines will not appear in a browser though as line breaks are ignored in HTML, however if you view the source of the HTML output of a script, wherever a \n newline was in the Perl output, the source will start at a new line.)
This header line however is slightly different, you should not see that output at all, it's there just to tell the system that it's for your browsers eyes only and that's where it should send the output data (in this case HTML).
The last two lines are pretty obvious, since we printed a header and everything else now
goes to the browser, we can start printing out HTML.. you can do whole
web sites in this manner if you like and have no actual HTML files at all.. and your users
will not really know the difference.. (except for the URL on your server.) However, the fun really starts when
we dynamically generate the HTML from a database or depending on user input later on.
Another item of great importance is the final character on each line. (except the shebang (1st) line and the comments.) that character is a semi colon ";" it tells Perl that it has reached the end of that particular instruction.. a missing ; is the third biggest cause of scripts not running and the result of this error in a browser is a
"Internal Server Error" message which you can see an example of by going to: Opps!. That script is exactly the same as the one above except I took the ; from the end of:
print "<html><head><title>Hello World!! </title></head>\n";
As you can see, the program stops dead...
One last thing to discuss, notice in the example, there were no HTML attributes listed.. like <font color="red"> I did that on purpose to keep it simple. Inserting that font example into the script would have resulted in an error. The reason is simple, as I will attempt to demonstrate. Lets start by looking at one of the print HTML lines again:
print "<body><h1>Hello world</h1></body></html>\n";
As you can see, its just a print statement with some HTML to print surrounded by double quotes, and therein lies the problem.
I'll demonstrate by adding some attributes enclosed in dlb quotes.
print "<body><font color="red">Hello world</font></body></html>\n";
If you look at that carefully, you will see what the problem is. Since the double quotes are intended to encapsulate the HTML, the inclusion of
the "red" has perl confused because all of a sudden the line looks like this: (to the Perl interpreter)
print "a text string" Illegal syntax characters(red) "text string"
There are several ways around this, and I will share two of my favorites with you.
Example: <font color='red'>
Example:Notes: ENDOFHTML can be replaced by any legal characters you like. The closing ENDOFHTML MUST be on a line by itself, with no spaces or other characters between it and the start of the line, and no semi colon on the end.
<html> HTML goes in here. </html>
To Show both those methods in practice, I have rewritten "hello world" to use both.
------snip below this point----------- #!/usr/bin/perl -w print "Content-type: text/html\n\n"; print <<ENDOFHTML; <html><head><title>hello world</title></head> <body> <table width="100%"> <tr><td align='center'>Hello World!</td></tr></table><br /> ENDOFHTML print "<font color='green'>bye!</font></body></html>\n"; -----snip above this point------------
One final note:
Please pay no attention to the HTML used in these code snippets, the font tag is depreciated as is the align attribute. They were used for example purposes and have no real place in current HTML/XHTML standards.
So, now you know how to upload a script, how to set its permissions, how to
tell it to print the output to the web browser, and how to print the HTML. pretty simple hey!!!
This site is totally free to use, you have absolutely no moral or legal obligations to help us continue.
There are however, some costs involved in running the site.
So if this site helped you find your way,
perhaps you could consider contributing to our costs.
Whatever amount you feel this site was worth to you would be just wonderful.
Use PayPal if you do decide to share and help us with the costs and in appreciation for our time and attention, or alternatively buy a book from our Bookstore..
Time in Don's part of the world is: Wed, March 29, 2017 at 11:48 PM
Time in Franki's part of the world is: Thursday, March 30, 2017 at 12:48 AM
Don't worry neither one sleeps very long!