develooper Front page | perl.perl5.porters | Postings from May 2012

[perl #52702] Consistent localization between foreach (...) and while (<fh>)

Thread Next
From:
James E Keenan via RT
Date:
May 7, 2012 17:18
Subject:
[perl #52702] Consistent localization between foreach (...) and while (<fh>)
Message ID:
rt-3.6.HEAD-4610-1336436327-19.52702-15-0@perl.org
On Thu Apr 10 06:41:40 2008, eda@waniasset.com wrote:
> This is a bug report for perl from eda@waniasset.com,
> generated with the help of perlbug 1.36 running under perl 5.10.0.
> 
> 
> -----------------------------------------------------------------
> [Please enter your report here]
> 
> This is an enhancement request for a small change to the perl
> language.  A 'foreach (1, 2, 3) {}' loop implicitly localizes $_ but a
> 'while (<$fh>)' does not.  I suggest changing the semantics of 'while
> (<$fh>)' so it does the same localization, or if this is too much to
> stomach, adding a warning if the 'while (<$fh>)' construct is used in
> a subroutine without localizing $_ first.
> 
> #!/usr/bin/perl
> use warnings;
> use strict;
> use 5.010;
> $_ = 'x';
> open FH, '<', '/dev/null' or die;
> while (<FH>) {}
> say defined($_) ? "defined: $_" : 'not defined';
> $_ = 'y';
> foreach (1, 2, 3) {}
> say defined($_) ? "defined: $_" : 'not defined';
> 
> This shows that a while-loop doesn't localize $_ but a foreach-loop
> does.  This is the documented behaviour, so this is not a bug.  It
> might be worth changing it for perl 5.12.
> 
> sub my_sub {
>   open my $fh, '<', 'some_file' or die;
>   while (<$fh>) { say 'hi' }
> }
> 
> This code doesn't produce a warning on current perl, but I think it
> should say
> 
> warning: while (<filehandle>) implicitly sets $_, but it is not
>   localized first in subroutine my_sub
> 
> or some similar warning.
> 

List:

This request for a change in Perl's behavior was made several years ago
but didn't garner any support.  Can we close the ticket now?

Thank you very much.
Jim Keenan

---
via perlbug:  queue: perl5 status: open
https://rt.perl.org:443/rt3/Ticket/Display.html?id=52702

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