[ale] sanity check article...

David S. Jackson deepbsd at earthlink.net
Sun Feb 3 15:36:25 EST 2002


Hi,

Just wanted a sanity check on this article: please give it a
onceover to see if it contains any errors.  (dvi2tty made several
errors; I think I caught them all, but I'm not sure.)

TIA!

-- 
David S. Jackson                        dsj at dsj.net
=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
For my birthday I got a humidifier and a
de-humidifier...  I put them in the same room and
let them fight it out.  -- Steven Wright






                            Fun With Perl



   I never cease to be amazed by Perl (Practical Extraction and Report

Language). It is aptly named the Swiss Army Chainsaw of scripting

languages. Since you UNIX/Linux geeks out there are already running your

own webservers (they're often installed by default), I'll show you one simple

and quick way to create a dynamic web page.

   First, your web server will probably be configured to run CGI scripts

out of only one or possibly two directories. For security reasons, this is a

good idea. So, you'll need access to the CGI directory on your computer.

Talk to your network admin if you don't have access. But, since you're

reading this, you're probably a fairly new Linux or UNIX user, and you

have access to the Apache web server installed with your OS and configured

to run CGI scripts (on my box the directory is /usr/lib/cgi-bin/).

   Second, there are far spiffier and advanced ways to do what I'm about

to show you. In Perl, there's always more than one way to do it. So what

I'm showing you is simply a starting point. You can take my suggestions

and add to them from there.

The Basic Idea. You want web pages that can take care of themselves as

much as possible. You don't want to have to go back through every line of

HTML every few weeks. It's far easier if you can simply upload files to the

various directories and let the web pages automatically update their

hyperlink content to reflect the new uploads.

   Let's assume that I have a directory on my website where I dump new

html files. Maybe they cover articles or homepages for my children or

whatever. I want the web page with links to these to be created dynamically

every time someone loads it. That means that a Perl script must be called

that reads the appropriate directory, looks for html files, and generates the

up-to-the-minute webpage. (Perl is just one language that can do this.)

The Tools. When you installed Perl on your system, you installed a whole

bunch of libraries of code that can be used again and again by different Perl

programs. But, there were about a bazillion pieces of code that weren't

installed. That's because if you install all the modules that were available,

you'll probably fill up the hard drives of every computer in your house and

still not have enough room. But CPAN (Comprehensive Perl Archive

Network) is there for you to get whatever you need; there are mirrors for it

all over the planet. If it's not in CPAN, you won't ever need it.

   The tool we'll be using today is the CGI.pm module. You might already



                                   1




have it installed on your computer; it's a pretty popular module. Just do a

locate CGI.pm (assuming you have a locate database on board), and if you

find it, you're golden. If not, you'll need to go to www.perl.com/cpan and

search for it, then download it.

   To install a Perl module is pretty simple, usually. You untar it into your

directory of choice. Then you type tar xvfz perlmodulename.tar.gz;

cd perlmodulename; perl Makefile.PL; make; make install. You

have to be root to install it. Most of the time, that's all you need to do.

The Code. We'll call this script showdir.cgi. We'll assume you have a

directory full of html files with recipes on them, and you want people to

click on the titles and load the recipe files without you having to manually

update the hyperlinks all the time. In a nutshell, here's the basic code:


#!/usr/bin/perl -w

use CGI qw(:standard);     # Here's what makes it all possible

my $directory = '/var/www/myfiles';     # web files located here
my $url = 'http://myhost.mydomain.com'; # baseurl here
my $dir = 'myfiles';                    # basename of dir where
                                        # web files are

print header(), start_html("My Recipes"),
h1("Welcome to my Recipe Collection");

print p("I was inspired by " .
a({href=>"http://www.computoredge.com"}, "this website") . " to
create a dynamically generated web page.");

print p("The following code will generate a list of all HTML
files in the current directory:");

opendir (DIR,"$directory");      # open a directory handle
while ($file = readdir(DIR)) {   # read a file at a time
 if ($file =~ /\.html$/) {       # is it an html file?
   open(FILE,"$directory/$file"); # if so, open the file
   while (<FILE>) {
    chomp;
    ($line) = $_;
    if ($line =~ /<title>/i) {
      # snarf filename and title
      ($title = $line) =~ s#(<title>)(.*)(</title>)#$2#i;
      # print the line with fresh hyperlinks
      print  p( a({href=>"$url/$dir/$file"}, "<b>$title</b>"));
    }
   }
 }
}
close DIR;  # close the directory handle (usually automatic)
print end_html();  # print </body></html>



                                   2




   Once this script is executable, a person can simply load the showfiles.cgi

program as though it were an html page itself. The web page gets created

dynamically.

   As you can see, when you call the CGI.pm module, there are lots of

functions that become available to you which save you many many

keystrokes. For example, paragraph tags are print p("text"); and

anchors are print a({href=>"put url here"}, "text to click on");.

Perl lets you do a lot with just a few lines of code. (Yes, it can be done

much shorter than what I have, but it's hard enough to read already.)

   The heart of the program is simply opening a directory filehandle,

reading the contents of all files with an html suffix, and snarfing a title as *
 *it

appears between the <title></title> tags. The title and the filename get

read into the variables $title and $file respectively and get printed back

out into a line of html that the client browser interprets dynamically. Each

time the user clicks his reload tab, the browser will display all new html

files in the directory and make them a hyperlink on the current page.

Conclusion. This script is just a small starting point. Once you see how

you can generate your web pages dynamically, you can add features, such as

database lookups or dynamic stylesheets or style classes, or you can simply

eliminate much of the drugery of keeping your web pages up to date.

Experienced programmers will make their code much more terse than what

I've shown.

   For more information on how to learn to use Perl, pick up a copy of

Learning Perl or Programming Perl, both from O'Reilly. They'll have you

up and writing usable Perl scripts before you know it!



                                   3



---
This message has been sent through the ALE general discussion list.
See http://www.ale.org/mailing-lists.shtml for more info. Problems should be 
sent to listmaster at ale dot org.




More information about the Ale mailing list