forked from szabgab/perlmaven.com
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathobject-oriented-perl-using-moose.tt
158 lines (109 loc) · 4.26 KB
/
object-oriented-perl-using-moose.tt
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
149
150
151
152
153
154
155
156
=title Object Oriented Perl using Moose
=timestamp 2013-02-12T10:45:56
=indexes OOP, Moose, object oriented, class, object, instance, constructor, getter, setter, accessor
=status show
=books advanced_book
=author szabgab
=index 1
=archive 1
=feed 1
=comments 1
=social 1
=abstract start
In the next few articles we are going to learn how to write Object Oriented code in Perl.
We will start with some simple examples and extend them step by step.
We start using Moose but we'll also learn how to create classes in other ways.
=abstract end
<h2>A constructor with Moose</h2>
Let's start by writing a simple script that uses the Person <hl>class</hl>.
We don't do anything special yet, just load the module and call
the <hl>constructor</hl> to create an <hl>instance</hl>.
<code lang="perl">
use strict;
use warnings;
use v5.10;
use Person;
my $teacher = Person->new;
</code>
Save this in somedir/bin/app.pl
This should not be new to you as I am sure you have
already used other modules in a similar way. Our focus is
how the Person class was implemented:
<code lang="perl">
package Person;
use Moose;
1;
</code>
That's it.
This code is saved in somedir/lib/Person.pm.
All you need to do to create a <hl>class</hl> is to create
a <hl>package</hl> with the name of the class, add <hl>use Moose;</hl> to it,
end the file with a true value, and save it in a file with
the same name (case sensitive!) as the package, and with a .pm extension.
Loading Moose automatically sets up <hl>use strict</hl> and <hl>use warnings</hl>.
This is nice, but be careful you don't get so used to the convenience that you
forget them in non-Moose code.
Loading Moose also automatically adds a default constructor called <hl>new</hl>.
As a side note, it is not a requirement in Perl that the constructor will be called
new, but in most cases that's what the author chooses anyway.
<h2>Attributes and accessors</h2>
Having an empty class is not much fun. Let's go further in our use:
<code lang="perl">
use strict;
use warnings;
use v5.10;
use Person;
my $teacher = Person->new;
$teacher->name('Joe');
say $teacher->name;
</code>
In this code, after creating the <hl>object</hl>, we call the "name" <hl>method</hl>
with a string as a parameter; this sets the "name" <hl>attribute</hl> of the class
to be 'Joe'. Because this method sets the respective attribute it is also called a
<hl>setter</hl>.
Then we call the same method again, this time without any parameter. That will fetch
the value previously stored. Because this gets the value this is also called a
<hl>getter</hl>.
In our case the <hl>getter</hl> and the <hl>setter</hl> have the same name but
it isn't a requirement either.
In general <hl>getters</hl> and <hl>setters</hl> are called <hl>accessors</hl>.
The code implementing the new class is this:
<code lang="perl">
package Person;
use Moose;
has 'name' => (is => 'rw');
1;
</code>
The new part, <hl>has 'name' => (is => 'rw');</hl> says that
"The Person class <hl>has</hl> an attribute called <hl>'name'</hl>
which <hl>is</hl> <hl>r</hl>eadable and <hl>w</hl>riteable"
This automatically creates a method called "name" which becomes both a setter (for writing)
and a getter (for reading).
<h2>Try the code</h2>
In order to try this create a directory called "somedir", with a subdirectory called
"lib" inside it, and save the Person.pm file inside the "lib" subdirectory.
Also create a subdirectory called "bin" and save the script there called person.pl.
You should have
<code>
somedir/lib/Person.pm
somedir/bin/person.pl
</code>
Open your terminal (or cmd window on Windows), change the directory to be in "somedir" and
type <hl>perl -Ilib bin/person.pl</hl>
(On MS Windows you might need to use back-slashes: \ )
<h2>Constructor parameters</h2>
In the next script we pass a key-value pair to the constructor,
corresponding to the name of the attribute and its value.
<code lang="perl">
use strict;
use warnings;
use v5.10;
use Person;
my $teacher = Person->new( name => 'Joe' );
say $teacher->name;
</code>
This works too with the same module as we already have:
Using the constructor to set the initial value of an attribute
in this way works without making any changes to the Person module itself.
Moose automatically accepts every <hl>member</hl> (another name for attributes)
to be passed during construction.