This is a live mirror of the Perl 5 development currently hosted at https://github.com/perl/perl5
Make the test more portable.
[perl5.git] / t / lib / io_linenum.t
CommitLineData
91cce263
PJ
1#!./perl
2
1e374101
PJ
3# test added 29th April 1999 by Paul Johnson (pjcj@transeda.com)
4# updated 28th May 1999 by Paul Johnson
91cce263 5
1e374101
PJ
6my $File;
7
8BEGIN
9{
10 $File = __FILE__;
11 if (-d 't')
12 {
13 chdir 't';
14 $File =~ s/^t\W+//; # Remove first directory
15 }
16 unshift @INC, '../lib' if -d '../lib';
18bb4703 17 require strict; import strict;
91cce263
PJ
18}
19
91cce263
PJ
20use Test;
21
1e374101
PJ
22BEGIN { plan tests => 12 }
23
24use IO::File;
91cce263
PJ
25
26sub lineno
27{
28 my ($f) = @_;
29 my $l;
30 $l .= "$. ";
31 $l .= $f->input_line_number;
1e374101 32 $l .= " $."; # check $. before and after input_line_number
91cce263
PJ
33 $l;
34}
35
91cce263
PJ
36my $t;
37
1e374101
PJ
38open (F, $File) or die $!;
39my $io = IO::File->new($File) or die $!;
40
41<F> for (1 .. 10);
42ok(lineno($io), "10 0 10");
91cce263 43
1e374101
PJ
44$io->getline for (1 .. 5);
45ok(lineno($io), "5 5 5");
91cce263 46
1e374101
PJ
47<F>;
48ok(lineno($io), "11 5 11");
91cce263 49
1e374101
PJ
50$io->getline;
51ok(lineno($io), "6 6 6");
91cce263 52
1e374101
PJ
53$t = tell F; # tell F; provokes a warning
54ok(lineno($io), "11 6 11");
91cce263 55
1e374101
PJ
56<F>;
57ok(lineno($io), "12 6 12");
91cce263 58
1e374101
PJ
59select F;
60ok(lineno($io), "12 6 12");
91cce263 61
1e374101
PJ
62<F> for (1 .. 10);
63ok(lineno($io), "22 6 22");
91cce263 64
1e374101
PJ
65$io->getline for (1 .. 5);
66ok(lineno($io), "11 11 11");
67
68$t = tell F;
69# We used to have problems here before local $. worked.
70# input_line_number() used to use select and tell. When we did the
71# same, that mechanism broke. It should work now.
72ok(lineno($io), "22 11 22");
73
74{
75 local $.;
76 $io->getline for (1 .. 5);
77 ok(lineno($io), "16 16 16");
78}
91cce263 79
1e374101 80ok(lineno($io), "22 16 22");