From the keyboard of Peter Scott [13.09.06,09:20]:

> Here's a distillation of something that just bit me, behaves the same on
> 5.6.1 and 5.8.5.  Observe the following program:

Try this :-)

$_ = "bar";
print "before the loop: \$_ = $_\n";
for (1..1) {
  print "before print_file(): \$_ = $_\n";
  print_file($0);
  print "after print_file(): \$_ = $_\n";
}

sub print_file {
  print "before while: \$_ = $_\n";
  local @ARGV = shift;
  while (<>) { print }
  print "after while: \$_ = $_\n";
  $_ = "foo";
}
print "before __END__: \$_ = $_\n";
__END__

gives:

before the loop: $_ = bar
before print_file(): $_ = 1
before while: $_ = 1
$_ = "bar";
print "before the loop: \$_ = $_\n";
for (1..1) {
  print "before print_file(): \$_ = $_\n";
  print_file($0);
  print "after print_file(): \$_ = $_\n";
}

sub print_file {
  print "before while: \$_ = $_\n";
  local @ARGV = shift;
  while (<>) { print }
  print "after while: \$_ = $_\n";
  $_ = "foo";
}
print "before __END__: \$_ = $_\n";
__END__
after while: $_ =
after print_file(): $_ = foo
before __END__: $_ = bar

> Before running it, can you tell:
>   Will it succeed or not?
>   If not, with what error?
>   Where?
>   Why?

The error is thrown at the moment the <> oprator tries to assign to $_,
which is an alias to a constant - the '1'.

Common pitfall ;-)

The <> operator does no further aliasing, it operates upon $_ at it's
"current aliasing level".

0--gg-

-- 
_($_=" "x(1<<5)."?\n".q·/)Oo.  G°\        /
                              /\_¯/(q    /
----------------------------  \__(m.====·.(_("always off the crowd"))."·
");sub _{s,/,($e="'Itrs `mnsgdq Gdbj O`qkdq")=~y/"-y/#-z/;$e,e && print}

Reply via email to