我需要一些指导与以下Perl代码.当我离开没有严格的“参考”时,子工程正常.但是如果没有严格的’refs’被删除,那么我会收到错误信息:
Can't use string ("A") as a symbol ref while "strict refs" in use at test.pl
它在下面标记为“Here”的行中死亡.该子需要打开(写入>)所有来自A..Z的文件,并且根据READ文件(LOG)的正则表达式,输出将被写入对应的文件输出.
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 = $_它没有任何有用的东西.
@FH应该是%FH,而不是push try:
$FH{ $_ } = $fh
将$1替换为$FH {$1}.