Perl Program Template

From GM-RKB
Jump to navigation Jump to search

A Perl Program Template is a software program template that is a Perl program.



References

#!/usr/bin/perl
########################################
my $PROGRAM = 'templateProgram.pl' ;
my $AUTHOR  = 'Gabor Melli' ;
my $VERSION = '1.0.0' ;
my $VERSIONDATE = "2015.02.13" ;
########################################

# system settings and libraries
use strict ;
use warnings ;
use utf8 ;
use Getopt::Long ;
use lib "." ;

# Subroutine templates
sub trim($) ;

# Globals
my $debug       = 0 ;
my $printHeader = 0 ;
my $logFH ;

# set from input params if at all
my $inFile  ;
my $outFile ;
my $logFile ;

# PROCESS INPUT PARAMETERS
my $help ;
my $verbose = 0 ;
die "ERROR in parameter processing.\n" if not GetOptions (
   'f|inFile:s' => \$inFile,
   'outFile:s'	=> \$outFile,
   'logFile:s'	=> \$logFile,
   'debug:i'    => \$debug,
   'verbose+'   => \$verbose,
   'h|help'     => \$help,
#  ...

) ;

# compose the USAGE reprot
my $USAGE ;
$USAGE .= "USAGE:\n" ;
$USAGE .= "   PARAMS: [--inFile=] [--outFile=] [--logFile=] [--debug=] [--verbose] [--help]\n" ;
$USAGE .= "   Example: ./$PROGRAM -inFile=master.txt -outFile=tok.dat -dictFile=syn.dat -d=1\n" ;
$USAGE .= "   Program info: Version: $VERSION($VERSIONDATE)   Contact: $AUTHOR\n" ;

# begin the debugging
$debug=$debug+$verbose ;

# if the user asked for --help
if (defined($help)) {
   print $USAGE ;
exit ;
}

########################################
# create the handle to the log file
my $isSTDOUTlog = 0 ;
# Test the --logFile parameter
if (defined $logFile){
	die("\nERROR: --logFile must be a file not a directory. [$logFile]\n") if -d $logFile ;
	open $logFH, ">", $logFile or die "ERROR: when opening the log file [$logFile]:\n   $!" ;
} else {
  $logFH = *STDOUT ;
  $isSTDOUTlog++ ;
  $logFile="-" ;
}

if ($debug>0) {
   print $logFH "DEBUG: Debugging enabled and set to level: $debug.\n" ;
}

#######################################
# create the handle to the input file
my $isSTDIN = 0 ;
my $inputFH ;
# Test the --inFile parameter
if (defined $inFile){
	die("\nERROR: --inFile must be a file not a directory. [$inFile]\n") if -d $inFile ;
	open $inputFH, "<", $inFile or die "ERROR: when opening the input file [$inFile]:\n   $!" ;
} else {
  $inputFH = *STDIN ;
  $isSTDIN++ ;
  $inFile="-" ;
}

########################################
# create the handle to the output file
my $isSTDOUT = 0 ;
my $outputFH ;
# Test the --outFile parameter
if (defined $outFile){
	die("\nERROR: --outFile must be a file not a directory. [$outFile]\n") if -d $outFile ;
	open $outputFH, ">", $outFile or die "ERROR: when opening the output file [$outFile]:\n   $!" ;
} else {
  $outputFH = *STDOUT ;
	$isSTDOUT++ ;
  $outFile="-" ;
}

#############################################################
# For each text string perform the requested transformations
my $lineNum=0 ;
while (<$inputFH>) {
  $lineNum++ ;

  my $line = trim($_) ;
  print $logFH "DEBUG: raw[$line] (minus possible newline)\n" if $debug>=2 ;
  next if length($_) <= 0 ;
}

sub trim($) {
   my $string = shift;
   $string =~ s/^\s+|\s+$//g ;
   return $string;
}

#################################################################################
##################################### THE END ###################################
#################################################################################