line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
1
|
|
|
|
|
|
|
package Object::New; |
2
|
|
|
|
|
|
|
|
3
|
3
|
|
|
3
|
|
68564
|
use warnings; |
|
3
|
|
|
|
|
8
|
|
|
3
|
|
|
|
|
226
|
|
4
|
3
|
|
|
3
|
|
17
|
use strict; |
|
3
|
|
|
|
|
6
|
|
|
3
|
|
|
|
|
197
|
|
5
|
|
|
|
|
|
|
|
6
|
|
|
|
|
|
|
=head1 NAME |
7
|
|
|
|
|
|
|
|
8
|
|
|
|
|
|
|
Object::New - A default constructor for standard objects |
9
|
|
|
|
|
|
|
|
10
|
|
|
|
|
|
|
=head1 VERSION |
11
|
|
|
|
|
|
|
|
12
|
|
|
|
|
|
|
Version 0.02 |
13
|
|
|
|
|
|
|
|
14
|
|
|
|
|
|
|
=cut |
15
|
|
|
|
|
|
|
|
16
|
|
|
|
|
|
|
our $VERSION = '0.03'; |
17
|
|
|
|
|
|
|
|
18
|
|
|
|
|
|
|
=head1 SYNOPSIS |
19
|
|
|
|
|
|
|
|
20
|
|
|
|
|
|
|
When you want Moose, there is Moose, but when you just want to |
21
|
|
|
|
|
|
|
write "standard" Perl5 OO code, there is a bit of boilerplate, |
22
|
|
|
|
|
|
|
and "->new" is one bit of it. This module just does just one thing: provide |
23
|
|
|
|
|
|
|
a sane default constructor you can use for standard Perl5 objects. |
24
|
|
|
|
|
|
|
(A standard object is here taken to be a blessed hash.) |
25
|
|
|
|
|
|
|
|
26
|
|
|
|
|
|
|
If you want anything else, there is lots out there: Moose, obviously, |
27
|
|
|
|
|
|
|
but also Object::Tiny, and Rose::Object and many others. This modules is |
28
|
|
|
|
|
|
|
simply an exercise in code reuse, and an attempt to avoid a common |
29
|
|
|
|
|
|
|
piece of boilerplate. |
30
|
|
|
|
|
|
|
|
31
|
|
|
|
|
|
|
|
32
|
|
|
|
|
|
|
=head1 THE GUTS |
33
|
|
|
|
|
|
|
|
34
|
|
|
|
|
|
|
The new routine in all its glory is: |
35
|
|
|
|
|
|
|
|
36
|
|
|
|
|
|
|
sub new { |
37
|
|
|
|
|
|
|
my $class = shift; |
38
|
|
|
|
|
|
|
my $object = {}; |
39
|
|
|
|
|
|
|
bless $object, $class; |
40
|
|
|
|
|
|
|
if $object->can("init") { |
41
|
|
|
|
|
|
|
$object->init(@_); |
42
|
|
|
|
|
|
|
} |
43
|
|
|
|
|
|
|
return $object; |
44
|
|
|
|
|
|
|
} |
45
|
|
|
|
|
|
|
|
46
|
|
|
|
|
|
|
So you get a blessed hash at the end. If your code has a init routine defined |
47
|
|
|
|
|
|
|
that an object in its namespace can access, it will call init as well, with the |
48
|
|
|
|
|
|
|
argument list passed to new. |
49
|
|
|
|
|
|
|
|
50
|
|
|
|
|
|
|
To customise object construction, all you have to do is define an init routine: |
51
|
|
|
|
|
|
|
|
52
|
|
|
|
|
|
|
package POW; |
53
|
|
|
|
|
|
|
|
54
|
|
|
|
|
|
|
use Object::New; |
55
|
|
|
|
|
|
|
use feature "say"; |
56
|
|
|
|
|
|
|
|
57
|
|
|
|
|
|
|
sub init { |
58
|
|
|
|
|
|
|
my $self = shift; |
59
|
|
|
|
|
|
|
my ($name, $rank, $serial_number) = @_; |
60
|
|
|
|
|
|
|
$self->set_name($name); |
61
|
|
|
|
|
|
|
$self->set_rank($rank); |
62
|
|
|
|
|
|
|
$self->set_serial_number($serial_number); |
63
|
|
|
|
|
|
|
} |
64
|
|
|
|
|
|
|
|
65
|
|
|
|
|
|
|
# attribute accessors defined here... |
66
|
|
|
|
|
|
|
|
67
|
|
|
|
|
|
|
sub interrogate { |
68
|
|
|
|
|
|
|
my $self = shift; |
69
|
|
|
|
|
|
|
say join(", ", $self->name, $self->rank, $self->serial_number); |
70
|
|
|
|
|
|
|
} |
71
|
|
|
|
|
|
|
|
72
|
|
|
|
|
|
|
=head1 EXPORT |
73
|
|
|
|
|
|
|
|
74
|
|
|
|
|
|
|
This module exports one routine by default: "new" |
75
|
|
|
|
|
|
|
|
76
|
|
|
|
|
|
|
If you don't want to import this, you don't want to be using this module. |
77
|
|
|
|
|
|
|
|
78
|
|
|
|
|
|
|
=cut |
79
|
|
|
|
|
|
|
|
80
|
3
|
|
|
3
|
|
15
|
use Exporter 'import'; |
|
3
|
|
|
|
|
10
|
|
|
3
|
|
|
|
|
1123
|
|
81
|
|
|
|
|
|
|
our @EXPORT = qw/new/; |
82
|
|
|
|
|
|
|
|
83
|
|
|
|
|
|
|
=head1 SUBROUTINES/METHODS |
84
|
|
|
|
|
|
|
|
85
|
|
|
|
|
|
|
=head2 new |
86
|
|
|
|
|
|
|
|
87
|
|
|
|
|
|
|
A default constructor. This returns a reference to a hash, blessed into its |
88
|
|
|
|
|
|
|
invoking class. If an init method is available, it is called with the argument |
89
|
|
|
|
|
|
|
list passed to the constructor. |
90
|
|
|
|
|
|
|
|
91
|
|
|
|
|
|
|
=cut |
92
|
|
|
|
|
|
|
|
93
|
|
|
|
|
|
|
sub new { |
94
|
3
|
|
|
3
|
1
|
1364
|
my $class = shift; |
95
|
3
|
|
|
|
|
8
|
my $object = {}; |
96
|
3
|
|
|
|
|
10
|
bless $object, $class; |
97
|
3
|
100
|
|
|
|
42
|
if ($object->can("init")) { |
98
|
2
|
|
|
|
|
11
|
$object->init(@_); |
99
|
|
|
|
|
|
|
} |
100
|
3
|
|
|
|
|
85
|
return $object; |
101
|
|
|
|
|
|
|
} |
102
|
|
|
|
|
|
|
|
103
|
|
|
|
|
|
|
|
104
|
|
|
|
|
|
|
=head1 AUTHOR |
105
|
|
|
|
|
|
|
|
106
|
|
|
|
|
|
|
Alex Kalderimis, C<< >> |
107
|
|
|
|
|
|
|
|
108
|
|
|
|
|
|
|
=head1 BUGS |
109
|
|
|
|
|
|
|
|
110
|
|
|
|
|
|
|
Please report any bugs or feature requests to C, or through |
111
|
|
|
|
|
|
|
the web interface at L. I will be notified, and then you'll |
112
|
|
|
|
|
|
|
automatically be notified of progress on your bug as I make changes. |
113
|
|
|
|
|
|
|
|
114
|
|
|
|
|
|
|
|
115
|
|
|
|
|
|
|
|
116
|
|
|
|
|
|
|
|
117
|
|
|
|
|
|
|
=head1 SUPPORT |
118
|
|
|
|
|
|
|
|
119
|
|
|
|
|
|
|
You can find documentation for this module with the perldoc command. |
120
|
|
|
|
|
|
|
|
121
|
|
|
|
|
|
|
perldoc Object::New |
122
|
|
|
|
|
|
|
|
123
|
|
|
|
|
|
|
|
124
|
|
|
|
|
|
|
You can also look for information at: |
125
|
|
|
|
|
|
|
|
126
|
|
|
|
|
|
|
=over 4 |
127
|
|
|
|
|
|
|
|
128
|
|
|
|
|
|
|
=item * RT: CPAN's request tracker |
129
|
|
|
|
|
|
|
|
130
|
|
|
|
|
|
|
L |
131
|
|
|
|
|
|
|
|
132
|
|
|
|
|
|
|
=item * AnnoCPAN: Annotated CPAN documentation |
133
|
|
|
|
|
|
|
|
134
|
|
|
|
|
|
|
L |
135
|
|
|
|
|
|
|
|
136
|
|
|
|
|
|
|
=item * CPAN Ratings |
137
|
|
|
|
|
|
|
|
138
|
|
|
|
|
|
|
L |
139
|
|
|
|
|
|
|
|
140
|
|
|
|
|
|
|
=item * Search CPAN |
141
|
|
|
|
|
|
|
|
142
|
|
|
|
|
|
|
L |
143
|
|
|
|
|
|
|
|
144
|
|
|
|
|
|
|
=back |
145
|
|
|
|
|
|
|
|
146
|
|
|
|
|
|
|
|
147
|
|
|
|
|
|
|
=head1 ACKNOWLEDGEMENTS |
148
|
|
|
|
|
|
|
|
149
|
|
|
|
|
|
|
|
150
|
|
|
|
|
|
|
=head1 LICENSE AND COPYRIGHT |
151
|
|
|
|
|
|
|
|
152
|
|
|
|
|
|
|
Copyright 2011 Alex Kalderimis. |
153
|
|
|
|
|
|
|
|
154
|
|
|
|
|
|
|
This program is free software; you can redistribute it and/or modify it |
155
|
|
|
|
|
|
|
under the terms of either: the GNU General Public License as published |
156
|
|
|
|
|
|
|
by the Free Software Foundation; or the Artistic License. |
157
|
|
|
|
|
|
|
|
158
|
|
|
|
|
|
|
See http://dev.perl.org/licenses/ for more information. |
159
|
|
|
|
|
|
|
|
160
|
|
|
|
|
|
|
|
161
|
|
|
|
|
|
|
=cut |
162
|
|
|
|
|
|
|
|
163
|
|
|
|
|
|
|
1; # End of Object::New |