This is a live mirror of the Perl 5 development currently hosted at https://github.com/perl/perl5
Convert t/op/pwent.t to test.pl, strict and warnings.
[perl5.git] / t / op / do.t
CommitLineData
8d063cd8
LW
1#!./perl
2
8d063cd8
LW
3sub foo1
4{
5d96a5e0 5 ok($_[0]);
8d063cd8
LW
6 'value';
7}
8
9sub foo2
10{
6d4ff0d2 11 shift;
5d96a5e0 12 ok($_[0]);
8d063cd8
LW
13 $x = 'value';
14 $x;
15}
16
5d96a5e0
MS
17my $test = 1;
18sub ok {
19 my($ok, $name) = @_;
20
21 # You have to do it this way or VMS will get confused.
22 printf "%s %d%s\n", $ok ? "ok" : "not ok",
23 $test,
24 defined $name ? " - $name" : '';
25
26 printf "# Failed test at line %d\n", (caller)[2] unless $ok;
27
28 $test++;
29 return $ok;
30}
31
6d5ba409 32print "1..50\n";
8d063cd8 33
5d96a5e0
MS
34# Test do &sub and proper @_ handling.
35$_[0] = 0;
96ccdd02
NC
36{
37 no warnings 'deprecated';
38 $result = do foo1(1);
39}
8d063cd8 40
5d96a5e0
MS
41ok( $result eq 'value', ":$result: eq :value:" );
42ok( $_[0] == 0 );
8d063cd8 43
5d96a5e0 44$_[0] = 0;
96ccdd02
NC
45{
46 no warnings 'deprecated';
47 $result = do foo2(0,1,0);
48}
5d96a5e0
MS
49ok( $result eq 'value', ":$result: eq :value:" );
50ok( $_[0] == 0 );
51
52$result = do{ ok 1; 'value';};
53ok( $result eq 'value', ":$result: eq :value:" );
378cc40b
LW
54
55sub blather {
5d96a5e0 56 ok 1 foreach @_;
378cc40b
LW
57}
58
96ccdd02
NC
59{
60 no warnings 'deprecated';
61 do blather("ayep","sho nuff");
62}
5d96a5e0
MS
63@x = ("jeepers", "okydoke");
64@y = ("uhhuh", "yeppers");
96ccdd02
NC
65{
66 no warnings 'deprecated';
67 do blather(@x,"noofie",@y);
68}
df739378
JH
69
70unshift @INC, '.';
71
72if (open(DO, ">$$.16")) {
5d96a5e0 73 print DO "ok(1, 'do in scalar context') if defined wantarray && not wantarray\n";
d1e4d418 74 close DO or die "Could not close: $!";
df739378
JH
75}
76
e81465be 77my $a = do "$$.16"; die $@ if $@;
df739378
JH
78
79if (open(DO, ">$$.17")) {
5d96a5e0 80 print DO "ok(1, 'do in list context') if defined wantarray && wantarray\n";
d1e4d418 81 close DO or die "Could not close: $!";
df739378
JH
82}
83
e81465be 84my @a = do "$$.17"; die $@ if $@;
df739378
JH
85
86if (open(DO, ">$$.18")) {
5d96a5e0 87 print DO "ok(1, 'do in void context') if not defined wantarray\n";
d1e4d418 88 close DO or die "Could not close: $!";
df739378
JH
89}
90
e81465be 91do "$$.18"; die $@ if $@;
df739378 92
5d96a5e0
MS
93# bug ID 20010920.007
94eval qq{ do qq(a file that does not exist); };
d1e4d418 95ok( !$@, "do on a non-existing file, first try" );
5d96a5e0
MS
96
97eval qq{ do uc qq(a file that does not exist); };
d1e4d418 98ok( !$@, "do on a non-existing file, second try" );
5d96a5e0 99
d4a8e56c
RGS
100# 6 must be interpreted as a file name here
101ok( (!defined do 6) && $!, "'do 6' : $!" );
102
db80722a
RGS
103# [perl #19545]
104push @t, ($u = (do {} . "This should be pushed."));
105ok( $#t == 0, "empty do result value" );
106
edbe35ea
VP
107$zok = '';
108$owww = do { 1 if $zok };
109ok( $owww eq '', 'last is unless' );
110$owww = do { 2 unless not $zok };
111ok( $owww == 1, 'last is if not' );
112
113$zok = 'swish';
114$owww = do { 3 unless $zok };
115ok( $owww eq 'swish', 'last is unless' );
116$owww = do { 4 if not $zok };
117ok( $owww eq '', 'last is if not' );
118
e91684bf 119# [perl #38809]
1c8a4223
VP
120@a = (7);
121$x = sub { do { return do { @a } }; 2 }->();
122ok(defined $x && $x == 1, 'return do { } receives caller scalar context');
123@x = sub { do { return do { @a } }; 2 }->();
124ok("@x" eq "7", 'return do { } receives caller list context');
125
e91684bf
VP
126@a = (7, 8);
127$x = sub { do { return do { 1; @a } }; 3 }->();
1c8a4223 128ok(defined $x && $x == 2, 'return do { ; } receives caller scalar context');
e91684bf 129@x = sub { do { return do { 1; @a } }; 3 }->();
1c8a4223
VP
130ok("@x" eq "7 8", 'return do { ; } receives caller list context');
131
132@b = (11 .. 15);
133$x = sub { do { return do { 1; @a, @b } }; 3 }->();
134ok(defined $x && $x == 5, 'return do { ; , } receives caller scalar context');
135@x = sub { do { return do { 1; @a, @b } }; 3 }->();
136ok("@x" eq "7 8 11 12 13 14 15", 'return do { ; , } receives caller list context');
137
138$x = sub { do { return do { 1; @a }, do { 2; @b } }; 3 }->();
139ok(defined $x && $x == 5, 'return do { ; }, do { ; } receives caller scalar context');
140@x = sub { do { return do { 1; @a }, do { 2; @b } }; 3 }->();
141ok("@x" eq "7 8 11 12 13 14 15", 'return do { ; }, do { ; } receives caller list context');
142
e91684bf
VP
143@a = (7, 8, 9);
144$x = sub { do { do { 1; return @a } }; 4 }->();
145ok(defined $x && $x == 3, 'do { return } receives caller scalar context');
146@x = sub { do { do { 1; return @a } }; 4 }->();
147ok("@x" eq "7 8 9", 'do { return } receives caller list context');
1c8a4223 148
e91684bf
VP
149@a = (7, 8, 9, 10);
150$x = sub { do { return do { 1; do { 2; @a } } }; 5 }->();
1c8a4223 151ok(defined $x && $x == 4, 'return do { do { ; } } receives caller scalar context');
e91684bf 152@x = sub { do { return do { 1; do { 2; @a } } }; 5 }->();
1c8a4223 153ok("@x" eq "7 8 9 10", 'return do { do { ; } } receives caller list context');
e91684bf 154
dd3e51dc
VP
155# Do blocks created by constant folding
156# [perl #68108]
157$x = sub { if (1) { 20 } }->();
158ok($x == 20, 'if (1) { $x } receives caller scalar context');
159
160@a = (21 .. 23);
161$x = sub { if (1) { @a } }->();
162ok($x == 3, 'if (1) { @a } receives caller scalar context');
163@x = sub { if (1) { @a } }->();
164ok("@x" eq "21 22 23", 'if (1) { @a } receives caller list context');
165
166$x = sub { if (1) { 0; 20 } }->();
167ok($x == 20, 'if (1) { ...; $x } receives caller scalar context');
168
169@a = (24 .. 27);
170$x = sub { if (1) { 0; @a } }->();
171ok($x == 4, 'if (1) { ...; @a } receives caller scalar context');
172@x = sub { if (1) { 0; @a } }->();
173ok("@x" eq "24 25 26 27", 'if (1) { ...; @a } receives caller list context');
174
ef9da979
FC
175$x = sub { if (1) { 0; 20 } else{} }->();
176ok($x == 20, 'if (1) { ...; $x } else{} receives caller scalar context');
177
178@a = (24 .. 27);
179$x = sub { if (1) { 0; @a } else{} }->();
180ok($x == 4, 'if (1) { ...; @a } else{} receives caller scalar context');
181@x = sub { if (1) { 0; @a } else{} }->();
182ok("@x" eq "24 25 26 27", 'if (1) { ...; @a } else{} receives caller list context');
183
184$x = sub { if (0){} else { 0; 20 } }->();
185ok($x == 20, 'if (0){} else { ...; $x } receives caller scalar context');
186
187@a = (24 .. 27);
188$x = sub { if (0){} else { 0; @a } }->();
189ok($x == 4, 'if (0){} else { ...; @a } receives caller scalar context');
190@x = sub { if (0){} else { 0; @a } }->();
191ok("@x" eq "24 25 26 27", 'if (0){} else { ...; @a } receives caller list context');
192
193
df739378
JH
194END {
195 1 while unlink("$$.16", "$$.17", "$$.18");
196}