This is a live mirror of the Perl 5 development currently hosted at https://github.com/perl/perl5
Add t/op/readline.t docs
[perl5.git] / t / op / readline.t
CommitLineData
79628082
SM
1#!./perl
2
3BEGIN {
4 chdir 't';
5 @INC = '../lib';
6 require './test.pl';
7}
8
e4b7ebf3 9plan tests => 18;
79628082 10
5b88351f
JJ
11# [perl #19566]: sv_gets writes directly to its argument via
12# TARG. Test that we respect SvREADONLY.
79628082
SM
13eval { for (\2) { $_ = <FH> } };
14like($@, 'Modification of a read-only value attempted', '[perl #19566]');
15
5b88351f 16# [perl #21628]
ba92458f 17{
1c25d394
NC
18 my $file = tempfile();
19 open A,'+>',$file; $a = 3;
ba92458f
AE
20 is($a .= <A>, 3, '#21628 - $a .= <A> , A eof');
21 close A; $a = 4;
22 is($a .= <A>, 4, '#21628 - $a .= <A> , A closed');
ba92458f 23}
10bcdfd6 24
5b88351f
JJ
25# [perl #21614]: 82 is chosen to exceed the length for sv_grow in
26# do_readline (80)
bfe0b846 27foreach my $k (1, 82) {
10bcdfd6 28 my $result
048e6480 29 = runperl (stdin => '', stderr => 1,
bfe0b846 30 prog => "\$x = q(k) x $k; \$a{\$x} = qw(v); \$_ = <> foreach keys %a; print qw(end)",
10bcdfd6 31 );
bfe0b846
CB
32 $result =~ s/\n\z// if $^O eq 'VMS';
33 is ($result, "end", '[perl #21614] for length ' . length('k' x $k));
10bcdfd6 34}
bc44a8a2
NC
35
36
bfe0b846 37foreach my $k (1, 21) {
bc44a8a2 38 my $result
048e6480 39 = runperl (stdin => ' rules', stderr => 1,
bfe0b846 40 prog => "\$x = q(perl) x $k; \$a{\$x} = q(v); foreach (keys %a) {\$_ .= <>; print}",
bc44a8a2 41 );
bfe0b846
CB
42 $result =~ s/\n\z// if $^O eq 'VMS';
43 is ($result, ('perl' x $k) . " rules", 'rcatline to shared sv for length ' . length('perl' x $k));
bc44a8a2
NC
44}
45
46foreach my $l (1, 82) {
47 my $k = $l;
48 $k = 'k' x $k;
49 my $copy = $k;
50 $k = <DATA>;
51 is ($k, "moo\n", 'catline to COW sv for length ' . length $copy);
52}
53
54
55foreach my $l (1, 21) {
56 my $k = $l;
57 $k = 'perl' x $k;
58 my $perl = $k;
59 $k .= <DATA>;
60 is ($k, "$perl rules\n", 'rcatline to COW sv for length ' . length $perl);
61}
2d726892
TF
62
63use strict;
64use File::Spec;
65
66open F, File::Spec->curdir and sysread F, $_, 1;
67my $err = $! + 0;
68close F;
69
70SKIP: {
389edf24 71 skip "you can read directories as plain files", 2 unless( $err );
2d726892
TF
72
73 $!=0;
74 open F, File::Spec->curdir and $_=<F>;
75 ok( $!==$err && !defined($_) => 'readline( DIRECTORY )' );
76 close F;
77
78 $!=0;
79 { local $/;
80 open F, File::Spec->curdir and $_=<F>;
81 ok( $!==$err && !defined($_) => 'readline( DIRECTORY ) slurp mode' );
82 close F;
83 }
84}
85
7b8203e3
YST
86fresh_perl_is('BEGIN{<>}', '',
87 { switches => ['-w'], stdin => '', stderr => 1 },
88 'No ARGVOUT used only once warning');
89
e4b7ebf3
RGS
90fresh_perl_is('print readline', 'foo',
91 { switches => ['-w'], stdin => 'foo', stderr => 1 },
92 'readline() defaults to *ARGV');
93
48de12d9
RGS
94my $obj = bless [];
95$obj .= <DATA>;
96like($obj, qr/main=ARRAY.*world/, 'rcatline and refs');
97
0f722b55
RGS
98# bug #38631
99require Tie::Scalar;
100tie our $one, 'Tie::StdScalar', "A: ";
101tie our $two, 'Tie::StdScalar', "B: ";
102my $junk = $one;
103$one .= <DATA>;
104$two .= <DATA>;
105is( $one, "A: One\n", "rcatline works with tied scalars" );
106is( $two, "B: Two\n", "rcatline works with tied scalars" );
107
bc44a8a2
NC
108__DATA__
109moo
110moo
111 rules
112 rules
48de12d9 113world
0f722b55
RGS
114One
115Two