blib/lib/Object/New.pm

filter: Statementstmt 100% 18 / 18 Branchbran 100% 2 / 2 Conditioncond n/a MC/DCmcdc n/a Subroutinesub 100% 4 / 4 Podpod 100% 1 / 1 totaltotal 100% 25 / 25 CC 2 SCAR 3.4
CC
2
Cov
100%
new
2.0
BEGIN
1.0
BEGIN
1.0
CRAP 1.4
↑ Summary
1 package Object::New;
2  
3 4 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 4 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
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 4 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 4 ▶use Exporter 'import';
Subroutine BEGIN: called CC=1 SCAR=0.0
81 4 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 3 ▶sub new {
Subroutine new: called CC=2 SCAR=6.9
Pod documented
94 3 my $class = shift;
95 3 my $object = {};
96 3 bless $object, $class;
97 3 ▶ if ($object->can("init")) {
Branch: 1 branch
BranchTrueFalseTotal
if ($object->can("init")) 2 1 3
98 2 $object->init(@_);
99 }
100 3 return $object;
101 }
102  
103  
104 =head1 AUTHOR
105  
106 Alex Kalderimis, C<< <alex at gmail dot com> >>
107  
108 =head1 BUGS
109  
110 Please report any bugs or feature requests to C<bug-object-new at rt.cpan.org>, or through
111 the web interface at L<http://rt.cpan.org/NoAuth/ReportBug.html?Queue=Object-New>. 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<http://rt.cpan.org/NoAuth/Bugs.html?Dist=Object-New>
131  
132 =item * AnnoCPAN: Annotated CPAN documentation
133  
134 L<http://annocpan.org/dist/Object-New>
135  
136 =item * CPAN Ratings
137  
138 L<http://cpanratings.perl.org/d/Object-New>
139  
140 =item * Search CPAN
141  
142 L<http://search.cpan.org/dist/Object-New/>
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 4 1; # End of Object::New
↑ Summary