-
Notifications
You must be signed in to change notification settings - Fork 0
/
notes_part_5.txt
81 lines (63 loc) · 2.38 KB
/
notes_part_5.txt
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
---------- Notes on Chapter 8 of Beginning Perl (Simon Cozens, 2000). ----------
-------------------------------- SUBROUTINES -----------------------------------
Subroutines don't return anything unless they are explicitly told to. Here's how
to define one:
sub name_of_sub {
# code goes here
}
Prototypes:
sub name_of_sub($$$) {
# takes 3 scalars as input
}
sub another_sub(\@\@) {
# takes 2 array references as input
}
To pre-declare a subroutine:
sub name_of_sub;
sub another_sub($$); # Prototyped to take two scalars
Or, as popular in Perl 4:
&name_of_sub;
To pass agruments into a subroutine:
name_of_sub(1,2,3);
name_of_sub(1...100);
We can pass any list into a subroutine as we would into a print statement. Each
element that gets passed in ends up in the special array @_
Example subroutine to sum all numbers passed in:
sub total {
my $total = 0;
$total += $_ for @_;
print "The total is $total\n";
}
---------------------------------- CACHING -------------------------------------
Store each answer generated from a set of parameters into a cache, usually hash.
If we need to calculate using those parameters again, we get the answer from
the cache rather than needing to recompute.
my %cache;
sub first_line {
my $filename = shift;
return $cache{$filename} if exists $cache{$filename}
open FILE, $filename or return "";
my $line = <FILE>;
$cache{filename} = $line;
return $line;
}
----------------------------------- SCOPE --------------------------------------
Global is the default if "my" is not specified.
By default, we are in the package called "main".
Thus, saying $x = 10 is really saying $main::x = 10.
We can create other packages to use the same variable name like so:
$Joe::name = "Joe Johnson";
$Naim::name = "Naim Ayat";
We can change the package we are currently working in by saying:
package Joe;
print $name; # this now returns "Joe Johnson"
------------------------- REFERENCES TO SUBROUTINES ----------------------------
Use ampersand to declare:
sub my_sub { print "Hello\n"; }
my $subReference = \&my_sub
Call like any other reference:
&{$subReference};
&{$subReference}(@parameters);
$subReference->();
$subReference->(@parameters);
________________________________________________________________________________