Global symbol requires explicit package [closed]

2019-09-07 05:03发布

What I am doing wrong? Opened file is not empty. But I'm still getting

Global symbol "$tabbb" requires explicit package name at mix.pl line 8.

#!/usr/bin/perl

use strict;
use warnings;

open FILE, "<", "seeds.data" or die $!;
my @tab = <FILE>;
print @$tab;

标签: perl file
2条回答
Melony?
2楼-- · 2019-09-07 05:32

you want print @tab; instead of print @$tab;.

查看更多
三岁会撩人
3楼-- · 2019-09-07 05:33

You have correctly used use strict and use warnings, and one of the benefits is that Perl will warn you if you use a variable you haven't declared. The error message

Global symbol "$tabbb" requires explicit package name at mix.pl line 8.

is saying that, because you are using strict, you cannot refer to a variable called $tabbb that hasn't been declared. Your line

print @$tab;

is dereferencing the scalar variable $tab as an array, and since you haven't declared a $tab I imagine that is what the error message means. However you do have an array variable @tab that contains the contents of the file you opened, so write

print @tab;

instead.

Best of all, read the file line-by-line and write

use strict;
use warnings;

open my $fh, '<', 'seeds.data' or die $!;
while (<$fh>) {
  print;
}
查看更多
登录 后发表回答