#!/usr/bin/perl -T
use CGI::Carp;
use Sial::Webit;
use POSIX qw(strftime);
# page defaults
my $title = 'Perl Code';
my $description = 'Various perl scripts and modules';
my @keywords = qw:script perl module benchmark:;
# create the page header
my $sial = new Sial::Webit ($title, $description, \@keywords);
print "Content-Type: text/html\n";
print "Expires: ", strftime("%a, %d %b %Y %H:%M:%S %Z", gmtime (10800 + time)), "\n";
print "Last-Modified: ", strftime("%a, %d %b %Y %H:%M:%S %Z", gmtime ((stat $ENV{SCRIPT_FILENAME})[9])), "\n\n";
$sial->header ( '', {
'UP' => { 'HREF' => '../', 'TITLE' => 'Up to Code' },
'next' => { 'HREF' => 'bench/', 'TITLE' => 'Perl Benchmarks' }
});
print '
', $title, '
';
print 'Benchmarks | Documentation | Modules | Scripts | Related Links
';
print 'I use perl to dynamically serve out these pages, and to solve various administrative requirements to make my job easier. And just about everything else, as I mainly do text mangling, and haven\'t found any languages I like as much as perl.
';
print 'CVS is used to ensure sanity on my part when dealing with such an eclectic gaggle of scripts and modules.
';
print '';
print 'Some random benchmarks of common perl operations.
';
print '';
print 'Documentation, though mainly supporting config files for some of my scripts at this point.
';
print '';
print 'A bunch of modules that mainly do specific things on this website; they should be informative to someone learning perl, or at least a warning of some kind.
';
print '';
print 'Various scripts that do a whole bunch of different things, some buggy, some not. Enough hype, already...
';
print <<'RELATED';
Related Links
RELATED
sub do_load {
my $r_info = shift;
my $dir = shift;
my ($changes, $doc_text, $doc_pdf, $doc_html);
# read 'info' tag from filename.info file
open(F, "$dir/$_.info");
my $info = ;
close(F);
if (-e "$dir/$_.log") {
$changes = "$dir/$_.log";
}
$doc_text = "$dir/$_.txt" if -e "$dir/$_.txt";
$doc_pdf = "$dir/$_.pdf" if -e "$dir/$_.pdf";
$doc_html = "$dir/$_.html" if -e "$dir/$_.html";
my $size = $r_info->{'size'};
my $epoch = $r_info->{'mtime'};
my (undef, $min, $hh, $dd, $mm, $yy) = localtime($epoch);
$mm++; $yy += 1900;
$size = humanize($size);
print '';
} else {
print 'BGCOLOR="#CCCCFF">';
}
print ' ', $_, ' | ';
print '', $size, ' | ';
printf ("%04d-%02d-%02d", $yy, $mm, $dd);
printf (", %02d:%02d", $hh, $min) if ( $now_time - $epoch ) < 604800;
print ' |
';
print '| ', $info, ' |
' if defined $info;
print '| ';
if ($changes) {
print ' View Change Log ';
} else {
print 'No Change Log';
}
print ' |
';
# docs?
print '| Documentation:';
if ($doc_text || $doc_pdf || $doc_html) {
print ' HTML ' if $doc_html;
print ' PDF ' if $doc_pdf;
print ' Text ' if $doc_text;
} else {
print ' None Available';
}
print ' |
';
# print ' |
';
}
# clean up Enviro settings for Taint mode FIRST ( man perlsec )
sub BEGIN {
delete @ENV{qw(IFS CDPATH ENV BASH_ENV)};
$ENV{'PATH'} = '/bin:/usr/bin';
}
# and always finish off with a HTML footer
END {
$sial->footer;
}
# Inspired from GNU's df -h output, which fixes 133456345 bytes
# Inspired from GNU's df -h output, which fixes 133456345 bytes
# to be something human readable.
#
# takes a number, returns formatted string. Also takes optional
# hash containing various defaults that affect output style.
sub humanize {
my $num = shift; # could also take a array ref or hash ref to parse thru?
my %prefs = @_;
my %global_prefs = (
# include decimals in output? (e.g. 25.8 K vs. 26 K)
'decimal' => 0,
# include .0 in decmail output?
'decimal_zero' => 0,
# what to divide file sizes down by
'factor' => 1024,
# percentage above which will be bumped up
# (e.g. 999 bytes -> 1 K as within 5% of 1024)
# set to undef to turn off
'fudge' => 0.95,
# lengths above which decimals will not be included
# for better readability
'max_human_length' => 2,
# list of suffixes for human readable output
'suffix' => [ '', ' K', ' M', ' G', ' P', ' E', ' Z', ' Y' ],
);
# inherit global prefs, but give preference to user supplied ones
unless (keys %prefs) {
%prefs = %global_prefs;
} else {
# benchmarking w/ 5.6.0 on Linux PPC & i386 showed this next
# faster than direct merge method (p. 145 Perl Cookbook)
while (my ($k, $v) = each (%global_prefs)) {
$prefs{$k} = $v unless exists $prefs{$k};
}
}
# some local working variables
my $count = 0;
my $prefix = '';
my $tmp = '';
# handle negatives
if ($num < 0 ) {
$num = abs $num;
$prefix = '-';
}
# reduce number to something readable by factor specified
while ($num > $prefs{'factor'}) {
$num /= $prefs{'factor'};
$count++;
}
# optionally fudge "near" values up to next higher level
if(defined $prefs{'fudge'}) {
if ($num > ($prefs{'fudge'} * $prefs{'factor'})) {
$count++;
$num /= $prefs{'factor'};
}
}
# no .[1-9] decimal on longer numbers for easier reading
# only show decimal if prefs say so
if (length sprintf("%.f", $num) > $prefs{'max_human_length'} ||
! $prefs{'decimal'}) {
$tmp = sprintf("%.0f", $num);
} else {
$tmp = sprintf("%.1f", $num);
# optionally hack trailing .0 as is not needed
$tmp =~ s/\.0$// unless $prefs{'decimal_zero'};
}
# return number with proper style applied
return $prefix . $tmp . $prefs{'suffix'}->[$count];
}