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

Perl: Global Symbol Requires Explicit Package Name

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

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";

您的代码需要语句

my $filename;

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

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

Perl 甚至告诉过你。

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

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