Perl: Global Symbol Requires Explicit Package Name

点点圈 提交于 2019-12-24 11:37:20

问题


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.



来源:https://stackoverflow.com/questions/32366285/perl-global-symbol-requires-explicit-package-name

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