From: Paul Johnson Date: Fri, 28 May 1999 15:11:18 +0000 (+0100) Subject: Fixes related to working local $. X-Git-Url: http://git.shadowcat.co.uk/gitweb/gitweb.cgi?a=commitdiff_plain;h=1e374101a32f2df640b9fad36d86b2ed88f6eaf8;p=p5sagit%2Fp5-mst-13.2.git Fixes related to working local $. To: perl5-porters Message-ID: <19990528151118.A289@west-tip.transeda.com> p4raw-id: //depot/cfgperl@3494 --- diff --git a/ext/IO/lib/IO/Handle.pm b/ext/IO/lib/IO/Handle.pm index 02595e5..30ee2e5 100644 --- a/ext/IO/lib/IO/Handle.pm +++ b/ext/IO/lib/IO/Handle.pm @@ -468,30 +468,12 @@ sub input_record_separator { } sub input_line_number { - # local $. does not work properly, so we need to do it some other - # way. We use select, although this is not quite right. What we - # really need to know is the file handle that was the subject of the - # last read, seek or tell. - my $now = select; - my $keep = $.; - my $tell = tell qualify($_[0], caller) if ref($_[0]); - my $prev = $.; - $. = $_[1] if @_ > 1; - no strict "refs"; - $tell = tell $now; - $. = $keep; - $prev; -} - -=for when local $. works properly -sub input_line_number { local $.; my $tell = tell qualify($_[0], caller) if ref($_[0]); my $prev = $.; $. = $_[1] if @_ > 1; $prev; } -=cut sub format_page_number { my $old = new SelectSaver qualify($_[0], caller) if ref($_[0]); diff --git a/pod/perlvar.pod b/pod/perlvar.pod index cb41c96..9402608 100644 --- a/pod/perlvar.pod +++ b/pod/perlvar.pod @@ -220,8 +220,10 @@ to change that. An explicit close on a filehandle resets the line number. Because CE> never does an explicit close, line numbers increase across ARGV files (but see examples in L). Consider this variable read-only: setting it does not reposition -the seek pointer; you'll have to do that on your own. (Mnemonic: -many programs use "." to mean the current line number.) +the seek pointer; you'll have to do that on your own. Localizing C<$.> +has the effect of also localizing Perl's notion of "the last read +filehandle". (Mnemonic: many programs use "." to mean the current line +number.) =item input_record_separator HANDLE EXPR diff --git a/t/lib/io_linenum.t b/t/lib/io_linenum.t index 0d28e18..e981b95 100755 --- a/t/lib/io_linenum.t +++ b/t/lib/io_linenum.t @@ -1,19 +1,28 @@ #!./perl -# test added 29th April 1998 by Paul Johnson (pjcj@transeda.com) +# test added 29th April 1999 by Paul Johnson (pjcj@transeda.com) +# updated 28th May 1999 by Paul Johnson -BEGIN { - chdir 't' if -d 't'; - unshift @INC, '../lib' if -d '../lib'; +use strict; + +my $File; + +BEGIN +{ + $File = __FILE__; + if (-d 't') + { + chdir 't'; + $File =~ s/^t\W+//; # Remove first directory + } + unshift @INC, '../lib' if -d '../lib'; } -use strict; -use IO::File; use Test; -BEGIN { - plan tests => 9 #, todo => [10] -} +BEGIN { plan tests => 12 } + +use IO::File; sub lineno { @@ -21,49 +30,52 @@ sub lineno my $l; $l .= "$. "; $l .= $f->input_line_number; - $l .= " $."; + $l .= " $."; # check $. before and after input_line_number $l; } -sub OK -{ - my $s = select STDOUT; # work around a bug in Test.pm 1.04 - &ok; - select $s; -} - my $t; -open (Q, __FILE__) or die $!; -my $w = IO::File->new(__FILE__) or die $!; +open (F, $File) or die $!; +my $io = IO::File->new($File) or die $!; + + for (1 .. 10); +ok(lineno($io), "10 0 10"); - for (1 .. 10); -OK(lineno($w), "10 0 10"); +$io->getline for (1 .. 5); +ok(lineno($io), "5 5 5"); -$w->getline for (1 .. 5); -OK(lineno($w), "5 5 5"); +; +ok(lineno($io), "11 5 11"); -; -OK(lineno($w), "11 5 11"); +$io->getline; +ok(lineno($io), "6 6 6"); -$w->getline; -OK(lineno($w), "6 6 6"); +$t = tell F; # tell F; provokes a warning +ok(lineno($io), "11 6 11"); -$t = tell Q; # tell Q; provokes a warning - the world is full of bugs... -OK(lineno($w), "11 6 11"); +; +ok(lineno($io), "12 6 12"); -; -OK(lineno($w), "12 6 12"); +select F; +ok(lineno($io), "12 6 12"); -select Q; -OK(lineno($w), "12 6 12"); + for (1 .. 10); +ok(lineno($io), "22 6 22"); - for (1 .. 10); -OK(lineno($w), "22 6 22"); +$io->getline for (1 .. 5); +ok(lineno($io), "11 11 11"); + +$t = tell F; +# We used to have problems here before local $. worked. +# input_line_number() used to use select and tell. When we did the +# same, that mechanism broke. It should work now. +ok(lineno($io), "22 11 22"); + +{ + local $.; + $io->getline for (1 .. 5); + ok(lineno($io), "16 16 16"); +} -$w->getline for (1 .. 5); -OK(lineno($w), "11 11 11"); -__END__ -# This test doesn't work. It probably won't until local $. does. -$t = tell Q; -OK(lineno($w), "22 11 22", 'waiting for local $.'); +ok(lineno($io), "22 16 22");