. I recommend it.
Now that we’ve installed it all, let's try something simple, How about a 'hello world'?
Type this in:
#! /usr/bin/perl
# display some text, then wait for a return, then end.
print "Hello World\n";
<STDIN>;
If your using a mac, press command-shift-r, openperlIDE, press F9. If your using something else, save it as 'hello.pl' and (if you have a command prompt) go to the perl folder, and type 'perl "hello.pl"'. If your using Windows, just open it in Perl. (right click, and go ‘open with’)
Now, let me explain the script.
'#!' is a command to the Operating System, It is like saying 'this document needs to be compiled by'. The path name that follows is the path to your version of perl, if you don't know it, '/usr/bin/perl' is a good choice. This line must be the first line of your script.
'#' is how you place comments in your scripts. (Notice that '#!' line is really a comment.) Everything from the '#' to the end of the line is a comment.
'print "Hello World\n";' Well, 'print' makes sense, '"Hello World' makes sense, what's the '\n'? '\n' Means start a new line, you must put this in double quotes else it says something like 'SCALAR{346F8C2}', which isn't what you want.
'<STDIN>'? That means, wait for something, followed by a return. This is to stop the Dos Prompt from quickly disappearing for windows users.
You may be wondering, why does he keep putting a ';' at the end of every line? Simple, ';' means 'end of command' You must use these to separate one command from another.
MAKING A WEB PAGE. (CGI scripts)
You may be thinking: How does "Hello world" make a web page, the answer, it doesn't. You actually need to tell it the html code. Type out the following script: (Take care to get the '/' and '\' right).
#! /usr/bin/perl
print "Content-Type: text/html\n\n";
print "<HTML><HEAD>\n";
print "<TITLE>Hello World</TITLE>\n";
print "</HEAD><BODY>\n";
print "<B>HELLO WORLD</B>\n";
print "</BODY></HTML>";
To be any good at this, you need to know HTML. I'd suggest borrow a book from your library on it. It is something you need to know.
If you already know HTML, this script should make some sense. Except for maybe the "Content-Type: text/html" line. This tells the browser what's coming at it, If you wanted just plain text, it'd be 'text/plain'. If you wanted a image, It'd be 'image/gif' or 'image/jpeg', if it was a download, it'd be 'application/x-stuffit' or 'application/x-zip'. This line is only needed when your using CGI scripts, if your placing a html document on a server, the server will look at the extension of the document, and decide the line from there. If you place it in your HTML documents, it will be prepended to your webpage and either show up the top of the browser, or cause the entire document to not show. Something which you don’t want to happen.
VARIABLES
This is the one and only reason for CGI scripts, making the scripts variable.
Type out the following script:
#! /usr/bin/perl
$x = 4;
$y = "ello world";
print "$x is the number\n";
print "H$y is called a scalar";
<STDIN>;
This has the result of:
4 is a number
Hello World is called a scalar
Now, try the following:
#! /usr/bin/perl
$x = 5;
$y = 9;
$cost = $x+$y;
print "That comes to a total of $$cost";
This gives the result of 'That comes to a total of ', which isn't what you want. So, to fix it, I'll introduce you to the idea of 'escaping', replace the last line with:
print "That comes to a total of \$$cost";
This gives the desired results. the '\' tells perl to treat the next character as it looks. This also works for:
print "He said \"Hello!\" and handed me the flowers";
You will shortly find that the @ sign needs to be 'escaped', so an email address should look like "mailto:ashley\@ashleyharis.com".
LOOPING
Type in the following program:
#! /usr/bin/perl
for $x (1..50)
{
print "The value of \$x is $x\n";
};
this gives the result:
The value of $x is 1
The value of $x is 2
The value of $x is 3
The value of $x is 4
...
The value of $x is 50
Notice that the semi colon doesn't go after the ‘for’ command, only after the closing bracket '}';
If you omit $x, the variable $_ is used instead.
x..y is a rather 'exotic' operator, Perl is the only language that supports it. C style for loops are also supported.
You could also use the while operator:
#! /usr/bin/perl
$x = 0;
while ($x =< 50)
{
print "The value of \$x is $x\n";
$x++;
}
('=<' means less then or equal to).
(Notice that the '$x = 0' line is optional, but, perl will warn you if you do this).
Or, you could use the until operator:
#! /usr/bin/perl
$x = 0;
until ($x > 50)
{
print "The value of \$x is $x\n";
$x++;
}
('>' means greater then).
Or, you could use it this way:
#! /usr/bin/perl
print "The value of \$x is ".($x++)."\n" until $x > 50;
As you can see, in perl there are many ways of doing things.
Try these scripts, and try to fiddle round with them, change the occasional number, change the variable names. See what happens, etc. It's the only way to learn.
SECOND HOUR:
Welcome back! Your learning Perl! Just 3 hours to go before you can create your own dynamic websites!
FILE HANDLING
File handling is a rather complex concept, but, it is basically another file on the server that you can read and write to.
#! /usr/bin/perl
open(MYFILE,"C:\\autoexec.bat");
while(<MYFILE>)
{
print;
}
close(MYFILE);
NOTE the ‘\\’. This is for Windows users only. (‘\’ is special, remember…)
This simply reads in a document and prints it all, when it's done, it closes it for someone else to use.
You may remember that <STDIN> waits until the user presses enter? Well, I lied, It really gets data from the keyboard (STanDard IN) and returns it to the variable $_. Ending when it reaches a new line. So <MYFILE> gets data from MYFILE (which is "htmlheader.txt"). Ending at the end of a line. It then does this over and over until, basically, it’s out of file.
How does the while loop finish? Simple, most the commands in perl either return 1 (true) if they were able to do their job or 0 (false) if they didn't. The minute it reaches the end of the file, it can't go any further, so it returns false, and ends the loop.
By default, print prints the variable $_. As a matter of fact, many operations use this variable by default.
#! /usr/bin/perl
open (FILE,">myfile.txt");
print FILE "This is a test of something to be in this file";
close FILE;
Notice the ">" in the file name? That tells perl the write to the file, not read from it. The file is created if necessary, and always emptied before hand.
Notice how I used the print command? I told it where to print. If I had left out the word FILE, it would have printed to the screen or a web browser. If I had used STDERR, it would have written to the screen or (If used on a server) the servers error logs (/var/log/http/error_log).
If you want to add something to the bottom of a file, have a look at the following example:
#! /usr/bin/perl
open (AFILE, ">>myfile.txt");
print AFILE "This line appears at the bottom of the text file\n";
close AFILE;
notice the ">>" in the filename, this tells perl to write to the end of the file, not the start of it.
FORM PROCESSING
First things first, you need to get a copy of 'cgi-lib.pl'. It is a free download. You should be able to find it at www.perl.com/CPAN/. Or just type in 'cgi-lib.pl' in altavista.
There are a lot of new things introduced in this script, be patient, I'll explain them.
#! /usr/bin/perl
require 'cgi-lib.pl';
if (ReadParse(*in))
{
$name = $in{name};
$email = $in{email};
print "Content-type: text/html\n\n";
print "<HTML><HEAD><TITLE>THANKYOU</TITLE></HEAD></BODY>";
print "Your name is $name and your email address is $email";
print "</BODY></HTML>";
}
else
{
print "Content-Type: text/html\n\n";
print "<HTML><HEAD><TITLE>PLEASE ENTER YOUR NAME AND EMAIL TO CONTINUE</TITLE></HEAD><BODY>\n";
print "<FORM method=get><Input type=text name=email> Email address<BR>\n";
print "<INPUT type=text name=name> NAME<BR>\n";
print "<INPUT type=submit></FORM></BODY></HTML>";
}
OK, first thing, the 'require' statement tells perl to load the library 'cgi-lib.pl'. This is where most of the new stuff is stored.
'if'? Ah, whoops, forgot to explain them, they work simular to the 'while' statement, but, the only do what's in their brackets once if it's true, never if it's false. They can also have a 'else' bracket to do if it's false, but only if it’s false. I know that didn't make much sense, so, here are some examples:
#! /usr/bin/perl
$age = 6;
if ($age > 18)
{
print "Have a beer and ";
}
print "Have a coke";
If age is greater then 18, it displays 'Have a beer and Have a coke', if you are under age, it says "Have a coke". Make sense?
#! /usr/bin/perl
$age = 15;
if ($age > 17)
{
print "Get drunk";
}
else
{
print "Make friends";
}
print ", and be happy";
If your older then 17, it says "Get drunk, and be happy" otherwise, it says "Make friends, and be happy".
Now, back to me explaining that first script.
ReadParse(*in)? This is the magic keyword. It gets everything that was sent to the script, and places it in a special variable called $in. What's so special is it's a hash. More on these later. The capitalisation of ReadPase is important.
$name = $in{name};? I told you $in was special. Whatever is between the {} is the name of a field on a web page. ie (<Input type=text name=whatever>)
This is hard this bit, so, look at my examples over the following issues, and, in your programs, just copy and paste it then modify it until you get the idea of it.
EMAILING FROM A CGI SCRIPT
This is a hard topic, so, like the last one, you may need to resort to copy/paste/modify.
#! /usr/bin/perl
open(MAIL,"| /usr/lib/sendmail -t -oi");
print MAIL "To:ashley\@ashleyharris.com\n";
print MAIL "From:happyuser@myhouse.com\n";
print MAIL "Subject:It worked\n\n";
print MAIL "This is just a quick message to say everything worked.";
close MAIL;
OK, the '|' means don't open a file, open an application, and because It's before the filename, write to it. The -t and -oi tells sendmail that we'll enter the headers ourselves (better security), and to send when we close. This will only work from the server, and, some servers will remove it to prevent spam.
HOW TO ACTUALLY RUN THE WEB EXAMPLES OVER THE INTERNET.
This is a common question I get. You need to upload it to your website using and FTP program, then find the option to change the access privileges. You need to make it readable to everybody, executable by everybody, and the creator can write to it. If it asks you for a number, type 755. If you can telnet in, go to the folder, and type 'chmod 755 THEFILENAME'.
If you are using libraries (ie 'cgi-lib.pl', you need to do the same to them aswell).
Didn't work? Try putting it in a folder called 'cgi-bin' in your website folder. If that doesn't work, try backing up a folder (go to dir. ".."), and either go into or create a folder called 'cgi-bin'. If that doesn't work, try talking to your system administer or web hosting company. If you’re using something like geocities, don't even bother, you need to find one that will allow you to run scripts on their servers. (There aren't many free ones).
If your on a budget, try going to demented.org. They offer free 20MB web hosting with CGI script support, perl, everything. No ads. Sign up, and upload your scripts, do the previous paragraph, and try to access the page with a web browser. You should see what you want.
Now, these things go wrong sometimes. So, if you see an error:
500 First, check to see whether you made the script and every other script and library on the site executable. Then, run the script through your own version of perl, see if it works. Then, check the top line of the script to see if it's '/usr/bin/perl'. Then, check the server error logs, they should offer you some assistance. Basically, it means an error has occurred, and you need to fix it. (5 means permanent failure)
404 Any web user knows what this means, make sure you get your capitalisation right. Also, check to make sure you have the right privileges, and that every folder it's in is readable by everybody. (In Linux, if you can't read it, it's not even shown). Also, try placing /cgi-bin/ between your Domain and script name. ie. http://192.168.0.1/cgi-bin/htmltest.cgi instead of http://192.168.0.1/htmltest.cgi.
403 When you set up your script, and made it executable, you forgot to make it readable. Or something along those lines. Try making it readable/writable/executable to everyone (mode: 777)
THE THIRD HOUR!
You think you know this CGI stuff cold! You can your ‘hello world’ page look the coolest. Well, now the bombshell drops:
In this hour, I'll demonstrate Functions and Subroutines. How to make libraries, and placing headers and footers on every page.
FUNCTIONS+SUBROTINES
Functions or subroutines (They are the same thing in Perl). Are common bits of code. It is pretty hard to put into words, so, here are some examples.
#! /usr/bin/perl
for $x (1..5)
{
for $y (1..5)
{
print add($x,$y);
print "\n";
}
}
sub add
{
$num1 = shift;
$num2 = shift;
return $num1+$num2;
}
#! /usr/bin/perl
print "The factors of 65 are".factors(65)."\n";
print "The factors of 72 are".factors(72)."\n";
print "The factors of 360 are".factors(360)."\n";
sub factors
{
$num = shift;
$temp = "";
for $x (1..$num)
{
# Is it a whole number when you divide $num by $x?
if (int($num/$x) == $num/$x)
{
# Append it to $temp
$temp = $temp." $x";
}
}
return $temp;
}
shift gives you (returns) the next subroutine argument. (Thing in the () ) when the subroutine is called.
int gets a number, and removes everything after the '.'. No, it doesn't round the number.
Notice the '==', DO NOT, I repeat, DO NOT use '=', terrible things can happen.
LIBRARIES
Libraries are a group of functions in a single file. Here you would included functions to: pick a banner, check a password, read an account, draw a header, etc. Anything that you are likely to use on more then one page.
Libraries should be their own file. They don't need to have the '#! /usr/bin/perl' line at the top, but, it's good to.
Libraries are exactly the same as perl scripts, except, no code should execute when they're run.
When on a server, they need to be set to executable. See Pt. 2, getting your scripts to actually work on the web.
One more thing, at the end of each library, you need to included the following line:
1;
Why? This is a just a test used to see if there is something there.
To use a library in a script, simply place 'require "LIBRARYNAME.PL"' at the top of each script that you want the library to be used.
PLACING HEADERS AND FOOTERS ON EACH PAGE.
This is related to the previous 2 topics, In your script, it would have:
#! /usr/bin/perl
startbit();
print "This is a web page";
print "<P> I"m doing something";
endbit();
and in your library, you'd have:
sub startbit
{
print "Content-Type: text/html\n\n";
print "<HTML><HEAD><TITLE>This is my webpage</TITLE></HEAD><BODY leftmargin=0 topmargin=0 marginwidth=0 marginheight=0>";
print "<IMG border=0 src=\"http://www.clipart.com/titlebar.gif\"><IMG border=0 src=\"/singlebluepixel.gif\" width=100% height=50><P>\n\n\n";
}
sub stopbit
{
print "<SMALL><CENTER>Copywrite 2001, Ashley Harris</CENTER></SMALL><P></BODY></HTML>";
}
1;
This can significantly reduce file sizes and upgrade hassles. As a matter of fact, this one bit of functionality was the reason I learnt CGI.
Mess around with this, see what you can do with it.
THE LAST HOUR!
In this issue, I'll show you how to use JavaScript to place the result of a CGI script in an ordinary webpage. I'll also give you a list of suggestions on where to go from here.
USING CGI IN ORDINARY HTML FILE
Let's say your website is already well established in it's current location. You don't want to move it, on the other hand, you want to place some functionality in your webpage, lets say, a hit counter. And you don't want to put the entire webpage inside a CGI script, escape every little quote and Newline, maybe your site wont fit on demented.org?
If your a little bit web weary, you'll have heard of a language called JavaScript. If you really web weary, you'll know that they can be set out like this:
..
<SCRIPT src="myscript.js"></SCRIPT>
..
And the JavaScript code included in the file myscript.js is loaded, execute, and anything written to the screen, is placed at that point. This is perfect for CGI, because, perl can make the .js file. The bad problem is, you need to learn practically a new language, and because your writing a program to write a program to execute on every possible computer, which will cause most people eyes to gloss over about now. First, here is the code for the CGI script. (This example is a counter).
#! /usr/bin/perl
open (COUNT,"counter.txt");
$count = <COUNT>;
close COUNT;
$count++;
open (COUNT,>counter.txt");
print COUNT $count;
close COUNT;
print "Content-type: text/html\n\n"; #Yes, I'm sure that's right.
# Start the JavaScript document.
print "document.write(\"";
print "There have been a total of $count hits on this site since I built it";
print "\");
# Ended
On your webpage, you'd include the code:
<SCRIPT src="counter.cgi"></SCRIPT>
Done! Don’t believe me! Try it out!
WHERE TO GO FROM HERE
One things for certain, I haven't taught you everything about perl, why? Because, nobody knows it all. So, what can you do now?
- Finish learning the basic structure. I didn't teach you anything about hashes or arrays or complex data structures. I didn't even tell you how to change directories or pass commands to the system. Most of these can be found in the first few chapters of a book at your local book store. Also, if you have the mac version, your lucky, you have an entire manual under the help menu.
- Regular Expressions. These are very hard to learn, and very hard to read. I understand them, but don't believe I could teach them to you. There basically find and replace commands on steroids. They can do things like put the commas in long numbers, extract the text between two words, extract the text between two words of x length. Reformat paragraphs, anything that involves text and doing stuff to it.
- Object orientated programming. When you being to extend yourself, especially towards the graphic end of perl, you should under stand this concept. Once again, I know it, but not well enough to teach you.
- User Interface. Go to www.perl.com/CPAN/ and download the library (module) 'tk'. (If you don't have it already), This allows you to a GUI onto the your perl scripts. I have visual basic 6, so, realisticly, I can’t be screwed doing this, but, especially on linux, it'd be worth doing.
- System interaction. Do you know there is 1400 digit calculator on linux (type in bc at the command prompt). You can interact with that in a simular way that I showed you with email.
- Graphic design. Ironic, isn't it? A command line program is probably the best graphic design package ever. It can make some pretty impressive graphics, and turn crumby graphics into super ones. Go to CPAN (see above) and download the 'GD' package. You can also make them on the fly using the 'content-type: image/gif' line. If you have a poor imagination, skip this step.
- VRML. Virtual reality. You can make some AMAZING scenes using this. If you don't have an amazing imagination, skip this step. I messed around with meshes for a while, but, I have blender, so, I haven’t pursued Perl VR.
- Interaction. perl can talk to FTP servers, mail servers, telnet servers, other webpages, it could even talk to Napster (when it was up).
- The huge network behind perl, Cpan. Go here, learn much.
- Well, it's been fun, and I hoped you've learnt a lot. So, to test your skills, I've set up some basic projects for you to attempt. If you remember from the first issue, there are a billion ways to do each of them, but, I've told you all of the things you'll need. Good Luck!
TASKS:
1) I want to enter my email address in a text field, and, it will be added to the end of a file.
2) I want to have a user enter their email address, but, have perl go through a file to see if it's already there. if not, add them.
3) I want to be get a large text file, and turn all the A's into Z's. (This is a pretty basic regular expression)
4) I want to remove all HTML tags that a user enters in the email field of a form. (A regular Expression that swaps '<' with '<').
5) A regular expression that reverses the order of every word in a document. (Ignore punctuation)
6) A regular expression that can abbreviate all words in a document to 4 letters.
7) A script that compares two passwords typed, sees if they are the same, and longer then 5 letters.
8) A button with text, and a coloured bar on the left. (Graphics Design)
9) A script simular to the last, that you pass the text to in a form, and it makes a button with it.
You half way there!
10) A VRML scene, resembling rolling hills. With a black sky. Skip this one if you want.
11) Take a snapshot (CMD-SHIFT-3 or Print Screen) of the previous task, crop it to just the graphic, and have a perl script read it in (using GD), and place stars in the sky.
12) A guestbook, that displays the number of entries, and stores comments, email, and the name.
13) A script that will tell you how long it has been (Years, Hours, Minutes, Seconds) since Midnight, January the 1st, 1970.
14) A script that will tell your BMI (Body Mass Index) (How Fat/Thin you are). The formula is: You divide your weight in KG by your height in M's, and then square it. < 10 = dead, 10-20 Underweight, 20-25 Normal, 25-30 overweight, 30+ Obese. Go to Commission Junction, (www.cj.com), and find a good diet program. If they are overweight, show them the link to the diet program.
15) Go to the love calculator (www.lovecalculator.com), and fiddle around with the names of your high school teachers and your pets for a while, now, try to build something simular. Include a clause, so that if you AND your sweetheart names are placed in there, it automatically gives you 100%.
16) Get the LWP module (Load Web Page, or Lib-www-perl, depending on who you talk to). Make a script that will constantly request an address. Now, Make it POST data to that page, every time it reloads it. What it rereshes is up to you. (I'd suggest something that pays per hit, or a 'vote-for-me' button, but, it's up to you).
17) Sign up for a free POP3 email account at anywhere, dig out script #2, and make it check the email address (yours at whatever.com) frequently, and, if there is any email, to forward it to everyone in the list. Make sure to strip all the headers except subject. Remember to delete the email after sending it.
18) THE ULTIMATE. Same as above, but, instead of using POP, use a hotmail account. You'll need to use the LWP module and heaps of regular expressions for this.
If you've reached here, and have done all the tasks above. You are now truly a web programmer. Give yourself a pat on the back, and email me (ashley___harris@hotmail.com) giving any comments or suggestions.
if you've complete about 10-15, Be proud, you can now have a complete change of careers if you wish..
If you've completed about 5-10, That's still an achievement, can any of your friends do that? Keep trying.
One more challenge? OK
19) Make a script that will take one field with both a song title and an artist in it (or one or the other). Make the script search a peer to peer network (fastract, gnutella, etc) for each and every combination of all the words entered, and in either the artist or title field, each time for 100 songs on an ADSL or better, with a bit rate of 64 or more. Remove duplicates, and ask the user for which one they want (sorted by relevance). If they choose to download one, download it locally, then upload it to an FTP server, then email a copy to the person who downloaded it. And then remove from your server. If anyone else does a search, make sure it searches the FTP server first, and gives priority in the search listing for any on the FTP server. Also, if your using a free web site as your FTP server, use the LWP library to automatically sign up for more space when the current account is filled with songs. Include a script that will search commission junction (cj.com) for any new merchants with 'music', 'mp3', 'sound', etc as a keyword, and add their 468x60 banner to a banner rotator at the top of each page. Also have a link to a paypal (www.paypal.com) account so that anybody who wishes to buy advertising can do it easily. Remember to included statistics such as how many people clicked on their banner, what were their IP addresses? etc. Market it, and, pay me at least 1% of your net profit.
I'm not expecting you to actually do that one, but at least try it.
I hope you learnt something from this tutorial, hopefully, by now your good enough at perl to learn the rest yourself.
Please tell me what you think of this tutorial.