我的初始值有问题。我有一个脚本,其方法有:
sub compare_with_terminal {
use Term::Size::Any qw( chars pixels );
my $columns = 0;
my $rows = 0;
($columns, $rows) = chars *STDOUT{IO};
print "Columns: ".$columns."\n"; #line 73
print "Rows: ". $rows."\n";
if($height > $columns || $width > $rows){
print "Bigger than terminal: ";
my $option = <>;
chomp($option);
if($option eq "N"){
print "End.";
exit 0;
}
}
}
如您所见,我在那里创建并初始化了两个值$columns
和$rows
。但是,当我运行这个脚本直到Unix我得到:
Use of uninitialized value $columns in concatenation (.) or string at
./perl/script.pl line 73 (#1)
(W uninitialized) An undefined value was used as if it were already
defined. It was interpreted as a "" or a 0, but maybe it was a mistake.
To suppress this warning assign a defined value to your variables.
$rows
有同样的错误。
这些错误说我没有初始化变量$columns
,但我做到了。任何人都可以解释我并尝试解决这个问题吗?
答案 0 :(得分:4)
There is a bug report for Term::Size::Any
在没有任何参数的情况下使用char
会在linux上返回值(Term::Size::Any
版本0.002):
use warnings;
use strict;
use Term::Size::Any qw(chars);
my ($columns, $rows) = chars();
print "$columns $rows\n";
__END__
80 24
您已将$ column初始化为0,但在调用chars
函数时会将其覆盖。