在子例程参数中处理多个文件时出错

时间:2017-01-27 00:01:11

标签: perl

我正在尝试执行以下脚本,而perl表示只读变量正在被修改。我无法找到在此程序中修改的变量。任何人都可以帮我解决如何在下面的脚本中修复此错误?

 1. use strict;
 2. use warnings;
 3. use diagnostics;
 4. use vars;
 5. use 5.010;
 6. #--------------------------------------------#
 7.
 8. my @array;
 9. if (! open STDERR, ">", "error.txt") {
10.      die "cannot open the Error file for write";}
11. &config_parser ('O3CPU.py','config.ini');
12. #print "\nNo of elemnts is ". scalar @temp. "\n";
13.
14. sub config_parser {
15.             foreach (@_) {
16.                 
17.             if (! open HANDLE1, "<", "$_" ){
18.                 die "Cannot open $_: $!"; } 
19. 
20.             my @array;
21.             while (<HANDLE1>) {
22.             
23.                 chomp $_;   
24.                 if ($_=~ (s/Fetch\s?width/Fetch width /i)               || 
25.                             (s/Issue\s?width/Issue width /i)                ||
26.                         (s/Decode\s?width/Decode width /i)      ||
27.                         (s/Rename\s?width/Rename width /i)      ||
28.                         (s/wb\s?width/Writeback width /i)       || 
29.                         (s/Commit\s?width/Commit width /i)          ||
30.                         (s/LQEntries/Load queue size /i)            ||
31.                         (s/SQEntries/Store queue size /i)           ||
32.                         (s/ROBEntries/ROB buffer size /i)       ||
33.                         (s/Int\s?Regs/Integer registers /i)             ||
34.                         (s/Float\s?Regs/Floating point registers /i)    ||
35.                         (s/Pred\s?Type/Branch predictor type /i)    ||
36.                         (s/Global\s?pred\w+/Global predictor size/i) ||
37.                         (s/Local\s?pred\w+/Local predictor size/i)  ||
38.                         (s/Local\s?Hist\w+/Local History table size/i)
39.                        
40.                     ){
41.                          print "$_\n";
42.                          push @array, "$_";
43.                          }
44.
45.                                 }   # END of While Loop
46.                         }
47. #return @array;             # Return the collected results in an array.
48. close HANDLE1;              # Close the file handle.
49.  }      # END of the Program.




#---------------------------------------------------------------------------#

The error message is :

Modification of a read-only value attempted at stats_parser.pl line 23 (#1)
(F) You tried, directly or indirectly, to change the value of a
constant.  You didn't, of course, try "2 = 1", because the compiler
catches that.  But an easy way to do the same thing is:

    sub mod { $_[0] = 1 }
    mod(2);

Another way is to assign to a substr() that's off the end of the string.

Yet another way is to assign to a foreach loop VAR when VAR
is aliased to a constant in the look LIST:

        $x = 1;
        foreach my $n ($x, 2) {
            $n *= 2; # modifies the $x, but fails on attempt to modify the 2
        }

Uncaught exception from user code:
Modification of a read-only value attempted at stats_parser.pl line 23.
at stats_parser.pl line 23
main::config_parser('O3CPU.py', 'config.ini') called at stats_parser.pl line                    13  

1 个答案:

答案 0 :(得分:2)

问题是perl不喜欢你对匿名$_变量的嵌套使用:

sub config_parser {
    foreach (@_) {
        # $_ is in scope here
        if ( !open HANDLE1, "<", "$_" ) { 
            die "Cannot open $_: $!";
        }   
        while (<HANDLE1>) {
            # This is a different $_
            chomp $_;
        }
    }   
}

给出循环变量名称......它还会使您的代码更具可读性。我还建议使用词法文件句柄而不是旧版的GLOBAL文件句柄。

sub config_parser {
    for my $filename (@_) {
        open my $fh, '<', $filename
          or die "Cannot open $filename: $!";
        while ( my $line = <$fh> ) {
            chomp $line;
            if ( $line =~ (...) ) { 
                ...;
            }
        }
    }
}
}