Beefy Boxes and Bandwidth Generously Provided by pair Networks
Problems? Is your data what you think it is?
 
PerlMonks  

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).

##sq_proxy.pl
##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.
##


#!/usr/bin/perl

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";
$driverout="Pg";
$hostout="raider-ralph";

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



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");
$numdone_stmnt->execute();
$num_arry=$dbhout->selectrow_array($numdone_stmnt);


if ($num_arry eq 0)
  {
          $destination_counter = 1;
          $source_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_stmnt->execute;
         @lastdone_arry = $dbhout->selectrow_array($lastdone_stmnt);
         $destination_counter=@lastdone_arry[0];
         
         ##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')");
         $lastcnt_stmnt->execute;
         @lastdone_arry=$dbhout->selectrow_array($lastcnt_stmnt);
         $last_done=@lastdone_arry[0];

                   $source_counter=@src_arry[0];
                   $source_counter++;
          }
 }



  
     
###manual specification of the counter variables here allows for the script to be run for predetermined values in appropriate
###circumstances.
##$counter=156169;
##$count=1275146;


##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_stmnt->execute();

$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
perl2html
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.

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

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:/text.pl"; 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 $_; }

    cheers

    tachyon

    s&&rsenoyhcatreve&&&s&n.+t&"$'$`$\"$\&"&ee&&y&srve&&d&&print

Re: neat little win32 freeware utility
by Chmrr (Vicar) on Sep 13, 2001 at 09:17 UTC
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

    s]]HrLfbfe|EbBibmv]e|s}w}ciZx^RYhL}e^print

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others making s'mores by the fire in the courtyard of the Monastery: (10)
As of 2015-07-07 08:25 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    The top three priorities of my open tasks are (in descending order of likelihood to be worked on) ...









    Results (87 votes), past polls