This is a live mirror of the Perl 5 development currently hosted at https://github.com/perl/perl5
Integrate perlhist entries for 5.8.6 and its perldelta to blead
[perl5.git] / t / op / do.t
CommitLineData
8d063cd8
LW
1#!./perl
2
79072805 3# $RCSfile: do.t,v $$Revision: 4.1 $$Date: 92/08/07 18:27:45 $
378cc40b 4
8d063cd8
LW
5sub foo1
6{
5d96a5e0 7 ok($_[0]);
8d063cd8
LW
8 'value';
9}
10
11sub foo2
12{
6d4ff0d2 13 shift;
5d96a5e0 14 ok($_[0]);
8d063cd8
LW
15 $x = 'value';
16 $x;
17}
18
5d96a5e0
MS
19my $test = 1;
20sub ok {
21 my($ok, $name) = @_;
22
23 # You have to do it this way or VMS will get confused.
24 printf "%s %d%s\n", $ok ? "ok" : "not ok",
25 $test,
26 defined $name ? " - $name" : '';
27
28 printf "# Failed test at line %d\n", (caller)[2] unless $ok;
29
30 $test++;
31 return $ok;
32}
33
db80722a 34print "1..22\n";
8d063cd8 35
5d96a5e0
MS
36# Test do &sub and proper @_ handling.
37$_[0] = 0;
38$result = do foo1(1);
8d063cd8 39
5d96a5e0
MS
40ok( $result eq 'value', ":$result: eq :value:" );
41ok( $_[0] == 0 );
8d063cd8 42
5d96a5e0
MS
43$_[0] = 0;
44$result = do foo2(0,1,0);
45ok( $result eq 'value', ":$result: eq :value:" );
46ok( $_[0] == 0 );
47
48$result = do{ ok 1; 'value';};
49ok( $result eq 'value', ":$result: eq :value:" );
378cc40b
LW
50
51sub blather {
5d96a5e0 52 ok 1 foreach @_;
378cc40b
LW
53}
54
5d96a5e0
MS
55do blather("ayep","sho nuff");
56@x = ("jeepers", "okydoke");
57@y = ("uhhuh", "yeppers");
58do blather(@x,"noofie",@y);
df739378
JH
59
60unshift @INC, '.';
61
62if (open(DO, ">$$.16")) {
5d96a5e0 63 print DO "ok(1, 'do in scalar context') if defined wantarray && not wantarray\n";
d1e4d418 64 close DO or die "Could not close: $!";
df739378
JH
65}
66
67my $a = do "$$.16";
68
69if (open(DO, ">$$.17")) {
5d96a5e0 70 print DO "ok(1, 'do in list context') if defined wantarray && wantarray\n";
d1e4d418 71 close DO or die "Could not close: $!";
df739378
JH
72}
73
74my @a = do "$$.17";
75
76if (open(DO, ">$$.18")) {
5d96a5e0 77 print DO "ok(1, 'do in void context') if not defined wantarray\n";
d1e4d418 78 close DO or die "Could not close: $!";
df739378
JH
79}
80
81do "$$.18";
82
5d96a5e0
MS
83# bug ID 20010920.007
84eval qq{ do qq(a file that does not exist); };
d1e4d418 85ok( !$@, "do on a non-existing file, first try" );
5d96a5e0
MS
86
87eval qq{ do uc qq(a file that does not exist); };
d1e4d418 88ok( !$@, "do on a non-existing file, second try" );
5d96a5e0 89
d4a8e56c
RGS
90# 6 must be interpreted as a file name here
91ok( (!defined do 6) && $!, "'do 6' : $!" );
92
db80722a
RGS
93# [perl #19545]
94push @t, ($u = (do {} . "This should be pushed."));
95ok( $#t == 0, "empty do result value" );
96
df739378
JH
97END {
98 1 while unlink("$$.16", "$$.17", "$$.18");
99}