A perl template environment

Scripting is for lazy people. To avoid thinking it over again and again I created some templates for a perl environment. The templates provide logging, a configuration file, a library for function availabillity in different scripts, strict code and some examples. The environment variable PERLHOME is used to have a basedir anyone can adapt to his/her environment. Under PERHOME create a ‘rc’ and a ‘logs’ directory, create a rc-file with the name <scriptname>.rc

The library:

###########################################################################
#
# Name template.pm
#
# Description :
# (template) Perl library for <what your application is called>
###########################################################################

use strict;

sub tempFunctions {
print “Functions in template.pm\n”;
print “============================\n”;
print “getconfig (parameter, configfile) \n”;
print ” returns an array for parameter in config-file.\n”;
print ” Get parameters in your program like: my (parameter) = &getconfig(<parameter>, <rcfile>)\n”;
print ” The config-file should look like ‘<parametername> = <value>[,<value>[, ]]’\n”;
print ” \${HOME}/<programname>.rc is the default config-file\n”;
print “openLog \n”;
print ” Open logfile \$loghome/<program>.log. \n”;
print “printLog \n”;
print ” Print a line in the logfile opened by openLog. \n”;
}

sub getconfig {
my ($configfile,$parameter,$item,$value);
my @end = ();
my $Filename = $0 =~ s/\.pl$//;
($parameter,$configfile) = @_;
if (!defined $configfile ) {
$configfile = “$ENV{‘HOME’}/.$Filename.rc”;
}
open (CONFIG, $configfile) || die “Configfile $configfile can not be opened\n” ;
while ( <CONFIG> ) {
if (( $_ =~ /\S+.*=.*\S+/) && ($_ !~ /^#/ )) {
s/\s//g;
($item,$value) = split (/=+/,$_ ,2);
defined $value && chomp $value;
if ($item =~ /$parameter/) {
@end = split(/,/,$value);
}
}
}
return @end;
}

sub openLog {
# Make sure $0 is stripped to contain only the filename (see template.pl)
my ($loghome) = @_;
open (LOG, “>> $loghome/$0.log”) || die (“Unable to open logfile $loghome/$0.log”);
&printLog(“Opened Logfile.”);
}

sub printLog {
my ($string) = @_;
my $now = scalar localtime();
chomp($now);
print LOG $now.”: “.$string.”\n”;
}

1;

template

The script template:

#!/usr/bin/perl -w
#—————————————————————————–
#
# Name template.pl
#
# Description :
# Template and example code for perl programs
#
# Limitations:
#
# usage:
# template.pl
#
# This script
# – uses the template perl library
# – uses environment variable PERLHOME(points to where lib is located)
#—————————————————————————–
##### Standard code, think twice before modifying. Modify template too. ###

# Strict helps you writing correct code. Other tips are;
# Declare variables in the smallest context possible (in subroutine if you
# don’t use it elsewhere)
# Write well structured code. Routines have only 1 entrance and 1 exit. Keep
# variables local (see previous tip) data comes in when the routine is called
# (in @_) and goes out when it is closed (as return value).
# Write readable code, use indent, and use TABs to indent (set tabstop to 3)
# Use a nice script editor like notepad++ or bluefish.
use strict;
# Getopt is used to fetch options and parameters from the commandline.
use Getopt::Long;

# Define PERLHOME in your userenvironment this is when the configfile and logfiles
# are to be found. Make sure the name is just PERLHOME, no spaces around it
# in windows define it like # set PERLHOME=<full path to your perlhome directory>
my $perlhome = $ENV{“PERLHOME”};
my $library = $perlhome.”/template.pm”;
# strip $0 (get program name without directory prefix)
# This is important for generating the correct logfilename and nice for e.g.
# the ‘die’ messages.
$0 =~ s:.*/::;
my $rcfile=”$perlhome/rc/$0.rc”;
defined $perlhome || die “$0: \$PERLHOME not defined. Exit …”;
require $library || die “$0: library $library can not be opened. Exit …”;
##### End of standard code, think twice before modifying. Modify template too. ###

#
# MAIN
#
my ($var1, $var2);

my @array = ();
my %hash = {};

my ($Directory, $Filename) = $Location =~ m/(.*\/)(.*)$/;

&process_cmd_line;
&openLog($0);
&openfile;

# Get parameters from configfile
#my ($speedRRD) = &getconfig(‘speedRRD’,$rcfile);

MORE CODE;

&printLog(“Ready, $count neighbours processed”);
#############################################################################
#
# FUNCTIONS
#
# process command line arguments
sub process_cmd_line {
my $help;
Getopt::Long::Configure (‘bundling’);
GetOptions (‘input|file|f=s’ => \$inputfile,
‘item|i=s’ => \$item,
‘h’ => \$help);

if ( !defined $help ) {
if ( !defined $inputfile ) {
print “inputfile is mandatory\n”;
$help = 1
}
elsif ( ! -r $inputfile ) {
print “inputfile $inputfile is not readable.\n”;
$help = 1
}
if ( !defined $item ) {
print “Item is mandatory.\n”;
$help = 1
} elsif ( $item !~ /VAL1|VAL2|VAL3/ ) {
print “$item is not a valid item.\n”;
$help = 1
}
}
if ( defined $help) {
print ” This program bla bla\n”;
print ” usage: $0 [-h] –input <inputfile> –item <Item>\n”;
print ” -h: this help\n\n”;
print ” bla bla\n”;

exit;
}
}
sub openfile {
open (INFILE, “< $inputfile”) || die(“Open failed for $inputfile”);
@infile = <INFILE>;
close(INFILE);
}

template

The configuration (rc) file

item1 = value1
item2 = value2
item3 = value31,value32, value33

 

Leave a Comment

This site uses Akismet to reduce spam. Learn how your comment data is processed.