Beefy Boxes and Bandwidth Generously Provided by pair Networks
Keep It Simple, Stupid

neat little win32 freeware utility

by ralphie (Friar)
on Sep 13, 2001 at 04:36 UTC ( #112077=perlmeditation: print w/ replies, xml ) Need Help??

i was just browsing through today's lockergnome and ran across this little utility, codex,
available here. it formats code in 20-30 languages
(including perl, of course) and will export it to either html or rtf. i just ran one of my own pieces through
it ... following is an excerpt of the output. kind of fun to play with.

i realize that some less-responsible monks might use this to easily format their posted code in
outrageous colors...only your own innate sense of responsibility can keep you from such a path (grin).
##this script is run to generate the squid table that is optimized for analysis (sq_proxy).
##the script is structured so it can be run either to update the analysis (sq_proxy) table 
##with the interpolated values based on the records in the accesslog table that are more recent
##than the last update, or to populate the database from scratch.


use strict;

use DBI;
use DBD::mysql;
use Date::Manip;
use DBD::Pg;

##initialize scalars
##(some of these may have been deprecated)

my $destination_counter;
my $source_counter;
##used as proxies or flags within the script itself
my $holiday1;
my $holiday2;
my $holiday_today;
my $holiday_yesterday;

##generated proxy variables stored in output table
my $monday;
my $workday;
##used to generate the request's position relative to 7am that morning
my $num_hrs;
my $num_mins;
my $num_secs;

##hold values fomatted for given functions
my $day_format;
my $yday_format;

##quite possibly no longer relevant
my $starttime;

##output table
my $basetime;
my $day;
my $test;
my $code;
my $secs;

my $cyear;

##scalars for connect statement, drivers and hosts should correspond to your own environment
$driverin = "mysql";
$hostin = "raider-ralph";

##the following two are blank to protect the identities of the innocent <grin>
$user = '<suerid>';
$password = '<password>;

my $line;
#dsn statement for source of records
$dsnin = "DBI:$driverin:database=$database:host=$hostin";
#dsn statement for destination transformed data
$dsnout = "dbi:$driverout:dbname=$database;host=$hostout";

#establish connections

$dbhin = DBI->connect($dsnin,$user,$password);
$dbhout = DBI->connect($dsnout,$user);

##prepare sql statements executed within loops
$rec_stmnt=$dbhin->prepare("select * from accesslog where count = ?");
$ins_stmnt=$dbhout->prepare("insert into sq_proxy (time,day,monday,workday,basetime,elapsed,bytes,j_date,code,count) values (?,?,?,?,?,?,?,?,?,?)");

##determine how many records are in the transformed file ...if there are none, initialize the counter scalars to 1
$numdone_stmnt=$dbhout->prepare("select count(time) from sq_proxy");

if ($num_arry eq 0)
          $destination_counter = 1;

##the following block determines the relative postion of the last record processed in the source and destination 
##tables and sets the counter variables appropriately.
if ($num_arry gt 0)
         ##find the last record in the destination table and assign that value to the destination counter
         $lastdone_stmnt=$dbhout->prepare("select max(to_number(count,'9999999999')) from sq_proxy");
         @lastdone_arry = $dbhout->selectrow_array($lastdone_stmnt);
         ##determine the time value for that record
         $lastcnt_stmnt=$dbhout->prepare("select time from sq_proxy where to_number(count,'999999999')=to_number($destination_counter,'9999999999')");


###manual specification of the counter variables here allows for the script to be run for predetermined values in appropriate

##determine the value of the count field in the last record in the accesslog table;
$max_stmnt = $dbhin->prepare("select max(count) from accesslog");

$max_arry = $dbhin->selectrow_array($max_stmnt);

##outer loop for stepping through source file

Edited by boo_radley : fixed formatting

Edit: chipmunk 2001-09-12

Comment on neat little win32 freeware utility
Replies are listed 'Best First'.
Re: neat little win32 freeware utility
by Chmrr (Vicar) on Sep 13, 2001 at 09:17 UTC
Re: neat little win32 freeware utility
by tachyon (Chancellor) on Sep 13, 2001 at 08:55 UTC

    Here is a very short Perl snippet that does Text (including code) to HTML. It will also escape the [ and ] for you so is PM HTML compatible!

    my $text = "c:/"; open TEXT, $text or die "Oops can't open $text $!"; open HTML, ">$text.htm" or die "Oops can't write $text.htm $!"; print HTML "<pre>\n"; while (<TEXT>) { $_ = escapeHTML($_); print HTML $_; } print HTML "</pre>\n"; close HTML; close TEXT; sub escapeHTML { local $_ = shift; # make the required escapes s/&/&amp/g; s/"/&quot;/g; s/</&lt;/g; s/>/&gt;/g; # change tabs to 4 spaces s/\t/ /g; # make the whitespace escapes - not required within <pre> tags s/( {2,})/"&nbsp;" x length $1/eg; # make the brower bugfix escapes; s/\x8b/&#139;/g; s/\x9b/&#155;/g; # make the PERL MONKS escapes (if desired) s/\[/&#091;/g; s/\]/&#093;/g; # change newlines to <br> if desired - not required with <pre> # s/\n/<br>\n/g; return $_; }




by jepri (Parson) on Sep 13, 2001 at 05:48 UTC
    I found a neat little utility that came with Debian called perl2html which does a similar job. I use it to format my programs before I post them to a webpage.

    I didn't believe in evil until I dated it.

Re: neat little win32 freeware utility
by MrNobo1024 (Hermit) on Sep 13, 2001 at 18:59 UTC
    Unfortunately, it's impossible to parse Perl code, and therefore also impossible to (reliably) highlight it. Consider this snippet:
    BEGIN { if(time % 2) { eval 'sub foo(){}' } else { eval 'sub foo($){}' + } } foo /1; # is this a comment or not? /;
    The "comment" is only a comment if the time the program was run was an odd number.

    -- MrNobo1024


Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: perlmeditation [id://112077]
Approved by root
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others pondering the Monastery: (2)
As of 2015-10-04 03:53 GMT
Find Nodes?
    Voting Booth?

    Does Humor Belong in Programming?

    Results (98 votes), past polls