#!perl -l # perlvars by epoptai # thanks to japhy for modlist.pl $start=(times)[0]; use strict qw(subs refs); use CGI ':standard'; use Pod::Html; use HTML::Entities; use FindBin qw($Bin); use File::Find; use File::Spec::Functions 'rel2abs'; $ltime= localtime(); ($sec,$min,$hour,$mday,$mon,$year,$wday,$yday,$isdst) = localtime(time); $url = url(); $rurl = url(-relative=>'1'); $qurl = url(-path_info=>'1',-query=>'1'); ($name= $rurl)=~s/(.*)\..*/$1/; $cfg = $name.'.cfg'; $dat = $name.'.dat'; $path = $0; $path =~ s/\/$rurl//o; &import_names('IN'); unless(eval "require '$dat'"){$nodat=1} else {$nodat=0} unless(eval "require '$cfg'"){$init=1} else {$init=0} if(($nodat==1) or ($init==1)){&install()} # find required data files or install if($IN::go eq 'perlmod'){&perlmod()} if($IN::go eq 'perldoc'){&perldoc($IN::pm,$IN::title)} if($IN::go eq 'restore default'){&install()} if($IN::go eq 'save config'){&config_save();exit} if($IN::go eq 'home'){ print "Location: $url\n\n"} if($IN::go eq 'refresh'){ print "Location: $url?rd=1\n\n"} if($IN::go eq 'update'){&updatedat();exit} if($IN::go eq 'config'){&configure();exit} if($IN::go eq ' ? '){&info();exit} if($IN::go eq ' url '){&selfurls();exit} if(($IN::file eq 'view') && ($IN::filename)){&view_file()} if(($IN::file eq 'edit') && ($IN::filename)){&edit_file()} if(($IN::file eq 'save') && ($IN::filename)){&save_file()} if(($IN::file eq 'stat') && ($IN::filename)){&stat_file()} if(($IN::mods eq '1') or ($show_mod eq '1')){&findmodules()} if( ($IN::env eq '1') or ($show_env eq '1') ){ foreach $key (keys(%ENV)){ unless($ENV{$key} eq '') {$envtotal++}} $check_env = ' checked'; } ($dev,$ino,$mode,$nlink,$uid,$gid,$rdev,$size,$atime,$mtime,$ctime,$blksize,$blocks) = stat $ENV{'SCRIPT_FILENAME'}; if($IN::rd eq '1'){$check_rdir = ' checked'} print header,start_html(-title=>'perlvars',-bgcolor=>"$bgc",-text=>"$btext",-link=>"$blink",-vlink=>"$bvlink"); print<

HTML if(($IN::env eq '1') or ($show_env eq '1')){ print< HTML } if(($IN::mods eq '1') or ($show_mod eq '1')){ print< HTML } for(@INC){ if($_ eq '.'){ pop(@INC)}} print<


Perl Variables

HTML if($ENV{'HTTP_USER_AGENT'}=~/MSIE/){ print< HTML } print '
$envtotal Environment Variables
$modtotal Installed Modules
version$]
executable$^X
INC HTML for(@INC){ print qq~$_~} print<.
readdir HTML unless($show_dir eq '1'){ print<

HTML } if(($IN::rd eq '1') or ($show_dir eq '1')){ print<

File size in bytes is shown for text files larger than 30k as a warning that they may exceed a common html browser textarea limit.

IE may attempt to execute perl scripts if the file extension is associated with perl when "view" is used. Try "edit" instead.
'; } print qq~
~} else { print qq~$tc>~} if(($IN::rd eq '1') or ($show_dir eq '1')){ opendir THIS, "." or die "$!"; while(defined ($_ = readdir(THIS))){ next if $_ =~ /^\.\.?$/; next if -d $_; # if(-d $_){$_='/'.$_}; # list dirs push @dir, $_; } closedir THIS; @dir = sort { lc($a) cmp lc($b) } @dir; print<
  Current directory
 
HTML } unless(($IN::rd) or ($show_dir eq '1')){ print "$Bin" } print< $url OS $^O localtime $ltime   sec=$sec, min=$min, hour=$hour, mday=$mday, mon=$mon, year=$year, wday=$wday, yday=$yday, isdst=$isdst '; if(($IN::env eq '1') or ($show_env eq '1')){ for(keys(%ENV)){ unless($ENV{$_} eq ''){ if($_=~/DOCUMENT_ROOT|PWD|WINDIR|SCRIPT_FILENAME/){ print ""; } elsif($_=~/PATH/){ %ENV->{$_}=~s/;/; /g; print ""; } elsif($_=~/REMOTE_ADDR/){ print ""; } elsif($_=~/SERVER_ADMIN/){ print ""; } elsif($_!~/DOCUMENT_ROOT|PWD|WINDIR|SCRIPT_FILENAME|PATH|REMOTE_ADDR|SERVER_ADMIN/){ print ""; } } } } print "


$envtotal Environment Variables
HTML unless($show_env eq '1'){ print< HTML } print '

$_  {$_} . "\">" . %ENV->{$_} . "
$_  " . %ENV->{$_} . "
$_  {$_} . "\">" . %ENV->{$_} . "
$_  {$_} . "\">" . %ENV->{$_} . "
$_  " . %ENV->{$_} . "
"; print<


$modtotal Installed Modules
HTML unless($show_mod eq '1'){ print< HTML } if(($IN::mods eq '1') or ($show_mod eq '1')){ print qq~ ~; } if(($filter eq '1') && (($IN::mods eq '1') or ($show_mod eq '1'))){ print qq~
$mt2 displayed, $mt3 filtered (~; for(@filtered){ print "$_ "} print ')'; } if(($IN::mods eq '1') or ($show_mod eq '1')){ print qq~
Modules without pod * are linked directly to the module.~; } print ''; unless(($IN::mods eq '1') or ($show_mod eq '1')){ print ''} if(($IN::mods eq '1') or ($show_mod eq '1')){ if($filter eq '1'){$modtotal=$mt2} $third = $modtotal/3; $count=0; print qq~~; foreach $mod(@foundmods){ &make($mod); $count++; if($count <= $third){ print qq~~; } else{ push (@mod1,$mod) } } print qq~
$root
~; $count=0; foreach $mod1(@mod1){ &make($mod1); $count++; if($count <= $third){ print qq~~; } else{ push (@mod2,$mod1) } } print qq~
$root
~; $count=0; foreach $mod2(@mod2){ &make($mod2); $count++; if($count <= $third){ print qq~~; } } print qq~
$root
~; } unless(($IN::mods eq '1') or ($show_mod eq '1')){ print qq~~; } print qq~
~; $end=(times)[0]; printf "That took %.2F CPU seconds.", $end - $start; print qq~
~; print end_html; exit(0); sub findmodules{ for(@INC){ if($_ eq '.'){ pop(@INC)}} @path{@INC} = (); # modlist.pl - http://www.crusoe.net/~jeffp/programs/modlist for $base (@INC) { find(\&modules, $base) } # ditto $check_mods = ' checked'; $modtotal = @foundmods; if($filter eq '1'){ for(@foundmods){ $off=0; $mname=$_; @fm = split(/::/,$_); $fm = shift(@fm); for(@filtered){ if($_ eq $fm){$off=1}} unless($off==1){$fms{$mname}=1} } @foundmods = keys(%fms); $mt2 = @foundmods; $mt3 = ($modtotal-$mt2); # $modtotal = $mt2; } @foundmods = sort { lc($a) cmp lc($b) } (@foundmods); } sub modules { # sub from modlist.pl - http://www.crusoe.net/~jeffp/programs/modlist $File::Find::prune = 1, return if exists $path{$File::Find::dir} and $File::Find::dir ne $base; $f = $File::Find::name; # < added if($f=~/\.pm$/){ # push @files, $f; # } # @files = sort @files; # < my $module = substr $File::Find::name, length $base; return unless $module =~ s/\.pm$//; $module =~ s!^/+!!; $module =~ s!/!::!g; # $module =~ tr!A-Z!a-z!; push @foundmods, $module; # print qq~

  • $module~; } sub make{ my$var = shift; my@smn = split(/::/,$var); my$name = pop(@smn); my$foo = shift(@smn); for(@files){ if($_=~/$foo.*$name\.pm/){ unless($modpath{$var}){$modpath{$var}=$_} } } for(keys(%moddat)){ if($_ eq $var){ if($moddat{$_}==1){ $root=qq($var); } if($moddat{$_}==0){ $root=qq($var$markpm); } } } } sub updatedat{ &makedat(); print header,start_html(-title=>"module list updated",-bgcolor=>"$bgc",-text=>"$btext",-link=>"$blink",-vlink=>"$bvlink"),h1('Updated'),p("Module list $dat updated with $modtotal modules"),hr,p(a({-href=>"$url"},'Reload')); } sub makedat{ &findmodules(); for(@foundmods){ my$var = $_; my@smn = split(/::/,$var); my$lname = pop(@smn); my$foo = pop(@smn); for(@files){ if($_=~/$foo.$lname\.pm/){ open(IT,"< $_") or next; local $/ = undef; my$it=; close(IT) or die "Couldn't close module: $!"; if($it=~/=cut/ or /=head/ or /=item/){$rec{$var}=1} else{$rec{$var}=0} } } } open(FILE,"> $dat") or die "$!"; print FILE qq~%moddat=(~; for(sort { lc($a) cmp lc($b) } keys(%rec)){ print FILE qq~$_=>$rec{$_},~; } print FILE qq~);~; close(FILE) or die "$!"; } sub perlmod{ open(MU,"< $IN::pm") or die "$!"; local $/ = undef; my$it=; close(MU) or die "$!"; print header(-type=>'text/html'),start_html(-title=>"$IN::title",-bgcolor=>"$bgc",-text=>"$btext",-link=>"$blink",-vlink=>"$bvlink"),pre("$it");exit } sub perldoc{ $pod = $^T; pod2html( "--htmlroot=$Bin", "--infile=$_[0]", "--outfile=$pod.html", "--title=$_[1]", ); open(IT,"< $pod.html") or die "$!"; local $/ = undef; my$it=; close(IT) or die "$!"; unlink "$pod.html"; print header(-type=>'text/html'),start_html(-title=>"$IN::title",-bgcolor=>"$bgc",-text=>"$btext",-link=>"$blink",-vlink=>"$bvlink"),p("$it");exit } sub check_type{ $file=$IN::filename; if(-B $file){$type='binary'} if(-T $file){$type='text/plain'} if( $file=~/\.s?html?/i){$type='text/html'} if($file=~/\.gif/i){$type='image/gif'} if($file=~/.jpe?g?/i){$type='image/jpeg'} } sub view_file{ &check_type(); if($type=~/text/){ print header(-type=>"$type"); if($type=~/html/){ print start_html(-title=>"viewing $IN::filename",-bgcolor=>"$bgc",-text=>"$btext",-link=>"$blink",-vlink=>"$bvlink")} open(FILE, "< $file") or die "$!"; local $/ = undef; my$data = ; close(FILE) or die "$!"; print $data; } if($type=~/image/){ print "Location: $file\n\n"; } if($type=~/binary/){ print header,start_html(-title=>'Unsupported Binary',-bgcolor=>"$bgc",-text=>"$btext",-link=>"$blink",-vlink=>"$bvlink"),h1('Unsupported Binary'),p('Only GIF and JPEG binaries can be viewed.'),hr,("Ok"); } exit; } sub edit_file{ &check_type(); if($type =~ /image/){ print header,start_html(-title=>'Feature not yet implemented',-bgcolor=>"$bgc",-text=>"$btext",-link=>"$blink",-vlink=>"$bvlink"),p,br,blockquote,p("Images can only be viewed"); exit; } if($type=~/binary/){ print header,start_html(-title=>'Unsupported Binary',-bgcolor=>"$bgc",-text=>"$btext",-link=>"$blink",-vlink=>"$bvlink"),h1('Unsupported Binary'),p('Only text files can be edited.'),hr("Ok"); exit; } $fs = -s $IN::filename; open (FILE, "< $IN::filename") or die "$!"; local $/ = undef; my$file = ; close(FILE) or die "$!"; &encode_entities($file); print header,start_html(-title=>"editing $IN::filename",-bgcolor=>"$bgc",-text=>"$btext",-link=>"$blink",-vlink=>"$bvlink"); my$fc = (length($IN::filename)+3); ($fL = $0) =~ s/$rurl$//o; print<
    HTML if($fs <= 30000){ print qq~($fs bytes) in $fL~; } if($fs >= 30000){ print<($fs bytes) in $fL
    If a large text file was selected and the textarea is empty your browser
    has a textarea limit. This warning appears on files >= 30k.
    HTML } print<$file<\/textarea> HTML print end_html; exit; } sub stat_file{ ($dev,$ino,$mode,$nlink,$uid,$gid,$rdev,$size,$atime,$mtime,$ctime,$blksize,$blocks) = stat $IN::filename; print header,start_html(-title=>"stat $IN::filename",-bgcolor=>"$bgc",-text=>"$btext",-link=>"$blink",-vlink=>"$bvlink"); print<

    results of stat $IN::filename

    FieldValue Device number of filesystem $dev   Inode number $ino   File mode (type and permissions) $mode   Number of (hard) links to the file $nlink   Numeric user ID of file's owner $uid   Numeric group ID of file's owner $gid   The device identifier (special files only) $rdev   Total size of file, in bytes $size   Last access time since the epoch $atime   Last modify time since the epoch $mtime   Inode change time (NOT creation time!) since the epoch $ctime   Preferred blocksize for file system I/O $blksize   Actual number of blocks allocated $blocks   Current time since the epoch $^T   ok HTML print end_html; exit; } sub save_file{ &decode_entities($IN::filebody); open (FILE, "> $IN::filename") or die "$!"; print FILE $IN::filebody; close(FILE) or die "$!"; print header,start_html(-title=>'File Saved',-bgcolor=>"$bgc",-text=>"$btext",-link=>"$blink",-vlink=>"$bvlink"),h1('File Saved'); print qq~$IN::filename saved in $Bin
    return~; exit; } sub selfurls{ $url3 = url(-absolute=>1); $url4 = url(-path_info=>1); $url5 = url(-path_info=>1,-query=>1); ($url5 = $url5) =~ s/%([a-fA-F0-9]{2})/chr(hex($1))/eg; $url5 =~ tr/ /+/; print header,start_html(-title=>'urls',-bgcolor=>"$bgc",-text=>"$btext",-link=>"$blink",-vlink=>"$bvlink"); print table({-border=>"$tbd",-bgcolor=>"$tc",-cellspacing=>'1',-cellpadding=>'4'}, Tr([td({-colspan=>'2',-bgcolor=>"$ta"},font({-size=>'+2'},b(tt('Full Path to this script by various methods'))))]), Tr({-align=>'left',-bgcolor=>'dddddd'},[th('method').th('result')]), Tr([td({-colspan=>'2',-bgcolor=>"$tb"},small(b('system paths')))]), Tr([td(tt('$0')).td($0)]), Tr([td(tt('FindBin($Bin)')).td($Bin)]), Tr([td(tt('rel2abs($0)')).td(rel2abs($0))]), Tr([td({-colspan=>'2',-bgcolor=>"$tb"},small(b('web paths')))]), Tr([td("\$ENV{'SCRIPT_NAME'}").td($ENV{'SCRIPT_NAME'})]), Tr([td(tt('$q->url()')).td($url)]), Tr([td(tt('$q->url(-relative=>1)')).td($rurl)]), Tr([td(tt('$q->url(-absolute=>1)')).td($url3)]), Tr([td(tt('$q->url(-path_info=>1)')).td($url4)]), Tr([td(tt('$q->url(-path_info=>1,-query=>1)')).td(small("$url5"))]), Tr([td({-colspan=>'2',-bgcolor=>"$tb",-align=>'right'},"ok")]), ); print end_html; } sub configure{ if($show_dir eq '1'){$checkp_dir=' checked'} if($show_env eq '1'){$checkp_env=' checked'} if($show_mod eq '1'){$checkp_mod=' checked'} if($filter eq '1'){$checkp_filter=' checked'} print header,start_html(-title=>'configure perlvars.pm',-bgcolor=>"$bgc",-text=>"$btext",-link=>"$blink",-vlink=>"$bvlink"); print<

    Configure perlvars

    Display Preferences
    If expand is checked the section will be expanded by default, otherwise a check box and expand button will appear near the section title.
    sectionexpand
    readdir
    environment variables
    installed modules
     
    • mark non-pod links with a
    • windowname for mod docs
    • filter modules
      module dirs excluded from display
    elementvalue
    background color
    text color
    link color
    visited link color
    table shade light
    table shade medium
    table shade dark
    table border
    will overwrite current config with a fresh default. HTML exit; } sub info{ print header,start_html(-title=>"$rurl",-bgcolor=>"$bgc",-text=>"$btext",-link=>"$blink",-vlink=>"$bvlink"); print<

    $rurl


    perlvars is based on the concept of ScriptSolutions' great free unix-oriented perl diver which lists useful environment variables and installed perl modules. It was written so these functions could be expanded upon and enjoyed under Win32 as well as Unix systems.

    Also in perlvars:

    • Listed modules are linked to their documentation. Either an html rendering of the pod or raw module source code.

    • Control panel to customize preferences.

    • Automatic configuration file creation if either $cfg or $dat are not found in the current directory.

    • Directory browser that can view, edit and stat local files. Stat applies to all files, text files with any extension can be viewed and edited, jpeg and gif images can be viewed. Unfortunately, text files larger than a given browser software's textarea limit cannot be edited.

    Credits:

    Coded by epoptai
    modlist.pl - getting a list of modules
    perldiver.pl- splitting module table into thirds


    Ok

    HTML print end_html; } sub config_save{ %cfg = ( bgc => "\$bgc='$IN::bgc';", btext => "\$btext='$IN::btext';", blink => "\$blink='$IN::blink';", bvlink => "\$bvlink='$IN::bvlink';", tc => "\$tc='$IN::tc';", tb => "\$tb='$IN::tb';", ta => "\$ta='$IN::ta';", tbd => "\$tbd='$IN::tbd';", dir => "\$show_dir='$IN::show_dir';", env => "\$show_env='$IN::show_env';", mod => "\$show_mod='$IN::show_mod';", target => "\$target='$IN::target';", markpm => "\$markpm='$IN::markpm';", filter => "\$filter='$IN::filter';", filtered => "\@filtered=qw($IN::filtered);", ); open (FILE, "> $cfg") or die "Problem saving config file: $!"; for(keys(%cfg)){ print FILE qq($cfg{$_}) } print FILE "\n1;\n"; close(FILE) or die "$!"; print header,start_html(-title=>'Configuration Saved',-bgcolor=>"$bgc",-text=>"$btext",-link=>"$blink",-vlink=>"$bvlink"); if($IN::show_dir eq '1'){$showdir='yes'} else {$showdir='no'} if($IN::show_env eq '1'){$showenv='yes'} else {$showenv='no'} if($IN::show_mod eq '1'){$showmod='yes'} else {$showmod='no'} if($IN::filter eq '1'){$filt='yes'} else {$filt='no'} if($IN::diag eq '1'){$dia='On'} elsif($IN::diag eq '0'){$dia='Off'} print<

    Configuration Saved

    Display Preferences
    sectionexpand
    readdir$showdir
    environment variables$showenv
    installed modules$showmod
     
    display window$target
    marking non-pod links with a$IN::markpm
    filter modules$filt
    filtered modules$IN::filtered 
    elementvalue
    background color$IN::bgc
    text color$IN::btext
    link color$IN::blink
    visited link color$IN::bvlink
    table shade light$IN::tc
    table shade medium$IN::tb
    table shade dark$IN::ta
    table border$IN::tbd
      HTML print end_html; exit } sub install{ print header,start_html(-title=>'install perlvars.pm'),h1('Installation'); if($IN::go eq 'restore default'){ print 'Default configuration restored' } print ol; if(($init==1)){ print qq~
  • Data file $cfg not detected in $path
    ~; } if(($init==1) or ($IN::go eq 'restore default')){ while(){$df .= $_} # read data open (FILE, "> $cfg") or die "$!"; print FILE $df; close(FILE) or die "$!"; print qq~
  • Created data file $path/$cfg~; } if($nodat==1){ print qq~
  • Data file $dat not detected in $path
    ~; &makedat(); print qq~
  • Created data file $path/$dat~; } print<
    Ready to reload or configure HTML exit } __END__ $bgc='#d0d0d0'; $btext='#000000'; $blink='#0000ff'; $bvlink='#6600aa'; $tbd='0'; $tc='#c0c0c0'; $tb='#b0b0b0'; $ta='#a0a0a0'; $show_dir=''; $show_env='1'; $show_mod='1'; $markpm='*'; $target='docs'; $filter=''; @filtered=qw(Tk);