#!/usr/bin/perl

I sat down to rewrite this bit of embarrassing code in Perl.  In my vi session, I typed

#!/usr/bin/perl
use strict;


And that’s as far as I got.  I had forgotten perl.  I haven’t used in in maybe 4 years and I had completely forgotten the entire perl language.

So after reviewing my Perl Resource Kit from O’Reilly (the Lizard books), I managed to sit down and get this banged out:

#!/usr/bin/perl

# use strict
use strict;

# Use MySQL connection library
use Mysql;

# use CPAN's date::Format and date::Parse libraries
use date::Format;
use date::Parse;

# sub to determine current local time zone
sub calcLclTimeZone {
        my $date_str = $_[0];
        my $dst_start;
        my $dst_end;

        # get the year from the arg
        my $date = str2time($date_str);
        my $year = time2str("%Y", $date);

        # Perl doesn't have a case select construct, so brute force the year
        if ($year == 2009 || $year == 2015 || year = 2020) {
                $dst_start = str2time("March 8, " . $year);
                $dst_end   = str2time("November 1, " . $year);
        }

        elsif ($year == 2008 || $year == 2014 || $year == 2025) {
                $dst_start = str2time("March 9, " . $year);
                $dst_end   = str2time("November 2, " . $year);
        }

        elsif ($year == 2013 || $year == 2019 || $year == 2024) {
                $dst_start = str2time("March 10, " . $year);
                $dst_end   = str2time("November 3, " . $year);
        }

        elsif ($year == 2007 || $year == 2012 || $year == 2018) {
                $dst_start = str2time("March 11, " . $year);
                $dst_end   = str2time("November 4, " . $year);
        }

        elsif ($year == 2017 || $year == 2023) {
                $dst_start = str2time("March 12, " . $year);
                $dst_end   = str2time("November 5, " . $year);
        }

        elsif ($year == 2011 || $year == 2016 || $year == 2022) {
                $dst_start = str2time("March 13, " . $year);
                $dst_end   = str2time("November 6, " . $year);
        }

        else {
                $dst_start = str2time("March 14, " . $year);
                $dst_end   = str2time("November 7, " . $year);
        }

        # figure out if our date is in the CDT range for the given year
        if ($date >= $dst_start && $date <= $dst_end) {
                return 'CDT';
        }

        else {
                return 'CST';
        }

}

# MySQL config variables
$host     = "";
$user     = "";
$database = "CLICKSTREAM_OLTP";
$pw       = "";

# Connect to MySQL
$connect = Mysql->connect($host, $database, $user, $pw);

# Select the transaction schema
$connect->selectdb($database);

# query the transaction schema
$sql = "SELECT 'TIMESTAMP', 1MINLOAD, 5MINLOAD, 15MINLOAD, SWAP, FREE, BUFFER, CACHE,
 SWAPIN, SWAPOUT, IOIN, IOOUT, USER, SYS, IDLE, IOWAIT, INFOCON FROM CPU_MEMORY";

$execute = $connect->query($sql);

# lock the tables
$lock_query = "LOCK TABLES CLICKSTREAM_OLTP.CPU_MEMORY READ, 
CLICKSTREAM_OLTP.CPU_MEMORY C1 WRITE, 00CLICKSTR.01CPUFACT WRITE, 
00CLICKSTR.02LCLDTEDM READ, 00CLICKSTR.02LCLTMEDM READ, 00CLICKSTR.02GMTDTEDM READ, 
00CLICKSTR.02GMTTMEDM READ, mysql.time_zone_name READ, mysql.time_zone READ, 
mysql.time_zone_transition_type READ, mysql.time_zone_transition READ");

$lock_execute = $connect->query($lock_query);

# loop through rows in transaction schema CPU table
while (@row = $execute->fetchrow()) {
        $sql          = "SELECT id02LCLDTEDM FROM 00CLICKSTR.02LCLDTEDM WHERE 
00CLICKSTR.03SQLDATE = DATE('" . $row[0] . "') LIMIT 1";
        $date_exec    = $connect->query($sql);
        @date_row     = $date_exec->fetchrow();
        $localdatekey = $date_row[0];

        $sql          = "SELECT id02LCLTMEDM FROM 00CLICKSTR.02LCLTMEDM WHERE 
00CLICKSTR.03HOURS24 = HOUR('" . $row[0] . "') AND 00CLICKSTR.03MINUTES = 
MINUTE('" . $row[0] . "') AND 00CLICKSTR.03SECONDS = SECOND('" . $row[0] . "') 
LIMIT 1";
        $time_exec    = $connect->query($sql);
        @time_row     = $time_exec->fetchrow();
        $localtimekey = $time_row[0];

It’s obviously nowhere near finished.  But I’m remembering how much easier Perl is than PHP and how much more fun it is to write perl code.

Advertisements
  1. #1 by Joshua on November 8, 2009 - 7:11 PM

    And how one can easily tell a scalar from an array from a hash: $scalar; @array; $array[0]; %hash; $hash{‘key’};

    • #2 by Joshua on November 8, 2009 - 7:12 PM

      See, Scalar starts with S and $ kinda looks like S. And Array starts with A and @ kinda looks like “a.” And % kinda looks like a plate of hash…

      • #3 by Chadwick on November 8, 2009 - 9:33 PM

        You’re a silly bastard.

      • #4 by Joshua on November 9, 2009 - 8:13 AM

        Well, I would think the hash would use the actual hash sign (#) but that’s already taken for comments.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: