Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

what's wrong with this script? 1

Status
Not open for further replies.

stones1030

Programmer
May 30, 2006
15
US
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);
}
 
if common.pl is 'required' by another file, be sure it has a

Code:
1;
at the end

see
The file must return true as the last statement to indicate successful execution of any initialization code, so it's customary to end such a file with 1; unless you're sure it'll return true otherwise. But it's better just to put the 1;, in case you add more statements.

Jeb\0
 
To be precise
I created another test file to use my common.pl

Here is the test file........................
.................
use strict;

require 'common.pl';

$Commons::prepLogFile();
$Commons::logMsg();
.......................
The error I get is

syntax error at test.pl line 5, near "$Commons::prepLogFile("
syntax error at test.pl line 6, near "$Commons::logMsg("
Execution of test.pl aborted due to compilation errors.
 
1)
Code:
require 'common.pl';

Commons::prepLogFile();
Commons::logMsg();
2) common.pl, line 37 missing semicolon at end of
Code:
print LOG "Processing $ENV{'RefType'}..."
3) common.pl missing true return value
4) common.pl uses strict, but variables in prepLogFile are not declared

hope this helps

Jeb
\0
 
Thanks. you made my day. I was able to fix all the errors and now the script is working as I wanted it to be.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top