开发者

Perl Can't use string as a symbol ref while 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

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
                                   开发者_JAVA技巧 } );
    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";
                }
    }
}


You're asking perl to use the value of $fh as the name of the filehandle here:

for ( @alpha) {                            
            $fh = $_ ;
            open ( $fh,">","$_-$file" )  || die $!;      <--------- HERE
            push @FH, $fh;
    }

You should instead consider using a lexical variable and having that autovivified into a filehandle by open, then store this in a hash to get at it later:

for ( @alpha) {                            
            open ( my $fh,">","$_-$file" )  || die $!;
            $handles{$_} =  $fh;
}

so that you can use it later here:

while ( my $row = $csv->getline( *LOG ) ) {
            if ( $row->[0] =~ /^(\w)/ ) {
                    print $handles{$1}                       <--------- HERE
                            ...


Don't assign $fh = $_ it's not doing anything useful.

@FH should be %FH and instead of the push try:

$FH{ $_ } = $fh

Replace the $1 with $FH{ $1 }.


You're trying to use a string literal as a filehandle: in your foreach loop, $_ takes on the values "A","B",etc. So, don't do that. Just make a new filehandle and push that onto @FH.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜