Perl:全局符号需要显式包名称

时间:2015-09-03 03:04:09

标签: perl package global symbols explicit

所以我一直试图找到解决方案,但到目前为止,我在线阅读的所有内容都与范围问题有关,而不是用 my 声明变量em> 关键字。但是,我似乎无法解决这些问题,因为我已经将所有内容都声明在顶层,至少对我来说,似乎我没有范围问题。我对以下代码的错误是:

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.

代码:

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 个答案:

答案 0 :(得分:3)

您的代码需要声明

my $filename;

它目前不包含该声明。它包含以下无效语句:

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

Perl甚至告诉过你。

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

先修复第一个错误。通过添加缺少的分号来实现。