develooper Front page | perl.perl5.porters | Postings from February 2011

[perl #84724] No implicit assignment to $_ from readline/<> when &CORE::GLOBAL::readline is defined

From:
Marty O'Brien
Date:
February 24, 2011 00:43
Subject:
[perl #84724] No implicit assignment to $_ from readline/<> when &CORE::GLOBAL::readline is defined
Message ID:
rt-3.6.HEAD-24085-1298435050-90.84724-75-0@perl.org
# New Ticket Created by  "Marty O'Brien" 
# Please include the string:  [perl #84724]
# in the subject line of all future correspondence about this issue. 
# <URL: http://rt.perl.org/rt3/Ticket/Display.html?id=84724 >


Perl's behavior that treats the expressions

        while (<HANDLE>)
        while (readline(<HANDLE>))

as equivalent to

        while (defined($_=readline(HANDLE)))

breaks when &CORE::GLOBAL::readline is defined. This makes it
difficult to override the builtin readline function with any legacy
code.

Demo:

BEGIN {
   *CORE::GLOBAL::readline = sub { readline(shift || *ARGV) }
};
($_,$bar) = ("foo\n", "bar\n");
open X, '<', \$bar;
while (<X>) {
   if ($_ eq "bar\n") {
        print "PASS: <X> is the same as ($_=<X>).\n";
   } elsif ($_ eq "foo\n") {
        print "FAIL: <X> is not the same as ($_=<X>).\n";
   }
}

This script fails as written but passes if you comment out the BEGIN block.

Another demo:

Compare:

    $ perl -MO=Deparse -e 'print while <>'
    print $_ while (defined($_ = <ARGV>));   <---- $_ is set implicitly
    -e syntax OK

with:

    $ perl -MO=Deparse -e 'BEGIN{*CORE::GLOBAL::readline=sub{}}' \
             -e 'print while <>'
    sub BEGIN {
        *CORE::GLOBAL::readline = sub {
        }
        ;
    }
    print $_ while readline(ARGV);          <----- $_ is not set !
    -e syntax OK

See also: http://stackoverflow.com/questions/5081767/.

If this isn't a bug, then it is at least underdocumented.




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