问题
I have 3 arrays and then I open a txt file which contains firstname , lastname and email(alone with some html codes like shown below)
<first>Mike<first>
<sec>stone<sec>
<email>mike@rt.com<email>
I first count the lines in the file, then I parse this file and put first , last and email in 3 different arrays and then I remove the htmls tags from each array element. But I'm facing problems like Global symbol required explicit package name in one of the variable during removing the html tags even if I have declared it with my Here is the snippet
if ( $ct <= $end ) {
my $i = 0;
}
$ct++;
}
my $j = 0;
my $l = @lines;
while ( $j < $l ) {
if ( $lines[$j] =~ m/@/ ) {
50 $line[$i] = $lines[$j];
51 $j = $j - 3;
52 $last[$i] = $lines[$j];
53 $j++;
54 $first[$i] = $lines[$j];
55 $i++;
56 $j = $j + 7;
57;
}
else 58 {
59 $j++;
60
}
Im getting errors like
Global symbol "$i" requires explicit package name at pqwe.pl line 50.
Global symbol "$i" requires explicit package name at pqwe.pl line 53.
Global symbol "$i" requires explicit package name at pqwe.pl line 55.
Global symbol "$i" requires explicit package name at pqwe.pl line 56.
Execution of pqwe.pl aborted due to compilation errors.
回答1:
You need to move the $i declaration out of the if block. Check below code:
#!/usr/bin/perl
use strict;
use warnings;
my $var = 1;
if ( $var eq 1)
{
my $i = 100;
}
print $i."\n";
output:
# perl test.pl
Global symbol "$i" requires explicit package name at test.pl line 12.
Execution of test.pl aborted due to compilation errors.
Now, I moved the $i declaration out of if block:
#!/usr/bin/perl
use strict;
use warnings;
my $i;
my $var = 1;
if ( $var eq 1)
{
$i = 100;
}
print $i."\n";
OUTPUT:
# perl test.pl
100
Read More about it in the perl documentation: http://perldoc.perl.org/perlintro.html#Variable-scoping and http://perlmaven.com/scope-of-variables-in-perl
来源:https://stackoverflow.com/questions/23854436/global-symbol-i-requires-explicit-package-name