Up: CGI Scripts with Perl
Prev: System Calls

Perl Peculiarities

It's easy to get engrossed in Perl because there are ten ways of doing anything. At the same time, it is easy to write Perl programs quickly that get the job done. Here is an example:
#!/usr/local/bin/perl


for ($n =0; $n < 10; $n++) {

    $max = 100 + 10 * $n;

    open(OUT, ">square$n.eps");

    print OUT
"%! PostScript
%%Orientation: Portrait
%%BoundingBox: 0 0 250 250
%%Pages: 0
newpath
0 0 moveto
250 0   lineto
250 250 lineto
0   250 lineto
closepath
stroke
20 20 moveto
$max 20   lineto
$max $max lineto
20   $max lineto
closepath
stroke
showpage
";
    close(OUT);


    system("convert square$n.eps square$n.tiff");
}

system("img2mpeg -o square.mpg square?.tiff");

Here is another example relying more on Perl tricks. This script scans a web log and counts hits per page. The log file (which, incidently, is the output of another Perl Script...) looks like this:
Date: [Mon Apr  1  0:07:00 CST 1996]  Page: download
From host:   yamuna.iitk.ernet.in
Linked from: http://www.geom.umn.edu/~rminer/jmath/
Browser:     Lynx/2-4-2  libwww/2.14 via Harvest Cache version 1.4pl2

Date: [Mon Apr  1  0:19:46 CST 1996]  Page: WebEQ
From host:   escher.cs.umass.edu
Linked from: http://www.javaworld.com/apprev/ar1.html
Browser:   welcome  Mozilla/2.01 (X11; I; IRIX 5.3 IP19)

Date: [Mon Apr  1  0:21:05 CST 1996]  Page: documentation
From host:   escher.cs.umass.edu
Linked from: http://www.geom.umn.edu:80/~rminer/jmath/
Browser:     Mozilla/2.01 (X11; I; IRIX 5.3 IP19)

Date: [Mon Apr  1  1:34:44 CST 1996]  Page: HOMEPAGE
From host:   tyo3.gate.nec.co.jp
Linked from: NA
Browser:     Mozilla/2.01Ib7 [ja] (Win95; I)  via proxy gateway  CERN-HTTPD/3.0 libwww/2.17

Here is the Perl Script:
#!/usr/local/bin/perl

open(LOG, "ref_log");

if ($#ARGV == 1) {              # read argument list
	$short = 1;             # -s date (short format)
	$date = $ARGV[1];
}
else {                          # just date
	$date = $ARGV[0];
}
while (<LOG>) {
    if (/$date/) {              # applies pattern match to current line
                                # $_ =~ /$date/ is equivalent

	$count++;
	/Page: (.*)/;           # still using $_ extract page
	$page = $1;
	$download++ if ($page =~ /download/);

	$pages{$page}++;        # increment assoc array entries
	$vs = <LOG>;
	$visitors{$vs}++;
	$lk = <LOG>;            # read the referring link and reformat

	$lk =~ s/Linked from: http:\/\///;
	$lk =~ s/:80//;
	if ($lk =~ /www.geom.umn.edu..rminer/) { $lk = "local pages \n"; }
	if ($lk =~ /www.javaworld.com.cgi.bin.w3com/) {
	 	$lk = "www.javaworld.com/cgi-bin/w3com\n";
	}
	if ($short) {
		$lk =~ /([^\/\n]*)/;   match up to first '/' ie hostname
		$lk = "$1\n";}
	$link{$lk}++;
    }
}

print "\nPage Distribution Summary \n\n";

foreach $place (sort (keys %pages ) ){
    $num = sprintf("%2d", $pages{$place});
    print "$num hits on page $place \n";
}

print "\nLink Report Summary \n\n";

foreach $place (sort (keys %link ) ){
    $num = sprintf("%2d", $link{$place});
    print "$num hits from $place";
}

print "\nVisitor Report Summary \n\n";

@junk = keys(%visitors);
foreach $place (@junk){
    $tot = $tot + $visitors{$place};
}

print "Total Pages hit =         $count\n";
print "Total Visitors =          ", $#junk , "\n";
$tmp = sprintf("Average pages per visit = %3.2f \n", $tot / $#junk);
print $tmp;
print "Total downloads =         ", $download, "\n";

close(LOG);


Up: CGI Scripts with Perl
Prev: System Calls

[HOME] The Geometry Center Home Page

Comments to: webmaster@geom.umn.edu
Created: May 08 1996 --- Last modified: May 29 1996