Perl Can't use string as a symbol ref while strict refs

后端 未结 3 611
执念已碎
执念已碎 2021-02-19 00:50

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\'

相关标签:
3条回答
  • 2021-02-19 01:09

    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 讨论(0)
  • 2021-02-19 01:14

    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 }.

    0 讨论(0)
  • 2021-02-19 01:15

    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
                                ...
    
    0 讨论(0)
提交回复
热议问题