-
Notifications
You must be signed in to change notification settings - Fork 0
/
Subroutines_#return#subroutineExit#Marine#persistent#state.pl
149 lines (110 loc) · 2.83 KB
/
Subroutines_#return#subroutineExit#Marine#persistent#state.pl
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
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
#! /usr/perl
use warnings;
# Subroutines examples, Early Subroutine Exit
# Subroutines Chapter 4
# sub marine
# {
# $n +=1;
# print "Hello, sailor $n!\n";
# }
# &marine;
# &marine;
# &marine;
# &marine;
# &marine;
# \/
# ||
# ||
#do not put a fucking semi\/ colon here or shit goes haywire !
# sub sum_of_fred_and_barney
# {
# print "Hey, you called the sum_of_fred_and_barney subroutine!\n";
# $fred + $barney; #That's the return value
# # print "Hey, I'm returing a value from!\n"; #oops #Not the addition anymore; it's now the print statement
# #whose return value is normally 1, meaning "printing was succesful"
# } #not the return value requested, be carefull adding additional code
# #to a subroutine, since the last expression evaluated will be the return value
# #a failure would result in return value of 0 or (false?)
# $fred = 3;
# $barney = 4;
# $wilma = &sum_of_fred_and_barney;
# print "\$wilma is $wilma.\n";
# $betty = 3 * &sum_of_fred_and_barney;
# print "\$betty is $betty.\n";
# $dino = 5 * &sum_of_fred_and_barney;
# print "\$dino is $dino.\n";
# Messing around:
#
#
# $fred = <STDIN>;
# $barney = <STDIN>;
# $bammbamm = &sum_of_fred_and_barney;
# print "\bammbamm is $bammbamm.\n";
# sub larger_of_fred_or_barney
# {
# if ($fred > $barney){
# $fred
# } else {
# $barney;
# }
# }
# $fred = 3;
# $barney = 4;
# print &larger_of_fred_or_barney;
# hello();
# sub hello
# {
# say "This is the hello function.";
# }
# print &max (20, 15);
# sub max
# {
# if ($_[0] > $_[1]) {
# $_[0];
# } else {
# $_[1];
# }
# }
#
# Last expression evaluated is return value:
# sub add_a_to_b
# {
# print "hey! I was invoked!\n";
# $a + $b;
# }
# $a = 3;
# $b =4;
# $c = &add_a_to_b;
# sub bigger_of_a_or_b
# {
# print "hey me too!";
# if ($a > $b) {$a} else {$b};
# }
# $t = &bigger_of_a_or_b;
# print "$t";
# $n = &max(12,11);
# sub max
# {
# if ($_[0] > $_[1]){$_[0]} else {$_[1]};
# }
# print "$n";
#======== Early Subroutine Exit Finder list item inside list
# my @names = qw(fred barney betty dino wilma);
# my $result = &which_element_is("wilma", @names);
# sub which_element_is
# {
# my($what, @array) = @_;
# foreach (0..$#array) { #indices of @array's elements
# if ($what eq $array[$_]) {return $_}
# }
# -1;
# }
# ======= print $result;
# Persistent But Local
use v5.10; #State only works when 5.10 version is specified
sub marine {
state $n = 0; #initial value
$n += 1;
print "Hello sailor number $n!\n";
}
print &marine;