xref: /openbsd-src/gnu/usr.bin/perl/dist/IO/t/io_linenum.t (revision eac174f2741a08d8deb8aae59a7f778ef9b5d770)
1b39c5158Smillert#!./perl
2b39c5158Smillert
3b39c5158Smillert# test added 29th April 1999 by Paul Johnson (pjcj@transeda.com)
4b39c5158Smillert# updated    28th May   1999 by Paul Johnson
5b39c5158Smillert
6b39c5158Smillertmy $File;
7b39c5158Smillert
8b39c5158SmillertBEGIN {
9b39c5158Smillert    $File = __FILE__;
10*eac174f2Safresh1    require strict; strict->import();
11b39c5158Smillert}
12b39c5158Smillert
13b8851fccSafresh1use Test::More tests => 12;
14b39c5158Smillert
15b39c5158Smillertuse IO::File;
16b39c5158Smillert
17b39c5158Smillertsub lineno
18b39c5158Smillert{
19b39c5158Smillert  my ($f) = @_;
20b39c5158Smillert  my $l;
21b39c5158Smillert  $l .= "$. ";
22b39c5158Smillert  $l .= $f->input_line_number;
23b39c5158Smillert  $l .= " $.";                     # check $. before and after input_line_number
24b39c5158Smillert  $l;
25b39c5158Smillert}
26b39c5158Smillert
27b39c5158Smillertmy $t;
28b39c5158Smillert
299f11ffb7Safresh1open (F, '<', $File) or die $!;
30b39c5158Smillertmy $io = IO::File->new($File) or die $!;
31b39c5158Smillert
32b39c5158Smillert<F> for (1 .. 10);
33b8851fccSafresh1is(lineno($io), "10 0 10");
34b39c5158Smillert
35b39c5158Smillert$io->getline for (1 .. 5);
36b8851fccSafresh1is(lineno($io), "5 5 5");
37b39c5158Smillert
38b39c5158Smillert<F>;
39b8851fccSafresh1is(lineno($io), "11 5 11");
40b39c5158Smillert
41b39c5158Smillert$io->getline;
42b8851fccSafresh1is(lineno($io), "6 6 6");
43b39c5158Smillert
44b39c5158Smillert$t = tell F;                                        # tell F; provokes a warning
45b8851fccSafresh1is(lineno($io), "11 6 11");
46b39c5158Smillert
47b39c5158Smillert<F>;
48b8851fccSafresh1is(lineno($io), "12 6 12");
49b39c5158Smillert
50b39c5158Smillertselect F;
51b8851fccSafresh1is(lineno($io), "12 6 12");
52b39c5158Smillert
53b39c5158Smillert<F> for (1 .. 10);
54b8851fccSafresh1is(lineno($io), "22 6 22");
55b39c5158Smillert
56b39c5158Smillert$io->getline for (1 .. 5);
57b8851fccSafresh1is(lineno($io), "11 11 11");
58b39c5158Smillert
59b39c5158Smillert$t = tell F;
60b39c5158Smillert# We used to have problems here before local $. worked.
61b39c5158Smillert# input_line_number() used to use select and tell.  When we did the
62b8851fccSafresh1# same, that mechanism brise.  It should work now.
63b8851fccSafresh1is(lineno($io), "22 11 22");
64b39c5158Smillert
65b39c5158Smillert{
66b39c5158Smillert  local $.;
67b39c5158Smillert  $io->getline for (1 .. 5);
68b8851fccSafresh1  is(lineno($io), "16 16 16");
69b39c5158Smillert}
70b39c5158Smillert
71b8851fccSafresh1is(lineno($io), "22 16 22");
72