Troubleshooters.Com and Code Corner Present

Steve Litt's Perls of Wisdom:
A Few Other Handy Snippets

Copyright (C) 1998 by Steve Litt


Converting a string to upper case

 $string =~ tr/[a-z]/[A-Z]/;

Reporting the Time as a String

This subroutine reports the time as YYYY-MM-DD @ HH:MM:SS format.

sub timestamp
  ($sec,$min,$hour,$mday,$mon,$year,$wday,$yday,$isdst) =
  $string = sprintf("%04d-%02d-%02d @ %02d:%02d:%02d",
               1900 + $year, $mon+1, $mday, $hour, $min, $sec);

Appending to a Log File

open(LOGFILE, ">>batch.log");
#**Note a dot between strings concatenates them. \t is tab, \n is newline
print LOGFILE &timestamp() . "\tSecond process started\n";

Using Command Line Arguments

#******* MAIN ROUTINE *********
print "First  arg (not program name as in C): $ARGV[0]\n;
print "Second arg: $ARGV[1]\n;
print "Third  arg: $ARGV[2]\n;
print "\n";

Converting a File to a List

Note that because of memory limitations, this should be done only on small to medium sized files. For large files or files whose length is unknown at design time, loop through the file instead.

open(PROFILE, ".profile");
@profile = <PROFILE>;

Looping Through a File

This opens autoexec.bat for input, autoexec.rem for output, and copies only the lines that aren't remmed out.

open(INPUT,  "autoexec.bat");
open(OUTPUT, ">autoexec.rem");   # The > means open for write, 
                                 #  over original if existed.
while (<INPUT>)
  my($line) = $_;  #preserve $1, the line just read, nomatter what
  chomp $line;     #blow off trailing newline

  unless($line =~ /^\s*rem/i)    #click word or symbol for explanation
    print OUTPUT "$line\n";      #write the non-remmed autoexec command

# often easier to use unless() than if(!())
# =~ means "compare to regular expression on right"
# the forward slashes delimit the regular expression
# a pipe (|) pair can be used instead, but then must be preceeded with
# m before the first pipe. You can also put the m before the first slash
# on a slash pair.

# ^ means "start of line
# \s means "whitespace"
# * means 0 or more of the preceeding (in this case 0 or more whitespace)
# rem or REM is what you're actually looking for
# The i after the last slash means case independent compare.

 [ | Code Corner | Email Steve Litt ]

Copyright (C)1998 by Steve Litt -- Legal