develooper Front page | perl.perl5.porters | Postings from March 2007

[perl #41800] New warning for while(<>) without local($_)

Thread Next
perlbug @ der-pepe . de
March 13, 2007 14:45
[perl #41800] New warning for while(<>) without local($_)
Message ID:
# New Ticket Created by 
# Please include the string:  [perl #41800]
# in the subject line of all future correspondence about this issue. 
# <URL: >

This is a bug report for perl from,
generated with the help of perlbug 1.35 running under perl v5.8.8.

I would like perl to emit a warning when you use while(<...>) without
local($_) or local(*_).  This would prevent you from bad surprises in
cases like this:

for (@a) {
  while (<>) {} # modifies @a

perldoc perlop suggests that you localize $_ before using while(<...>),
however, I see very many people using while(<...>) without doing so;  sometimes
because they copied it from somewhere (even from books), sometimes because they
are simply not aware of the problem.  Often the program will work fine, but
when it grows more complex, you get bugs which are very hard to trace.

The warning would have to appear whenever you use the magical while and there
wasn't a local($_) or local(*_) in the same block before --- unless it is the
outmost block of the main program.


Thread Next Perl Programming lists via nntp and http.
Comments to Ask Bjørn Hansen at | Group listing | About