开发者

"Modification of a read-only value" on while loop with implicit $_ variable

I don't really understand开发者_高级运维 why the following piece of perl code

#!/usr/bin/perl -w

use strict;
use warnings;

strange($_) for qw(a b c);

sub strange {
  open FILE, '<', 'some_file.txt' or die;
  while (<FILE>) { } # this is line 10
  close FILE;
}

Is throwing the following error

Modification of a read-only value attempted at ./bug.pl line 10.

Is this a bug? Or there is something I should know about the usage of the magic/implicit variable $_?


The while (<fh>) construct implicitly assigns to the global variable $_.

This is described in perlop:

If and only if the input symbol is the only thing inside the conditional of a while statement (...), the value is automatically assigned to the global variable $_, destroying whatever was there previously. (...) The $_ variable is not implicitly localized. You'll have to put a local $_; before the loop if you want that to happen.

The error is thrown because $_ is initially aliased to a constant value ("a").

You can avoid this by declaring a lexical variable:

while (my $line = <FILE>) {
    # do something with $line
}


Yes, the while-loop reads into $_ which at that point is aliased to a constant (the string "a"). You should use local $_; before the while-loop, or read into a separate variable.

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜