#!/usr/bin/perl # HTMLized Perl commentized by "rem" # #

HTMLized Perl

As long as you remember to download this page via "Save Page As" (vs cutandpaste) it will work just fine running as coolman.pl or viewable as coolman.htm. In addition, if you pipe coolman -h | txt2man, you should get a passable manpage Out-to-lunch? Maybe. But still fun... and one step closer to writing one piece of text which will compile as C, run as Perl, display as HTML or man page and feed the cat!

The listing begins below, with the shebang (#!) comment (harmlessly?) repeated for clarity.



#!/usr/bin/perl  
# does =not= use -s switch!
# cf OReilly Hacks p.75 mod JfC  this one makes the typing of switches minimal (and optional) (removed -w to lessen whining)

use strict; 

use Term::ANSIColor qw(:constants);

my $version = "0.2";

sub usage {
  print STDOUT "
USAGE
  coolman {[-[modifier][color][oncolor]] 'match expression'}

  modifier: rev[erse] [under]line blink bold dark
  color   : red yellow green blue magenta cyan black white
  oncolor : onred onyellow ongreen onblue onmagenta oncyan onblack onwhte

EXAMPLES

  cat /etc/fstab |coolor -blinkredoncyan '/(dev|mnt)/\w*' #/dev or /mnt
  cat /etc/hosts |coolor -boldblue '\\d+\\.\\d+\\.\\d+\\.\\d+' #hostaddresses
  cat local/bin/coolor | coolor -redoncyan '\\$\\w+'      #show vars
  cat ~/bashrc |coolor -reverse '(a|e|i|o|u)' | less -r   #keep colors w/less
  cat /etc/exports |coolor -boldblack '#.*'               #dim comments

  cat /etc/rc.d/rc.S |coolor JfC                    #my defaults
  cat text.txt |coolor -red ly er est -blue ed ing  #multiple args per color
  coolor -help |coolor -reverse '#.*' -red red -blue blue  #silly, but not plain silly
  
AUTHOR  
    
JfC 2006 : $version : $0   
    
";
die "ok\n";
}

my %target= ();
my $clr='UNDERLINE . BOLD . YELLOW';
my $arg='';
my $test='';
my $x='';
my $rst=RESET;

#"ROYGBVroygbv"
# Newton "felt" like there should be seven colours (like 7 notes on the scale), 
# so indigo was added. Nice poetry, Occam not withstanding.
# Historically speaking Richard Of York may well have Given Battle Vainly, 
# but not in vain.

while ( $test = shift) {
  chomp($test);
  if ($test =~ /^-/) {
    &usage if $test=~/help/;
    $clr = $test;
    $clr =~ s:^-::; $clr=~tr/[a-z]/[A-Z]/;
    $clr =~ s:BOLD:BOLD\ .\ :;
    $clr =~ s:DARK:DARK\ .\ :;
    $clr =~ s:BLINK:BLINK\ .\ :;
    $clr =~ s:REVE?R?S?E?:REVERSE\ .\ :;
    $clr =~ s:U?N?D?E?R?LINE:UNDERLINE\ .\ :;
    $clr =~ s:ON: . ON_:;
    $clr =~ s: . $::; # trim trailing eg -underline alone opposed to -underlinered
       
  } else {
    $arg = $test;
    $target{$arg} = eval($clr);
  
  
  }

}

#	my $rst = RESET;
	while (<>) {
	  foreach my $x (keys(%target)) {
	     s/($x)/$target{$x}$1$rst/g;
	  }
	  print;
	}


__END__
###

Revision History

	v0.0	RCG as noted above
	v0.1	coolor : naturalized the calling syntax, reduced typing
	v0.2	imbedded HTML documentation

	Coolman Homepage : http://www.zyqote.com/Linux/pub/coolman.pl.htm

Discussion

Don't try this w/ all your Perl code. For one thing > and < comparsons suffer, as does any pslurping, as do any shell redirects. In short, only a very select fraction of Perl will display correctly after being HTMLized in this way -- or run properly if it does display.

Reading Rob Flickenger's Linux Server Hacks #75 (Colorized log analysis) reminded me of the moment I made the leap to Linux. When I left university programming meant shuffling decks of punchcards, but I had been frustrated w/ one aspect of DOS from the day I was old enough to say "dir": I wanted to be able to spot my data in a sea of other files. I even went so far as to start to write a COMMAND.COM improvement, which I dubbed FIAT.COM which "improved" upon DOS by colorizing all directory listings, before passing other commands on to an exec call. Then I saw Linux. It was doing colorized ls right out of the box. I switched OSes that moment and have never looked back. True story.

The "moral" of this story (I think) is: Little features matter bigtime. From not obfustcating code, to easy-to-type switches, to nicely formatted output: nowadays, its called ergometric interface design. It used to be called craftmanship: building the car =you'd= like to drive yourself; the house you'd like to live in; the code you'd like to run...

Ys,,,jef <honour a nl.rogers.com>

Health Warning This is pet code only. It does a reckless eval on raw commandline input. It should never be run setuid, or at all, for that matter, outside the sandbox. If you don't know what the above means, please don't install it.