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 IamaSherpa on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

script error 1

Status
Not open for further replies.

lmb5kids

Programmer
Dec 18, 2003
36
US
can someone help me with this error please :

Global symbol "i" requires explicit package name at showcode.pl line 24.
Variable "$i" is not imported at showcode.pl line 24.

I can get my script to run fine with perl version 5.8.0 but when I try and run it on version 5.003 I get that error.

Thanks
 
#!/usr/bin/perl

use Getopt::Std;
use strict;
#use warnings;

my(%h_Option);
{
(getopts('N:p:h',\%h_Option) and (defined $h_Option{N} && defined $h_Option{P})) || PrintUsageAndQuit(1);
PrintUsageAndQuit(0) if $h_Option{'h'}; # Help doesn't need arguments
};

open(DATA, "data.dat") || die "Can't open data.dat\n";

<DATA>;
<DATA>;
my(@a_Lines)=(<DATA>);
chomp(@a_Lines);
my($s_Names,%h_Name)=32;
map {$_=uc $_} @a_Lines[0..$s_Names-1];
@h_Name{(@a_Lines)[0..($s_Names-1)]}=(1..$s_Names);
my($rh_Product);
for (my $i=$s_Names; $i < $#a_Lines; $i=$i+(2+$s_Names)) {
$rh_Product->{uc $a_Lines[$i]}=[(@a_Lines)[$i+1..$i+1+$s_Names]];
};
print "$h_Option{N} $h_Option{P} $rh_Product->{uc $h_Option{P}}[0] $rh_Product->{uc $h_Option{P}}[$h_Name{uc $h_Option{N}}]\n";

# Subs
{

sub PrintUsageAndQuit {
# Emits usage message, then exit with given error code.
print <<END_OF_HELP; exit($_[0] || 0);

Usage:
$0 [switches & values] <FTP address> <directory>

Switches:
-N<Name> Name
-p<ProductName> Product Name
-h display this help message
END_OF_HELP
}; # PrintUsageAndQuit:

};
 
Silly question but have you tried spaces line 24?
From:
Code:
$rh_Product->{uc $a_Lines[$i]}=[(@a_Lines)[$i+1..$i+1+$s_Names]];
to:
Code:
$rh_Product->{uc $a_Lines[$i]}=[(@a_Lines)[$i+1  ..  $i+1+$s_Names]];

Sorry, I don't have old versions of perl to test with.


Trojan.
 
I tried that and it still gives the same error.
 
here's a shot in the dark....try to declare $i with "our" instead of "my"

so...

for (our $i=$s_Names; $i < $#a_Lines; $i=$i+(2+$s_Names)) {
 
Don't think that's gonna work
If memory serves me correctly, "our" wasn't introduced until perl 5.6.0

:-(

Trojan.
 
maybe has nothing to do with the problem, but shouldn't this line:

Code:
$rh_Product[COLOR=#ff0000]->[/color]{uc $a_Lines[$i]}=[(@a_Lines)[$i+1..$i+1+$s_Names]];

be written as:

Code:
$rh_Product{uc $a_Lines[$i]}=[(@a_Lines)[$i+1..$i+1+$s_Names]];
 
Sorry KevinADC, I can't agree.
rh_Porduct is defined as a scalar, not a hash and it's being used as a scalar reference to a hash (which I would think should be fine).

Trojan.

 
Sorry guys,

I think I might have it now.
Remove the "my" from the "for" and stick it outside:

Code:
my $i;
for ($i=$s_Names; $i < $#a_Lines; $i=$i+(2+$s_Names)) {

I seem to remember scoping issues changing around that kinda time.
Let me know if this fixes it.

Trojan.


 
ok , Trojan that fixed the errors , Thanks, but now I have another problem the script doesn't work - all it does is print out the usage now? It works fine on 5.8.0 ..
 
First things first, I suggest quoting the hash keys in line 9.
I doubt that will fix the problem but it's a wise thing to do regardless.
You might like to try breaking the line (9) up into lots of "if" tests and maybe running under the debugger to see where it fails the test.


Trojan.

 
Sorry KevinADC, I can't agree.
rh_Porduct is defined as a scalar, not a hash and it's being used as a scalar reference to a hash (which I would think should be fine).

No need to be sorry. It's more than possible I am confused. Let's look:

Code:
my($rh_Product); [b]<-- has no value[/b]
for (my $i=$s_Names; $i < $#a_Lines; $i=$i+(2+$s_Names)) {
   $rh_Product->{uc $a_Lines[$i]}=[(@a_Lines)[$i+1..$i+1+$s_Names]];
       };

So how can $rh_Product->{uc $a_Lines[$i]} be referencing or assigning anything to it? Like I said, maybe I am confused and would welcome an explnation.

It would also be nice to have some lines from the data file to run the code against....hint-hint-hint ;-)
 
It's being assigned TO. As such, it's created on the fly.
Here's proof it works fine

Code:
use strict;

my $test;

$test->{1} = "blah";
print $test->{1}."\n";

it will print blah
 
OK, I see. It's an annonymous version of:

Code:
my %test = (foo => 'bar');
my $test = \%test;
$test->{foo} = 'blah';


I guess I was unaware that a scalar could be silently converted into a hash (or array) reference using this method:

my $string;
$string->{foo} = 'bar';


but now that I think about it, it's very perl-ish behavior.

 
can you give me an example of breaking line 9 out into if statements please ? I just don't understand how it works under 5.8.0 and not 5.003 ?

Thanks
 
ok, here goes.
Code:
my $allok = 0;
die "getopts failed" unless(getopts('N:P:h',\%h_Option));
if(defined $h_Option{'N'}) {
  print "Got option N\n";
  if(defined $h_Option{P}) {
    print "Got option P\n";
    $allok = 1;
  }
}
PrintUsageAndQuit(1) unless($allok);

Note: I haven't tested that but it should give you the idea.
You can, of course, add "else" sections and even prnit out the hash elements in question to see what they contain.

Trojan.


 
put that code in and it tells me it found options N and P on the 5.80 version but of course on the 5.003 those options arent being set to anything ?

thanks
 
Can you check which version of Getopt::Std you have on each machine?

Code:
perl -e 'use Getopt::Std; print $Getopt::Std::VERSION;'

See if they are the same.

Trojan.

 
ok , the 5.003 did not show any version. Thanks for the tip that helped me. I have it figured out now. Thanks for all your help.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top