Perl: Global Symbol Requires Explicit Package Name

2019-08-13 15:51发布

问题:

So I've been attempting to find the solution to this but so far everything I've read online has to do with scope issues and not declaring the variables with the my keyword. However, I can't seem to fix the issues because I've declared everything at the top and, to me at least, it seems I don't have scope issues. My errors for the following code are:

Global symbol "$filename" requires explicit package name at read_ids.pl line 6.
Global symbol "$filename" requires explicit package name at read_ids.pl line 8.
Global symbol "$filename" requires explicit package name at read_ids.pl line 9.
Global symbol "$filename" requires explicit package name at read_ids.pl line 22.

The code:

use strict;
use warnings;

#Create array of IDs.
my @ids
my $filename = 'ids.csv';

open(my $fh, '<:encoding(UTF-8)', $filename)
    or die "Could not open file '$filename'.";

#Read line using the readline operators <>.
while (my $row = <$fh>) {
#Remove any newline characters from line using the chomp() command.
    chomp $row;
    push @ids,'$row';
#  print "$row\n";
}

foreach (@ids) {
    print "$_\n";
}
print "Read '$filename' successfully.\n";

回答1:

Your code needs the statement

my $filename;

It does not currently contain that statement. It contains the following invalid statement instead:

my @ids my $filename = 'ids.csv';

Perl even told you about it.

syntax error at a.pl line 6, near "@ids
my "

Fix the first error first. Do so by adding the missing semi-colon.