stones1030
Programmer
I worked on Perl couple of years ago. I maintained someone else's code and I never had problem modifying. Now I try to develop something on those lines and I can't even do the basics. It istoo late for me to change the technology. Can some one help me with this.
I want to move buildheader, buildFooter to common.pl.
Then use the logging and the above two subs in phase1.pl,phase2.pl,etc.,
Can someone tell me how to do this? I tried using "require". It gave me errors. probably didn't use it in right way.
THANKS A LOT
..........................................
main.pl
..........................................
package RefUtil;
use strict;
#require 'common.pl';
my $inputDir = "Input";
my @validTypes = ('phase1','phase2','phase3','phase4');
my $TRUE = "true";
print "\nEnter input file name:";
my $inFile = <STDIN>; chomp($inFile);
print "Cannot find $inFile. Make sure the file exists and try again.\n"
unless (-e "$inputDir\\$inFile");
print "\nEnter data type:";
my $type = <STDIN>; chomp($type);
print "\nYou entered $type";
my $validType = "false";
my $i;
for $i (@validTypes) {
if ($type eq $i) {
$validType = $TRUE;
print "\n $type.pl";
last;
}
}
print "$type is not a valid type.\nTry again with a valid type." unless ($validType eq $TRUE);
#Set the global variables
$ENV{'InputFile'} = "$inputDir\\$inFile";
$ENV{'RefType'} = $type;
my $script = "$type.pl";
print("Error processing $type.pl:$!") unless (do $script);
..........................................
phase1.pl
............................................
package RefUtil;
use strict;
use warnings;
use vars qw($logFile);
sub buildHeader;#,buildPhase1,buildFooter;
print "\nProcessing Phase 1 codes...";
my $inFile = $ENV{'InputFile'};
print "\nInput File is $inFile";
open(INFILE,"<$inFile") or die"Error trying to open file $inFile:$!";
print "\n fails here";
buildHeader();
1;
#buildPhase1();
#buildFooter();
sub buildHeader {
print "\nBuilding Header Elements...";
return;
}
sub buildPhase1 {
print "\nBuilding Phase 1 Codes...";
}
sub buildFooter {
print "\nBuilding Footer Elements...";
}
..............................................
common.pl
...................................................
###################################################################
package Commons;
####################################################################
use strict;
use Time::localtime;
use vars qw($logFile);
#buildHeader();
#buildBPD();
#buildFooter();
#------------------------------------------------------------------
#
# prepLogFile - subroutine to prepare the log file
#
#------------------------------------------------------------------
sub prepLogFile
{
$t = localtime();
$year = 1900 +$t->year;
$mday = $t->mday;
$mon = 1 + $t->mon;
$hour = $t->hour;
$min = $t->min;
$sec=$t->sec;
my $log=$ENV{'RefType'}."_".$mon."_".$mday."_".$year."_".$hour.$min.$sec.".log";
open (LOG,">",$logFile);
print LOG "Processing $ENV{'RefType'}..."
close LOG;
}
# ----------------------------------------------------------------------------------------
# This subroutine logs the message passed to log file
# Parameters:
# $message = message to be logged
#
sub logMsg
{
my ($message) = @_;
open(LOG, ">>$logFile");
print(LOG "\n",scalar(localtime)," ",$message);
close(LOG);
}
I want to move buildheader, buildFooter to common.pl.
Then use the logging and the above two subs in phase1.pl,phase2.pl,etc.,
Can someone tell me how to do this? I tried using "require". It gave me errors. probably didn't use it in right way.
THANKS A LOT
..........................................
main.pl
..........................................
package RefUtil;
use strict;
#require 'common.pl';
my $inputDir = "Input";
my @validTypes = ('phase1','phase2','phase3','phase4');
my $TRUE = "true";
print "\nEnter input file name:";
my $inFile = <STDIN>; chomp($inFile);
print "Cannot find $inFile. Make sure the file exists and try again.\n"
unless (-e "$inputDir\\$inFile");
print "\nEnter data type:";
my $type = <STDIN>; chomp($type);
print "\nYou entered $type";
my $validType = "false";
my $i;
for $i (@validTypes) {
if ($type eq $i) {
$validType = $TRUE;
print "\n $type.pl";
last;
}
}
print "$type is not a valid type.\nTry again with a valid type." unless ($validType eq $TRUE);
#Set the global variables
$ENV{'InputFile'} = "$inputDir\\$inFile";
$ENV{'RefType'} = $type;
my $script = "$type.pl";
print("Error processing $type.pl:$!") unless (do $script);
..........................................
phase1.pl
............................................
package RefUtil;
use strict;
use warnings;
use vars qw($logFile);
sub buildHeader;#,buildPhase1,buildFooter;
print "\nProcessing Phase 1 codes...";
my $inFile = $ENV{'InputFile'};
print "\nInput File is $inFile";
open(INFILE,"<$inFile") or die"Error trying to open file $inFile:$!";
print "\n fails here";
buildHeader();
1;
#buildPhase1();
#buildFooter();
sub buildHeader {
print "\nBuilding Header Elements...";
return;
}
sub buildPhase1 {
print "\nBuilding Phase 1 Codes...";
}
sub buildFooter {
print "\nBuilding Footer Elements...";
}
..............................................
common.pl
...................................................
###################################################################
package Commons;
####################################################################
use strict;
use Time::localtime;
use vars qw($logFile);
#buildHeader();
#buildBPD();
#buildFooter();
#------------------------------------------------------------------
#
# prepLogFile - subroutine to prepare the log file
#
#------------------------------------------------------------------
sub prepLogFile
{
$t = localtime();
$year = 1900 +$t->year;
$mday = $t->mday;
$mon = 1 + $t->mon;
$hour = $t->hour;
$min = $t->min;
$sec=$t->sec;
my $log=$ENV{'RefType'}."_".$mon."_".$mday."_".$year."_".$hour.$min.$sec.".log";
open (LOG,">",$logFile);
print LOG "Processing $ENV{'RefType'}..."
close LOG;
}
# ----------------------------------------------------------------------------------------
# This subroutine logs the message passed to log file
# Parameters:
# $message = message to be logged
#
sub logMsg
{
my ($message) = @_;
open(LOG, ">>$logFile");
print(LOG "\n",scalar(localtime)," ",$message);
close(LOG);
}