/
11-sub.t
90 lines (65 loc) · 1.42 KB
/
11-sub.t
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
#!./parrot nqp.pbc
# check subs
say('1..14');
sub one ( ) {
say("ok 1 # sub def and call");
}
one();
{
sub two ( ) {
say("ok 2 # sub def and call inside block");
}
two();
}
sub three ( ) { say("ok 3 # sub def; sub call on same line"); }; three();
sub four_five ($arg1) {
say($arg1);
}
four_five('ok 4 # passed in 1 arg');
{
four_five('ok 5 # calling sub in outer scope');
}
{
our sub six ( ) {
say("ok 6 # def in inner scope, called from outer scope");
}
}
six();
sub seven () {
"ok 7 # return string literal from sub";
}
say(seven());
sub eight () {
"ok 8 # bind sub return to variable";
}
my $retVal := eight();
unless $retVal {
print("not ");
}
say($retVal);
sub add_stomp ($first, $last) {
my $sum := $first + $last;
$first := $last - $first;
$sum;
}
print("ok "); print(add_stomp(3,6)); say(" # returning the result of operating on arguments");
my $five := 5;
my $seven := 7;
add_stomp($five, $seven);
if $five != 5 {
print("not ");
}
say("ok 10 # subroutines that operate on args do not affect the original arg outside the sub");
sub eleven ($arg) {
say("ok 11 # parameter with a trailing comma");
}
eleven( 'dummy', );
sub &twelve() {
say("ok 12 # subroutine name with leading &");
}
&twelve();
# test that a sub can start with Q
sub Qstuff() { 13 };
say('ok ', Qstuff());
sub term:sym<self>() { 14 }
say('ok ', term:sym<self>());