Error - Global symbol requires explicit package name in Perl

I am having the below parameters called in from a command line and using the param $timeparm in my script.  

my ($db, $user, $pw, $wkdir, $firstday, $lastday, $timeparm)  = @ARGV;

Open in new window


The script has a condition check based on  value of $timeparm and the outfile generated has a concatenation and writing the file to a  ooutput directory "outdir".

However, the code snippet below gives an error on Line 9 states as
"global symbol $getfilename requires explicit package name at......"

I can get past the error by using a declare of the  my$ getfilename prior to the if statement, but that will cause the filecontatenatio to be incorrect.

How do I resolve the error and get the expected answer for getfilename?


if (lc $timeparm eq 'yearly') {
		  my $filedate = substr($firstday,0,4);	#example of the value here is YYYY or 2018 	
		  my $getfilename = $timeparm.$file.$filedate.'.csv';	#getfilename expected is = 'yearly_sales_2010.csv"   
		 } else {
			my $filedate = join '',substr($firstday,4,2), substr($firstday,0,4); #example of value here is MMYYYY or 012018
			my $getfilename = $timeparm.$file.$filedate.'.csv';	#getfilename expected is "monthly_sales_012010.csv"
		}

 my $outdir = "$wkdir/$getfilename";  # the error occurs on this line..

Open in new window

steve2312Asked:
Who is Participating?
 
FishMongerCommented:
It doesn't compile.


c:\test>perl -c steve2312.pl
Possible attempt to separate words with commas at steve2312.pl line 44.
Possible attempt to separate words with commas at steve2312.pl line 46.
Possible attempt to separate words with commas at steve2312.pl line 46.
Possible attempt to separate words with commas at steve2312.pl line 47.
Possible attempt to separate words with commas at steve2312.pl line 47.
Possible attempt to separate words with commas at steve2312.pl line 48.
Possible attempt to separate words with commas at steve2312.pl line 48.
Possible attempt to separate words with commas at steve2312.pl line 49.
Possible attempt to separate words with commas at steve2312.pl line 49.
Global symbol "$filedate" requires explicit package name at steve2312.pl line 83.
steve2312.pl had compilation errors.

Lines 44 - 49 still have the exact same error that I pointed out in your prior question.  You need to either remove the qw or the commas from those lines.  If you remove the qw's then you'll need to quote each of the elements.

Global symbol "$filedate" requires explicit package name at steve2312.pl line 83.
Declare that var prior to the if/else block and remove the my declarations from inside that block; just do the assignments.
0
 
FishMongerCommented:
What changes have you made to your code based on your last question?

Please post a short but complete script that demonstrates the problem.
0
 
steve2312Author Commented:
Here's the shortened version but complete script.  

As for change: Added one additional parameter $timeparm to command line and using this parameter inside the if condition.


use strict;
use warnings;
use DBI;
use Date::Manip;

$| = 1;

my ($db, $user, $pw, $wkdir, $firstday, $lastday, $timeparm)  = @ARGV;

#Check to see if required parameters are received by appworx
unless($db && $user && $pw && $wkdir && $firstday && $lastday && $timeparm)  
{
  die("\n\n  - ERROR:  The Required number of arguments were not received.  Verify that the AppWorx prompts are correct and reset the module..\n\n"); 
}

# Connectivity check to the database else display failed connectivity
my $dbh = DBI->connect("dbi:Oracle:$db", $user, $pw, { AutoCommit => 0, RaiseError => 1 }) or die("Cannot connect to Database '$db' " . DBI->errstr());

# Capture counts for distinct partitions for given firstday and lastday dates
my $partition_key = join '', $firstday ,'_',$lastday;
print("Partition Key : $partition_key \n\n");

my $sthcounts = $dbh->prepare(qq(select count(distinct r.execution_id) from region  r where r.partition_key = ? AND r.status = 'N' ));

# Values of partition key will be passed on by appworx will be parameterized in the script
$sthcounts -> execute($partition_key);


# Get value of rowcount returned from Count of partition keys from region table
my $rowcount = ($sthcounts-> fetchrow_array());
$sthcounts -> finish();

# Check if single partition exists for a given firstday/lastday parameters, ROW COUNT RETURNED will be 1 and then proceed with extract out columns from the table to a csv file
# IF multiple partitions exist OR no data exists for a given firstday/last day set - the PROCESS WILL ABORT

die "ERROR: NO DATA EXISTS FOR GIVEN FIRSTDAY LASTDAY SET OF DATES - ABORTING!!!\n\n"
    unless $rowcount;

die "ERROR: UNREPORTED DATA EXISTS FOR THE GIVEN FIRSTDAY LASTDAY SET OF DATES - ABORTING!!!\n\n"
    if ($rowcount > 1);

# Datasets for which files will  be processed.  The script will output a total of 5 files.

	do_dataset($timeparm, '_Detail_ESIID_', [qw(ESIID,REPCODE, REPNAME, TO_CHAR(STARTTIME,'MM/DD/YYYY') tradedate, ORIGIN, TOTALS)], status => "'N'");

	do_dataset($timeparm,'_Daily_REP_', [qw(REPNAME,  REPCODE, TO_CHAR(STARTTIME,'MM/DD/YYYY') tradedate, SUM(TOTALS), SUM(SUBTRACT_TOTAL), SUM(ADJ_TOTAL))], status => "'N'", group_by => [qw(GROUP BY REPNAME, REPCODE, TO_CHAR(STARTTIME,'MM/DD/YYYY'))]);
	do_dataset($timeparm,'_RecLoad_', [qw(TO_CHAR(STARTTIME,'MM/DD/YYYY')  tradedate, REPNAME, DUNSNUMBER,  SUM(ADJ_TOTAL) AS TOTALS)], status => "'N'", group_by => [qw(GROUP BY REPNAME, DUNSNUMBER, TO_CHAR(STARTTIME,'MM/DD/YYYY'))]);
	do_dataset($timeparm,'_ESIID_', [qw(ESIID, REPCODE, REPNAME, SUM(TOTALS) TOTALS)], status => "'N'", group_by => [qw(GROUP BY ESIID, REPCODE, REPNAME)]);
	do_dataset($timeparm,'_REP_', [qw(REPNAME, REPCODE, TO_CHAR(STARTTIME,'MM/DD/YYYY')  tradedate, SUM(SUBTRACT_TOTAL), SUM(ADJ_TOTAL))], status => "'N'", group_by => [qw(GROUP BY REPNAME, REPCODE, TO_CHAR(STARTTIME,'MM/DD/YYYY'))]);


# Subroutine does processing of datasets
sub do_dataset {
    my ($timeparm, $file, $cols, %opt) = @_;    
    my $col = join '', @$cols;
    my $group = ' ';
    
    # process the below if the above dataset should have a GROUP BY clause    
    if (exists $opt{group_by}){
        $group = join ' ', @{$opt{group_by}};
        delete $opt{group_by};
    	}
    
    # The below statement is placeholder to hold extra clauses or conditions, for instance dataset1 would have print a value of STATUS = 'N'      
    my $extra = %opt ? ('and ' . join(' and ', map { "$_ = $opt{$_}" } keys %opt)) : '';
    
    # Capture dates to append to filenames created
    
    my $firstmonth = substr($firstday,4,2);   
    my $lastmonth = substr($lastday,4,2);
    my $firstyear = substr($firstday,0,4);   
    my $lastyear = substr($lastday,0,4);
  
  
  	if (lc $timeparm eq 'yearly') {
		  my $filedate = substr($firstday,0,4);		
		  my $getfilename = $timeparm.$file.$filedate.'.csv';	   
		 } else {
			my $filedate = join '',substr($firstday,4,2), substr($firstday,0,4);
			my $getfilename = $timeparm.$file.$filedate.'.csv';	 
		}
		  
	   my $getfilename = $timeparm.$file.$filedate.'.csv';	 
 
      my $allrows = $dbh->selectall_arrayref("SELECT $col FROM REGION WHERE PARTITION_KEY = '$partition_key' $extra $group")
             or die("Could not get data from table REGION ".DBI->errstr);    
      
      my $dumpdir = "$wkdir/$getfilename";
      
      open OUT, '>', $dumpdir or die " - Could not open $dumpdir for export. $!";
      print OUT $col, "\n"; 									
      print OUT map {join(',', map { defined($_) ? $_ : '' } @$_), "\n" } @$allrows;
      close OUT;
      
 }

Open in new window

0
Get expert help—faster!

Need expert help—fast? Use the Help Bell for personalized assistance getting answers to your important questions.

 
steve2312Author Commented:
"Possible attempt to separate....." is only a warning message and code will compile once the "Global symbol "$filedate" requires explicit package name" is resolved.  

Will try out your suggestion and let you know.
0
 
FishMongerCommented:
I hope you meant that you will be fixing both of the issues in my suggestions.

Deliberately leaving in coding errors/mistakes even when they are non fatal should NOT be done.  Your code should compile without any errors OR warnings.
0
 
steve2312Author Commented:
Yes, working on both

On your removal of qw - did you mean modify the statement as  below ??

do_dataset($timeparm,'_REP_', [qw(ESIID, REPCODE, REPNAME, SUM(TOTALS) AS TOTALS"  - original statement

do_dataset($timeparm,'_REP_', 'ESIID', 'REPCODE','REPNAME', 'SUM(TOTALS) AS TOTALS' - modified statement
0
 
FishMongerCommented:
Yes, however, your first statement is passing an arrary ref and the second one is not.  So if you make that change in addition to adjusting the quoting, then you'll need to adjust how the sub accepts the args.
0
 
steve2312Author Commented:
Thanks, resolved the error issue.  Working on the warnings generated...
0
 
steve2312Author Commented:
I was trying to add a finish statement to the $allrows after line 92.  But it fails with can't call method finish with unblessed reference to the below statement.

$allrows -> finish();
0
 
FishMongerCommented:
Why are you wanting to do that?

The finish() method is preformed on a statement handle, not on the result of a select statement.  Also, there's no need to do a finish() statement because your select statement is part of a $dbh->selectall_arrayref() (database handle) statement and that method will (probably) call finish() if it's needed.
0
 
steve2312Author Commented:
Makes sense.  Meanwhile I am testing the removal warnings (keeping the qw but removal the , between columns). It works for the first couple of datasets.

Can get rid of the warnings for the first dataset.  

Need some work on datasets with the group by.  These require the comma to separate out the columns listed to make the script work, but then the warnings surface once the comma is added.
0
 
steve2312Author Commented:
I was able to resolve the warnings. Thanks once again!
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.