Global symbol “$i” requires explicit package name

南笙酒味 提交于 2019-12-23 07:05:27

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!