forked from Perl/perl5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Make IO::Handle::getline(s) respect the open pragma
See <https://rt.cpan.org/Ticket/Display.html?id=66474>. Also, this came up in <https://rt.perl.org/rt3/Ticket/Display.html?id=92728>. The <> operator, when reading from the magic ARGV handle, automatic- ally opens the next file. Layers set by the lexical open pragma are applied, if they are in scope at the point where <> is used. This works almost all the time, because the common convention is: use open ":utf8"; while(<>) { ... } IO::Handle’s getline and getlines methods are Perl subroutines that call <> themselves. But that happens within the scope of IO/Handle.pm, so the caller’s I/O layer settings are ignored. That means that these two expressions are not equivalent within in a ‘use open’ scope: <> *ARGV->getline The latter will open the next file with no layers applied. This commit solves that by putting PL_check hooks in place in IO::Handle before compiling the getline and getlines subroutines. Those hooks cause every state op (nextstate, or dbstate under the debugger) to have a custom pp function that saves the previous value of PL_curcop, calls the default pp function, and then restores PL_curcop. That means that getline and getlines run with the caller’s compile- time hints. Another way to see it is that getline and getlines’s own lexical hints are never activated. (A state op carries all the lexical pragmata. Every statement has one. When any op executes, it’s ‘pp’ function is called. pp_nextstate and pp_dbstate both set PL_curcop to the op itself. Any code that checks hints looks at PL_curcop, which contains the current run-time hints.)
- Loading branch information
Father Chrysostomos
committed
Sep 17, 2011
1 parent
7d167fe
commit 986a805
Showing
7 changed files
with
99 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
#!./perl | ||
|
||
BEGIN { | ||
unless ($] >= 5.008 and find PerlIO::Layer 'perlio') { | ||
print "1..0 # Skip: not perlio\n"; | ||
exit 0; | ||
} | ||
require($ENV{PERL_CORE} ? "../../t/test.pl" : "./t/test.pl"); | ||
} | ||
|
||
use utf8; | ||
|
||
|
||
plan(tests => 2); | ||
|
||
open my $fh, ">", 'io_utf8argv'; | ||
print $fh | ||
"\xce\x9c\xe1\xbd\xb7\xce\xb1\x20\xcf\x80\xe1\xbd\xb1\xcf\x80\xce". | ||
"\xb9\xce\xb1\x2c\x20\xce\xbc\xe1\xbd\xb0\x20\xcf\x80\xce\xbf\xce". | ||
"\xb9\xe1\xbd\xb0\x20\xcf\x80\xe1\xbd\xb1\xcf\x80\xce\xb9\xce\xb1". | ||
"\xcd\xbe\x0a"; | ||
close $fh or die "close: $!"; | ||
|
||
|
||
use open ":std", ":utf8"; | ||
|
||
use IO::Handle; | ||
|
||
@ARGV = ('io_utf8argv') x 2; | ||
is *ARGV->getline, "Μία πάπια, μὰ ποιὰ πάπια;\n", | ||
'getline respects open pragma when magically opening ARGV'; | ||
|
||
is join('',*ARGV->getlines), "Μία πάπια, μὰ ποιὰ πάπια;\n", | ||
'getlines respects open pragma when magically opening ARGV'; | ||
|
||
END { | ||
1 while unlink "io_utf8argv"; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters