【问题标题】:Perl: Global Symbol Requires Explicit Package NamePerl:全局符号需要显式包名
【发布时间】:2015-09-03 03:04:08
【问题描述】:

所以我一直在尝试找到解决方案,但到目前为止,我在网上阅读的所有内容都与范围问题有关,而不是使用 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";

【问题讨论】:

  • 你错过了;在我的@ids 之后。应该是我的@ids;

标签: perl package global symbols explicit


【解决方案1】:

你的代码需要声明

my $filename;

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

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

Perl 甚至告诉过你。

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

首先修复第一个错误。通过添加缺少的分号来做到这一点。

【讨论】:

  • 谢谢你,这么简单的修复。这就是我工作到很晚的原因。你说得对,我只是注意到它给了我这个错误。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-12-31
相关资源
最近更新 更多