NB: These pages were mostly written in 2001 or so. The résumé dates are accurate but the code is aged and unlike whiskey, 8 year-old code doesn't usually taste better. For a look at my current skills and to see my CPAN modules, sample code, and code discussions, please see these pages instead: Perl resources and sample code and PangyreSoft.
Coloring perl code in HTML
Social links
View Ashley Pond V's profile on LinkedIn
Miscellaneous

Other pages

Description

As is so often the case, a difficult task is mostly done already with Perl. Syntax::Highlight::Perl is a module by Cory Johns that parses code and provides you with hooks for manipulating how it will print.

To get nicely colored perl code, we just need to define the colors to match the callback names and insert the hooks for each. The script sends the HTML to STDOUT, so you have to pipe it to a file for display. Or you could adapt the code for a CGI to view scripts dynamically.

The script below is based on a much more dramatic version by the author of the module.

Code
Usage
jinx[12]>perl-colorizer
Give me a perl file to colorize!
jinx[13]>perl-colorizer ~/perl/stock-quote > /www/htdocs/stock-quote.html

Which gets us a chance to show another cool short script and the very pretty result of the colorizer. Now when I point my browser to http://localhost/stock-quote.html, I see the following.

Discussion

It is pretty easy to add File::Find into the mix to create trees of linked scripts so you can auto-index and see all your own code however you like it. I have such a script but it’s too sloptastic and half-done to put forward.

A quick after word: I used 6 digit hex for the colors originally, eg, “#009933;”. This is fine but redundant for web-safe colors because CSS supports using singles in place of doubles, eg, “#093;”.

Here’s a slightly dirty (don’t use it if you don’t know how it works) one-liner to prune 6 place hexadecimal to 3 places.

perl -pi.bk -e 's/([0-9A-F])\1(?=[0-9A-F]{2}|;)/$1/gi' [file_list]

Search these pages via Google
Text, original code, fonts, and graphics ©1990-2009 Ashley Pond V.