File Coverage

blib/lib/accessors/rw/explicit.pm
Criterion Covered Total %
statement 52 52 100.0
branch 10 16 62.5
condition 3 3 100.0
subroutine 14 14 100.0
pod 4 4 100.0
total 83 89 93.2


line stmt bran cond sub pod time code
1             package accessors::rw::explicit;
2              
3 2     2   18468 use warnings::register;
  2         3  
  2         262  
4 2     2   10 use strict;
  2         4  
  2         53  
5 2     2   10 use Carp qw/confess carp/;
  2         11  
  2         116  
6              
7 2     2   9 use base 'accessors';
  2         3  
  2         1344  
8 2     2   1801 use constant style => "explicit";
  2         3  
  2         108  
9 2     2   8 use constant ExportLevel => 1;
  2         3  
  2         638  
10              
11             =head1 NAME
12              
13             accessors::rw::explicit - RW object attribute accessors, with explicit semantics
14              
15             =head1 VERSION
16              
17             Version 0.01
18              
19             =cut
20              
21             our $VERSION = '0.02';
22              
23             =head1 SYNOPSIS
24              
25              
26             package Foo;
27             use accessors::rw::explicit qw(foo bar baz);
28              
29             my $obj = bless {}, 'Foo';
30              
31             # always return the current value, even on set:
32             $obj->set_foo( 'hello ' ) if $obj->bar( 'world' ) eq 'world';
33              
34             print $obj->foo, $obj->bar, $obj->set_baz( "!\n" );
35             ...
36              
37             =head1 DESCRIPTION
38              
39             ceci n'est pas un Moose
40              
41             The purpose of this module is to provide very basic
42             object instance attribute accessors for basic perl5
43             blessed hash-reference objects.
44              
45             That is a mouthful. Essentially is is just an exercise in
46             codifying convention, and sensible code reuse. It is not
47             an attempt at a complete object system, with flexible
48             attribute definitions. If you want that, you want Moose,
49             end of.
50              
51             This module will set up attribute accessors for you though,
52             and allow you to specify a prefix on your getters and setters. It
53             denotes itself as explicit, as it defaults to different names
54             for the getters and setters, namely prepending "set_" to setters.
55              
56             so for foo you would get:
57              
58             $obj->set_foo( $foo );
59              
60             and
61              
62             my $foo = $obj->foo;
63              
64             These prefixes can be changed by providing options on import.
65              
66             eg:
67              
68             use accessors::rw::explicit ({get_prefix => 'you_tell_me_', set_prefix => 'I_tell_you_'}, 'foo');
69              
70             would provide:
71              
72             $obj->I_tell_you_foo( $foo )
73              
74             and
75              
76             my $foo = $obj->you_tell_me_foo();
77              
78             =head1 METHODS
79              
80             =head2 GetPrefix
81              
82             returns the prefix prepended to getters. Defaults to the empty string.
83              
84             =cut
85              
86             my $get_prefix = "";
87 9     9 1 16 sub GetPrefix { $get_prefix };
88              
89             =head2 SetPrefix
90              
91             returns the prefix prepended to setters. Defaults to "set_".
92              
93             =cut
94              
95             my $set_prefix = "set_";
96 9     9 1 15 sub SetPrefix { $set_prefix };
97              
98             =head2 import
99              
100             As with other accessors modules, this takes a list of the attributes
101             you want setting up:
102              
103             use accessors::rw::explicit qw(foo bar baz);
104              
105             It can also change the default get and set prefixes by providing an optional
106             options hash:
107              
108             use accessors::rw::explicit ({get_prefix => 'get_'}, qw/foo bar baz/);
109              
110             The above would produce accessors that conform to the Perl Best Practice book's
111             recommendations.
112              
113             =cut
114              
115             sub import {
116 4     4   26 my $class = shift;
117 4 100 100     26 if ($_[0] && ref $_[0] eq 'HASH') {
118 2         3 my $opts = shift;
119 2 50       4 if (exists $opts->{get_prefix}) {
120 2         4 $get_prefix = $opts->{get_prefix};
121             }
122 2 100       5 if (exists $opts->{set_prefix}) {
123 1         1 $set_prefix = $opts->{set_prefix};
124             }
125             }
126 4         27 $class->SUPER::import(@_);
127             }
128              
129             =head2 create_accessors_for
130              
131             Creates a get accessor of the form
132             GetPrefix + AttributeName
133             and a set accessor of the form
134             SetPrefix + AttributeName
135              
136             See import for how to define the prefixes and the attribute names.
137              
138             This overrides a method in the accessors.pm package,
139             and should never need to be called directly.
140              
141             =cut
142             sub create_accessors_for {
143 3     3 1 29 my $class = shift;
144 3         4 my $callpkg = shift;
145              
146 3 50       8 warn( 'creating ' . $class->style . ' accessors( ',
147             join(' ',@_)," ) in pkg '$callpkg'" ) if $class->Debug;
148              
149 3         12 foreach my $property (@_) {
150 9 50       22 confess( "can't create accessors in $callpkg - '$property' is not a valid name!" )
151             unless $class->isa_valid_name( $property );
152 9 50       140 warn "Processing $property" if $class->Debug;
153 9         30 $class->create_explicit_accessors( $callpkg, $property );
154             }
155              
156 3         159 return $class;
157             }
158              
159             =head2 create_explicit_accessors
160              
161             The routine that actually creates the accessors. The body of a getter looks like:
162              
163             my $getter = sub {
164             return $_[0]->{$property};
165             }
166              
167             and a setter is defined as:
168              
169             my $setter = sub {
170             $_[0]->{$property} = $_[1];
171             return $_[0]->{$property};
172             }
173              
174             Where $property is defined to be
175              
176             "-" . $attribute_name.
177              
178             =cut
179              
180             sub create_explicit_accessors {
181 9     9 1 16 my ($class, $pkg, $property) = @_;
182 9         15 my $get_accessor = $pkg . '::' . $class->GetPrefix . $property;
183 9         18 my $set_accessor = $pkg . '::' . $class->SetPrefix . $property;
184 9         11 $property = "-$property";
185 2     2   9 no strict 'refs';
  2         3  
  2         358  
186 9 50       19 warn( "creating " . $class->style . " accessor: $get_accessor\n" ) if
187             $class->Debug;
188 9         42 *{$get_accessor} = sub {
189 9     9   36 return $_[0]->{$property};
190 9         40 };
191 9 50       22 warn( "creating " . $class->style . " accessor: $set_accessor\n" ) if
192             $class->Debug;
193 9         52 *{$set_accessor} = sub {
194 9     9   858 $_[0]->{$property} = $_[1];
195 9         17 return $_[0]->{$property};
196             }
197 9         32 }
198              
199              
200             =head1 AUTHOR
201              
202             Alex Kalderimis, C<< >>
203              
204             =head1 BUGS
205              
206             Please report any bugs or feature requests to C, or through
207             the web interface at L. I will be notified, and then you'll
208             automatically be notified of progress on your bug as I make changes.
209              
210             =head1 SUPPORT
211              
212             You can find documentation for this module with the perldoc command.
213              
214             perldoc accessors::rw::explicit
215              
216              
217             You can also look for information at:
218              
219             =over 4
220              
221             =item * RT: CPAN's request tracker
222              
223             L
224              
225             =item * AnnoCPAN: Annotated CPAN documentation
226              
227             L
228              
229             =item * CPAN Ratings
230              
231             L
232              
233             =item * Search CPAN
234              
235             L
236              
237             =back
238              
239              
240             =head1 ACKNOWLEDGEMENTS
241              
242             Steve Purkis for writing accessors.pm L
243              
244             =head1 LICENSE AND COPYRIGHT
245              
246             Copyright 2011 Alex Kalderimis.
247              
248             This program is free software; you can redistribute it and/or modify it
249             under the terms of either: the GNU General Public License as published
250             by the Free Software Foundation; or the Artistic License.
251              
252             See http://dev.perl.org/licenses/ for more information.
253              
254             =cut
255              
256             1; # End of accessors::rw::explicit