Skip to content


Show Time/Date/Weather in Perl

This little console program scrapes from and prints to the screen. A handy little program I often use to tell the time in other cities.

sub getCityTime {  
  use LWP::Simple;
  my %hash;

  #new york

  $url = "";
  %hash = ( 'newyork', '179', 
            'darwin',   '72', 
            'tokyo',    '248', 
            'lima',     '131'
  my $html = get($url.$hash{$_[0]})
    or die "Couldn't connect to the website.";
  $html =~ m!<span>([\s\w,;']*)</span>!;
  print "Location ======&gt; ".$1."\n";  

  $html =~ m!<strong>([\s\w:,]*)</strong>!;
  print "Date/Time =====&gt; ".$1."\n";  

  $html =~ m!<strong>(\d{1,3})[\s\w:,;&amp;]*</strong>!;
  print "Temperature ===&gt; ".$1.chr(167)."F\n";  



if ($ARGV[0] ne "") {
  getCityTime( $ARGV[0] );
else {

  print "\nDisplays the time from, based on city.\n\n";
  getCityTime( "newyork" );
  print "==============================\n";
  getCityTime( "lima" );
  print "==============================\n";
  getCityTime( "tokyo" );
  print "==============================\n";
  getCityTime( "darwin" );  


Convert Minutes to Hours

I often use both Winamp and my iPhone to listen to music. These two, unfortunately, show the time differently in the songs. Winamp displays the time in minutes (mm) while the iPhone does it hour/minutes (hh:mm). Here’s a quick little script I whipped together because I’m too lazy to do this in my head, especially for audio books where an audio book can be over 500 minutes, and I need to convert to iPhone time because I want to continue listening where I had just left off on Winamp.

use POSIX qw(ceil floor); # used for the floor function

sub GetToken {
  # @_ = flatten args list from an array
  # @_[0] = first argument
  $data      = @_[0];
  $delimiter = @_[1];
  $token     = @_[2] - 1;
  @tokens_array = split($delimiter, $data);   
  return @tokens_array[$token]; 

sub chr_conver_min {  
  if (length(@_[0]) == 1) {
    return "0".@_[0];
  else {
    return @_[0];

sub iphone_time_convert {

  # converts winamp time to iphone - winamp stores time only in minutes.  
  # @_[0]   =  winamp_time, e.g. 124:34
  # $hour   = floor($winamp_time/60);
  # $minute = $winamp_time % 60;

  $winamp_hour_min = GetToken(@_[0], ":", 1);  
  $winamp_seconds  = GetToken(@_[0], ":", 2);  
  return floor($winamp_hour_min/60).":".chr_conver_min( ($winamp_hour_min % 60) ).":".$winamp_seconds;

sub winamp_time_convert {  

  # converts iphone time to winamp  
  # @_[0] = iphone_time, e.g. 3:43:34    
  $iphone_hour     = GetToken(@_[0], ":", 1);  
  $iphone_min      = GetToken(@_[0], ":", 2);    
  $iphone_seconds  = GetToken(@_[0], ":", 3);
  return (($iphone_hour * 60) + $iphone_min).":".$iphone_seconds;

sub show_help {
  print "\nDisplays the conversion of time between winamp and iPhone.\n\n";
  print "   winamptime [-w2i|-i2p] [mm:ss][hh:mm:ss]\n\n";
  print "Example to convert winamp time to iPhone: \n\n";
  print "   winamptime -w2i 212:41\n\n";
  print "Example to convert iPhone time to winamp, seconds being optional: \n\n";
  print "   winamptime -i2w 2:31:41\n";
  print "   winamptime -i2w 2:31\n\n";


# Optimize this:
if( $ARGV[0] eq "-w2i" ) 
  # winamp to iphone time
  if ( length($ARGV[1]) > 0 ) {
    print "iPhone Time: ".iphone_time_convert( $ARGV[1] )."\n";
elsif( $ARGV[0] eq "-i2w" ) 
  # iphone to winamp time
  if ( length($ARGV[1]) > 0 ) {
    print "Winamp Time: ".winamp_time_convert( $ARGV[1] )."\n";


I’ve Switched to Python from Perl

So I’ve finally dumped Perl for my systems scripts. Partly was for maintainability. Overall, when doing some benchmarks myself, it seems that Perl beats Python in simple text parsing and file manipulation, which is most of the time is what I use it for. Ugh. I do find it though, that in most teams, Perl can be cryptic and unnecessarily harder for one to jump into. Python solves this. I think Python (after playing around with it for about a week) is a much more elegant language. Python will be a great addition to my toolkit for system automation. Much easier to apply OOP principles and write readable code. It’s a pleasure to write in this language and I look forward to learning more about it.

Also, while searching for performance tests on which language was “faster,” I ran across this site: The Great Win32 Computer Language Shootout . Of course, not to be used as a definitive guide, it does serve as a baseline, I think, for very simplistic tasks in a language.

On a related note, here’s a great video I saw on “Python in the Enterprise – How to Get Permission”:

If you start your own company or run your own project you can usually choose the programming language, but if you work for a large company there are probably architects and others who keep a tight rein on approved technology. How do you steer a big ship towards dynamic programming languages, and how fast can it turn? Come hear the story of one software developer employee who in 20 months facilitated the adoption of Python as the standard scripting language for an enterprise with 25,000 employees. Leave with ideas for advancing dynamic programming languages in your workplace, and with hope that change is possible.

I looked into Ruby, and found various similarites. Python sold me due to its larger community and greater applications in the wild. I took a look at PHP for system scripting and it wasn’t fast enough for parsing large files. Lastly, I thought about JavaScript on the console via JSDB but then realized its breadth of native library functions wasn’t as wide as that of Python. I really love that Python is getting a lot of momentum from Google and Microsoft is doing more to support the IronPython (Python on .NET) platform.

Remove Comments from IIS Logs

If you think that Log Parser is a bit on the slow side (i.e. if you’re dealing with big IIS logs) and you want to bulk import your logs into SQL Server, then you’ll have to remove # comments from the log files. Microsoft has the PrepWebLog Utility to do this, but it seems to choke for files that are > 100 MB. Also, you’ll have to write this as a batch file so it goes through a whole directory of files.

I wrote a Perl script that’s relatively fast (faster than PrepWebLog) and it can crawl folders/subfolders recursively. Here it is:

# example: 
#   parse c:\temp\logs\logs*\*.log
# Requirement: no spaces in the directory names and file names.
# This gets called via run.bat. 

sub getFileList 
    # This function returns an array of file list based on filter
    # This is the filter they can put in.       
    # Returns a file with full path. 
    # Example of filters: getFileList ( "*.log" );
    @files = ;
    return @files;    

sub remove_comments
  # Remove # pound sign comments from files. 
  # @_[0] = filename
  open (my $in, "", "@_[0].txt") 
      or die "out: @_[0]";

  while( my $line = )
      print $out $line
          unless $line =~ /^#/;

  close $in;
  close $out;

########## MAIN #############
$arg = @ARGV[0];

# Location of root directory of logs files
#$arg = 'c:\temp\logs\logs*\*.log';

# Replace slashes
$arg =~ s/\\/\\\\/g;

# Loop through all the log files. 
for $file (getFileList ($arg))
  print ( "Processing file $file ... \n" );    
  remove_comments( $file );  

The Perl script gets called via run.bat:

REM No spaces in directory and file names.
perl D:\statesites\W3SVC*\*.log