Perl 不能在严格引用时使用字符串作为符号引用 [英] Perl Can't use string as a symbol ref while strict refs
问题描述
我需要一些有关以下 Perl 代码的指导.当我离开 no strict 'refs'
时,sub
工作正常.但是,如果删除了 no strict 'refs'
,则会收到错误消息:
I need some guidance with the following Perl code. When I leave the no strict 'refs'
, the sub
works fine. But if the no strict 'refs'
is removed, then I get the error message:
Can't use string ("A") as a symbol ref while "strict refs" in use at test.pl
它死在下面标记为这里"的行中.这个 sub
需要打开(写入 >)来自 A..Z 的所有文件,并且基于来自 READ 文件(LOG)的 regex
输出将被写入相应的文件输出.
It dies in the lines marked as "Here" below. This sub
needs to open (write >) all files from A..Z and based on the regex
from the READ file (LOG) the output will be written to the correspond file output.
use strict;
use Text::CSV_XS;
no strict 'refs';
...
...
sub file_split {
my ( $i, $fh, @FH );
my ( $file ) = @_;
my ( @alpha) = ("A".."Z");
for ( @alpha) {
$fh = $_ ;
open ( $fh,">","$_-$file" ) || die $!; <--------- HERE
push @FH, $fh;
}
my $csv = Text::CSV_XS->new( { binary => 1,
allow_whitespace => 1,
allow_loose_escapes => 1,
allow_loose_quotes =>1,
escape_char => undef ,
sep_char => ',',
auto_diag=> 1
} );
open( LOG,"<", $file ) || die $!;
while ( my $row = $csv->getline( *LOG ) ) {
if ( $row->[0] =~ /^(\w)/ ) {
print $1 <--------- HERE
"$row->[0]".",".
"$row->[1]" .",".
"$row->[2]" .",".
"$row->[3]" .",".
"$row->[4]".",".
"$row->[5]"."\n";
} else {
print "Record skipped... --> $row->[0] <-- ... please verify \n";
}
}
}
推荐答案
不要分配 $fh = $_
它没有做任何有用的事情.
Don't assign $fh = $_
it's not doing anything useful.
@FH
应该是 %FH
而不是 push
试试:
@FH
should be %FH
and instead of the push
try:
$FH{ $_ } = $fh
将 $1
替换为 $FH{ $1 }
.
这篇关于Perl 不能在严格引用时使用字符串作为符号引用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!