#!/usr/bin/perl ########################################################################### # This is an example Perl script that doesn't do that much. It is mostly an # example of Perl syntax. The last part of the example is a basic web # server log analysis program. ########################################################################### ########################################################################### # Types of variables ########################################################################### # # Scalar variables always start with a "$" whether they are on the left or # right hand side of an expression. This differs from bourne, korn, # and C-shell scripts. $logDir="/var/log/httpd"; $logFile="$logDir/access_log"; # # arrays # Arrays can be referenced two ways, in an array "context", or in a # scalar "context". In an array context, the array name starts with # a %. When referenced with a $, the value of the array is evaluated # as a scalar value. Arrays are referenced as scalars when you are # accessing a single element of the array by its subscript. When the # name of the array is referenced with an $ and no subscript, the # value of the expression is the length of the array. %months=("", "January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December"); $mar=$months[3]; $monthCount=$months -1; # # Hashes or associative arrays # One of the most useful innovations in Perl. An associative array # is like an array where the index is a string rather than an integer. # When referenced with a @, the variable is an array. When used with # a $ and a subscript, you get the value of the array element. Without # the subscript, you get the %namesAndNicknames ("Andrew", "Andy", "William", "Bill", "James", Jim"); $nickname=$namesAndNicknames {"Andrew"); $namesAndNicknames {"William"} = "Billy Bob"; # # Get a list of hash keys @names=keys (%namesAndNicknames); # # Get a list of the values in the hash @values=values (%namesAndNicknames); ########################################################################### # A subroutine definition and call ########################################################################### sub isEqual { my ($parm1, $parm2) = @_; if ( $parm1 eq $parm2) { return 1; } else { return 0; }; }; $rc=isEqual ("abc", "def"); ########################################################################### # Simple control structures ########################################################################### if ( $nickname eq "Billy Bob" ) { nascarfan("true"); } elsif ( $nickname eq "Nigel" ) { f1fan("true"); }; $length = @names; for ($ii=0; $ii < $length; $ii++) { print ("$names [$ii] aka $namesAndNicknames{$names [$ii]} \n"); }; foreach $name (@names) { printf ("%s aka %s \n", $name, $namesAndNicknames {$name}); }; # # open a file for output, read from standard in, and write it to the outfile. open (OUTFILE, "> /var/tmp/junk"); while ( $_ = <>) { print (OUTFILE "$_"); }; # # The first parm in the open command is the file handle name, File handles # have odd syntax... they do not have any special character in front of them # such as $, %, or &. open (LOG,"<$logFile") || die "error: can not open $LOG"; # # split the lines of the log file. Ex. # host.someisp.net - someuserid [01/Jan/1998:08:30:15 -0500] "GET /~welter/index.html HTTP/1.1" 304 - while () { ($host,$junk,$userid,$date,$time,$request,$url,$protocol,$junk2) = split (/\s+/); if (($url =~ /welter/) && !($url =~ /jpg/)) { $urls {"$url"} ++; $hosts {"$host"} ++; $dates {"$date"} ++; }; }; print "URL Requests by host.\n"; while (($key ,$count) = each %hosts) { print "$count\t$key\n"; }; print "\nURL Requests by URL.\n"; while (($key ,$count) = each %urls) { print "$count\t$key\n"; }; # sort some hash by value @sorted = sort { $hash{$a} cmp $hash{$b} } keys %hash; for $key (@sorted) { print $hash{$key}\n; done