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

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

Thread Next
From:
perlbug @ der-pepe . de
Date:
March 13, 2007 14:45
Subject:
[perl #41800] New warning for while(<>) without local($_)
Message ID:
rt-3.6.HEAD-30201-1173798328-1542.41800-75-0@perl.org
# New Ticket Created by  perlbug@der-pepe.de 
# Please include the string:  [perl #41800]
# in the subject line of all future correspondence about this issue. 
# <URL: http://rt.perl.org/rt3/Ticket/Display.html?id=41800 >


This is a bug report for perl from perlbug@der-pepe.de,
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.

Regards,
Christoph
-----------------------------------------------------------------


Thread Next


nntp.perl.org: Perl Programming lists via nntp and http.
Comments to Ask Bjørn Hansen at ask@perl.org | Group listing | About