1 package Class::Accessor::Grouped;
5 use Class::Inspector ();
10 our $VERSION = '0.09002';
11 $VERSION = eval $VERSION;
13 # Class::XSAccessor is segfaulting on win32, so be careful
14 # Win32 users can set $hasXS to try to use it anyway
18 if (not defined $hasXS) {
21 if ($^O ne 'MSWin32') {
23 require Class::XSAccessor;
34 Class::Accessor::Grouped - Lets you build groups of accessors
40 This class lets you build groups of accessors that will call different
45 =head2 mk_group_accessors
49 =item Arguments: $group, @fieldspec
55 Creates a set of accessors in a given group.
57 $group is the name of the accessor group for the generated accessors; they
58 will call get_$group($field) on get and set_$group($field, $value) on set.
60 If you want to mimic Class::Accessor's mk_accessors $group has to be 'simple'
61 to tell Class::Accessor::Grouped to use its own get_simple and set_simple
64 @fieldspec is a list of field/accessor names; if a fieldspec is a scalar
65 this is used as both field and accessor name, if a listref it is expected to
66 be of the form [ $accessor, $field ].
70 sub mk_group_accessors {
71 my ($self, $group, @fields) = @_;
73 $self->_mk_group_accessors('make_group_accessor', $group, @fields);
80 no warnings 'redefine';
82 sub _mk_group_accessors {
83 my($self, $maker, $group, @fields) = @_;
84 my $class = Scalar::Util::blessed $self || $self;
86 # So we don't have to do lots of lookups inside the loop.
87 $maker = $self->can($maker) unless ref $maker;
91 foreach my $field (@fields) {
92 if( $field eq 'DESTROY' ) {
93 Carp::carp("Having a data accessor named DESTROY in ".
94 "'$class' is unwise.");
99 ($name, $field) = @$field if ref $field;
101 my $alias = "_${name}_accessor";
102 my $full_name = join('::', $class, $name);
103 my $full_alias = join('::', $class, $alias);
105 if ( $hasXS && $group eq 'simple' ) {
106 Class::XSAccessor::newxs_accessor("${class}::${name}", $field, 0);
107 Class::XSAccessor::newxs_accessor("${class}::${alias}", $field, 0);
109 # XXX: is the alias accessor really necessary?
112 my $accessor = $self->$maker($group, $field);
113 my $alias_accessor = $self->$maker($group, $field);
115 *$full_name = Sub::Name::subname($full_name, $accessor);
116 #unless defined &{$class."\:\:$field"}
118 *$full_alias = Sub::Name::subname($full_alias, $alias_accessor);
119 #unless defined &{$class."\:\:$alias"}
125 =head2 mk_group_ro_accessors
129 =item Arguments: $group, @fieldspec
135 Creates a set of read only accessors in a given group. Identical to
136 L</mk_group_accessors> but accessors will throw an error if passed a value
137 rather than setting the value.
141 sub mk_group_ro_accessors {
142 my($self, $group, @fields) = @_;
144 $self->_mk_group_accessors('make_group_ro_accessor', $group, @fields);
147 =head2 mk_group_wo_accessors
151 =item Arguments: $group, @fieldspec
157 Creates a set of write only accessors in a given group. Identical to
158 L</mk_group_accessors> but accessors will throw an error if not passed a
159 value rather than getting the value.
163 sub mk_group_wo_accessors {
164 my($self, $group, @fields) = @_;
166 $self->_mk_group_accessors('make_group_wo_accessor', $group, @fields);
169 =head2 make_group_accessor
173 =item Arguments: $group, $field
175 Returns: $sub (\CODE)
179 Returns a single accessor in a given group; called by mk_group_accessors
180 for each entry in @fieldspec.
184 sub make_group_accessor {
185 my ($class, $group, $field) = @_;
187 my $set = "set_$group";
188 my $get = "get_$group";
190 # eval for faster fastiness
193 return shift->$set('$field', \@_);
196 return shift->$get('$field');
201 =head2 make_group_ro_accessor
205 =item Arguments: $group, $field
207 Returns: $sub (\CODE)
211 Returns a single read-only accessor in a given group; called by
212 mk_group_ro_accessors for each entry in @fieldspec.
216 sub make_group_ro_accessor {
217 my($class, $group, $field) = @_;
219 my $get = "get_$group";
223 my \$caller = caller;
224 Carp::croak(\"'\$caller' cannot alter the value of '$field' on \".
225 \"objects of class '$class'\");
228 return shift->$get('$field');
233 =head2 make_group_wo_accessor
237 =item Arguments: $group, $field
239 Returns: $sub (\CODE)
243 Returns a single write-only accessor in a given group; called by
244 mk_group_wo_accessors for each entry in @fieldspec.
248 sub make_group_wo_accessor {
249 my($class, $group, $field) = @_;
251 my $set = "set_$group";
255 my \$caller = caller;
256 Carp::croak(\"'\$caller' cannot access the value of '$field' on \".
257 \"objects of class '$class'\");
260 return shift->$set('$field', \@_);
269 =item Arguments: $field
275 Simple getter for hash-based objects which returns the value for the field
276 name passed as an argument.
281 return $_[0]->{$_[1]};
288 =item Arguments: $field, $new_value
294 Simple setter for hash-based objects which sets and then returns the value
295 for the field name passed as an argument.
300 return $_[0]->{$_[1]} = $_[2];
308 =item Arguments: $field
314 Simple getter for Classes and hash-based objects which returns the value for
315 the field name passed as an argument. This behaves much like
316 L<Class::Data::Accessor> where the field can be set in a base class,
317 inherited and changed in subclasses, and inherited and changed for object
325 if (Scalar::Util::blessed $_[0]) {
326 my $reftype = Scalar::Util::reftype $_[0];
329 if ($reftype eq 'HASH' && exists $_[0]->{$_[1]}) {
330 return $_[0]->{$_[1]};
331 } elsif ($reftype ne 'HASH') {
332 Carp::croak('Cannot get inherited value on an object instance that is not hash-based');
339 no warnings qw/uninitialized/;
340 return ${$class.'::__cag_'.$_[1]} if defined(${$class.'::__cag_'.$_[1]});
342 # we need to be smarter about recalculation, as @ISA (thus supers) can very well change in-flight
343 my $pkg_gen = mro::get_pkg_gen ($class);
344 if ( ${$class.'::__cag_pkg_gen'} != $pkg_gen ) {
345 @{$class.'::__cag_supers'} = $_[0]->get_super_paths;
346 ${$class.'::__cag_pkg_gen'} = $pkg_gen;
349 foreach (@{$class.'::__cag_supers'}) {
350 return ${$_.'::__cag_'.$_[1]} if defined(${$_.'::__cag_'.$_[1]});
360 =item Arguments: $field, $new_value
366 Simple setter for Classes and hash-based objects which sets and then returns
367 the value for the field name passed as an argument. When called on a hash-based
368 object it will set the appropriate hash key value. When called on a class, it
369 will set a class level variable.
371 B<Note:>: This method will die if you try to set an object variable on a non
377 if (Scalar::Util::blessed $_[0]) {
378 if (Scalar::Util::reftype $_[0] eq 'HASH') {
379 return $_[0]->{$_[1]} = $_[2];
381 Carp::croak('Cannot set inherited value on an object instance that is not hash-based');
386 return ${$_[0].'::__cag_'.$_[1]} = $_[2];
390 =head2 get_component_class
394 =item Arguments: $field
400 Gets the value of the specified component class.
402 __PACKAGE__->mk_group_accessors('component_class' => 'result_class');
404 $self->result_class->method();
407 $self->get_component_class('result_class')->method();
411 sub get_component_class {
412 return $_[0]->get_inherited($_[1]);
415 =head2 set_component_class
419 =item Arguments: $field, $class
425 Inherited accessor that automatically loads the specified class before setting
426 it. This method will die if the specified class could not be loaded.
428 __PACKAGE__->mk_group_accessors('component_class' => 'result_class');
429 __PACKAGE__->result_class('MyClass');
431 $self->result_class->method();
435 sub set_component_class {
438 if (Class::Inspector->installed($_[2]) && !Class::Inspector->loaded($_[2])) {
441 Carp::croak("Could not load $_[1] '$_[2]': ", $@) if $@;
445 return $_[0]->set_inherited($_[1], $_[2]);
448 =head2 get_super_paths
450 Returns a list of 'parent' or 'super' class names that the current class inherited from.
454 sub get_super_paths {
455 my $class = Scalar::Util::blessed $_[0] || $_[0];
457 return @{mro::get_linear_isa($class)};
464 You can speed up accessors of type 'simple' by installing L<Class::XSAccessor>.
465 Note however that the use of this module is disabled by default on Win32
466 systems, as it causes yet untracked segfaults. If you are a Win32 user, and
467 want to try this module with L<Class::XSAccessor>, set
468 C<$Class::Accessor::Grouped::hasXS> to a true value B<before> registering
469 your accessors (e.g. in a C<BEGIN> block)
473 Matt S. Trout <mst@shadowcatsystems.co.uk>
474 Christopher H. Laco <claco@chrislaco.com>
476 With contributions from:
478 Guillermo Roditi <groditi@cpan.org>
480 =head1 COPYRIGHT & LICENSE
482 Copyright (c) 2006-2009 Matt S. Trout <mst@shadowcatsystems.co.uk>
484 This program is free software; you can redistribute it and/or modify
485 it under the same terms as perl itself.