Perl Script Snipits

Date and time stuff

This simple IF statement checks to see if the variable is equal
to a number of text strings. If statements are enclosed in brackets.

#!/usr/bin/perl
# Quite a bit of documentation at:
# http://search.cpan.org/dist/Date-Calc/Calc.pod
# Uses

use strict ;
use POSIX qw(strftime);
use Date::Calc qw(Add_Delta_YMD Gmtime Mktime System_Clock);

# Variables
my $vTheDate ;
my $vTheTime ;

#
# POSIX Time Stamps
#

$vTheDate = strftime "%Y %m %d", gmtime ;
print "POSIX Date: $vTheDate \n" ;
$vTheTime = strftime "%H %M %S", gmtime ;
print "POSIX Time $vTheTime \n" ;

#
# Date:Calc Time Stamps
# Requires Date::Calc
#

my $vYear ;
my $vMonth ;
my $vDay ;

my $vHour ;
my $vMin ;
my $vSec ;

my $vGmt ;
($vYear,$vMonth,$vDay, $vHour,$vMin,$vSec) = System_Clock([$vGmt]);
$vTheDate = "$vYear $vMonth $vDay" ;
$vTheTime = "$vHour $vMin $vSec" ;
print "Date::Calc System_Clock time: $vTheDate \n";
print "Date::Calc System_Clock date: $vTheTime \n";

#
# Date:Calc Time calculation with POSIX Time
#

my $vMonthsAgo = 5 ;
my $vPastDate ;

#
# Passes the POSIX time generator the instruction to display the time provided from the Date::Calc module
# Date::Calc module Current time provided from system_clock previously
# Uses Mktime & Add_Delta_YMD to subtract the months (and a day for some reason)
#

$vPastDate = strftime("%Y %m %d",gmtime(Mktime(Add_Delta_YMD($vYear,$vMonth,$vDay, 0, - $vMonthsAgo, 1),0,0,0))); print "Five months ago was: $vPastDate" ;
}

File Open, Write and Append

Example script to open a file & write text to it, then re-open and append, then open and display contents

#!/usr/bin/perl

use strict ;
my $vFile = "c:\\1.temp" ;
my $vTextToFile = "If I wanted to be written to a file I would be text... and I am \n" ;
my $vMoreTextToFile = "I and to join the fun \n" ;
unlink $vFile ;
# Just remove the file first

open(DAT, ">$vFile" ) ;
# Create the file - or wipe and write
  "print DAT "$vTextToFile" ;
# Write to it
close(DAT) ;
# Close it

open(DAT, ">>$vFile" ) ;
# Open the file for append
  "print DAT "$vMoreTextToFile" ;
# Write to it
close(DAT) ;
# Close it

open(DAT, "<$vFile" ) ;
# Open the file and read it
  "my @aFileRaw = <DAT> ;
# Populate array with the data
close(DAT) ; # Close the file

# Now cycle through array and print
for (@aFileRaw)
{
  "print "$_" ;
}

Simple IF block

This simple IF statement checks to see if the variable is equal
to a number of text strings. If statements are enclosed in brackets.

${vblFruit} = "apple"

if ( ${vblFruit} -eq "pear" )
{
  "The fruit is a pear"
}
elseif ( ${vblFruit} -eq "orange" )
{
  "The fruit is an orange"
}
else
{
  "The fruit is something else"
}

String Matching

#!/usr/bin/perl
use strict ;

# Some text to play with
my $vTextSentance = "Blah Blah Blah... tractor statistics up..." ;

if ($vTextSentance =~ "tractor") # This will match
{
  "print "TRUE - matched tractor in sentence \n" ;
}


if ($vTextSentance =~ "racto") # This will match
{
  "print "TRUE - matched racto in sentence \n" ;
}


if ($vTextSentance !~ "5thDimensionImp") # This will match
{
  "print "TRUE - no 5th Dimension Imp's in the sentence \n" ;
}


#
# Regular expression matching
#
if ($vTextSentance =~ /tractor/)
{
  "print "TRUE - matched tractor in sentence \n" ;
}


if ($vTextSentance =~ /.*/)
{
  "print "TRUE - matched some text in sentence \n" ;
}


#
# Using the default variable method
#

$_ = $vTextSentance ; # Set the default variable to the sentence

if (/tractor/) # This will match
{
  "print "TRUE - matched tractor in default variable \n" ;
}