Login | Register For Free | Help
Search for: (Advanced)

Mailing List Archive: Perl: porters
[perl #52702] Consistent localization between foreach (...) and while (<fh>)
 

Index | Next | Previous | View Flat


perlbug-followup at perl

May 7, 2012, 5:18 PM


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

On Thu Apr 10 06:41:40 2008, eda [at] waniasset wrote:
> This is a bug report for perl from eda [at] waniasset,
> 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

Subject User Time
[perl #52702] Consistent localization between foreach (...) and while (<fh>) perlbug-followup at perl May 7, 2012, 5:18 PM
    Re: [perl #52702] Consistent localization between foreach (...) and while (<fh>) eda at waniasset May 8, 2012, 2:28 AM
    [perl #52702] Consistent localization between foreach (...) and while (<fh>) perlbug-followup at perl May 8, 2012, 5:23 AM

  Index | Next | Previous | View Flat
 
 


Interested in having your list archived? Contact Gossamer Threads
 
  Web Applications & Managed Hosting Powered by Gossamer Threads Inc.