This is a live mirror of the Perl 5 development currently hosted at https://github.com/perl/perl5
Remove the port to MiNT. It's a dead platform that hasn't had any love since 5.005
[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
1c8a4223 32print "1..38\n";
8d063cd8 33
5d96a5e0
MS
34# Test do &sub and proper @_ handling.
35$_[0] = 0;
36$result = do foo1(1);
8d063cd8 37
5d96a5e0
MS
38ok( $result eq 'value', ":$result: eq :value:" );
39ok( $_[0] == 0 );
8d063cd8 40
5d96a5e0
MS
41$_[0] = 0;
42$result = do foo2(0,1,0);
43ok( $result eq 'value', ":$result: eq :value:" );
44ok( $_[0] == 0 );
45
46$result = do{ ok 1; 'value';};
47ok( $result eq 'value', ":$result: eq :value:" );
378cc40b
LW
48
49sub blather {
5d96a5e0 50 ok 1 foreach @_;
378cc40b
LW
51}
52
5d96a5e0
MS
53do blather("ayep","sho nuff");
54@x = ("jeepers", "okydoke");
55@y = ("uhhuh", "yeppers");
56do blather(@x,"noofie",@y);
df739378
JH
57
58unshift @INC, '.';
59
60if (open(DO, ">$$.16")) {
5d96a5e0 61 print DO "ok(1, 'do in scalar context') if defined wantarray && not wantarray\n";
d1e4d418 62 close DO or die "Could not close: $!";
df739378
JH
63}
64
e81465be 65my $a = do "$$.16"; die $@ if $@;
df739378
JH
66
67if (open(DO, ">$$.17")) {
5d96a5e0 68 print DO "ok(1, 'do in list context') if defined wantarray && wantarray\n";
d1e4d418 69 close DO or die "Could not close: $!";
df739378
JH
70}
71
e81465be 72my @a = do "$$.17"; die $@ if $@;
df739378
JH
73
74if (open(DO, ">$$.18")) {
5d96a5e0 75 print DO "ok(1, 'do in void context') if not defined wantarray\n";
d1e4d418 76 close DO or die "Could not close: $!";
df739378
JH
77}
78
e81465be 79do "$$.18"; die $@ if $@;
df739378 80
5d96a5e0
MS
81# bug ID 20010920.007
82eval qq{ do qq(a file that does not exist); };
d1e4d418 83ok( !$@, "do on a non-existing file, first try" );
5d96a5e0
MS
84
85eval qq{ do uc qq(a file that does not exist); };
d1e4d418 86ok( !$@, "do on a non-existing file, second try" );
5d96a5e0 87
d4a8e56c
RGS
88# 6 must be interpreted as a file name here
89ok( (!defined do 6) && $!, "'do 6' : $!" );
90
db80722a
RGS
91# [perl #19545]
92push @t, ($u = (do {} . "This should be pushed."));
93ok( $#t == 0, "empty do result value" );
94
edbe35ea
VP
95$zok = '';
96$owww = do { 1 if $zok };
97ok( $owww eq '', 'last is unless' );
98$owww = do { 2 unless not $zok };
99ok( $owww == 1, 'last is if not' );
100
101$zok = 'swish';
102$owww = do { 3 unless $zok };
103ok( $owww eq 'swish', 'last is unless' );
104$owww = do { 4 if not $zok };
105ok( $owww eq '', 'last is if not' );
106
e91684bf 107# [perl #38809]
1c8a4223
VP
108@a = (7);
109$x = sub { do { return do { @a } }; 2 }->();
110ok(defined $x && $x == 1, 'return do { } receives caller scalar context');
111@x = sub { do { return do { @a } }; 2 }->();
112ok("@x" eq "7", 'return do { } receives caller list context');
113
e91684bf
VP
114@a = (7, 8);
115$x = sub { do { return do { 1; @a } }; 3 }->();
1c8a4223 116ok(defined $x && $x == 2, 'return do { ; } receives caller scalar context');
e91684bf 117@x = sub { do { return do { 1; @a } }; 3 }->();
1c8a4223
VP
118ok("@x" eq "7 8", 'return do { ; } receives caller list context');
119
120@b = (11 .. 15);
121$x = sub { do { return do { 1; @a, @b } }; 3 }->();
122ok(defined $x && $x == 5, 'return do { ; , } receives caller scalar context');
123@x = sub { do { return do { 1; @a, @b } }; 3 }->();
124ok("@x" eq "7 8 11 12 13 14 15", 'return do { ; , } receives caller list context');
125
126$x = sub { do { return do { 1; @a }, do { 2; @b } }; 3 }->();
127ok(defined $x && $x == 5, 'return do { ; }, do { ; } receives caller scalar context');
128@x = sub { do { return do { 1; @a }, do { 2; @b } }; 3 }->();
129ok("@x" eq "7 8 11 12 13 14 15", 'return do { ; }, do { ; } receives caller list context');
130
e91684bf
VP
131@a = (7, 8, 9);
132$x = sub { do { do { 1; return @a } }; 4 }->();
133ok(defined $x && $x == 3, 'do { return } receives caller scalar context');
134@x = sub { do { do { 1; return @a } }; 4 }->();
135ok("@x" eq "7 8 9", 'do { return } receives caller list context');
1c8a4223 136
e91684bf
VP
137@a = (7, 8, 9, 10);
138$x = sub { do { return do { 1; do { 2; @a } } }; 5 }->();
1c8a4223 139ok(defined $x && $x == 4, 'return do { do { ; } } receives caller scalar context');
e91684bf 140@x = sub { do { return do { 1; do { 2; @a } } }; 5 }->();
1c8a4223 141ok("@x" eq "7 8 9 10", 'return do { do { ; } } receives caller list context');
e91684bf 142
df739378
JH
143END {
144 1 while unlink("$$.16", "$$.17", "$$.18");
145}