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 require Class::XSAccessor;
107 Class::XSAccessor::newxs_accessor("${class}::${name}", $field, 0);
108 Class::XSAccessor::newxs_accessor("${class}::${alias}", $field, 0);
110 # XXX: is the alias accessor really necessary?
113 my $accessor = $self->$maker($group, $field);
114 my $alias_accessor = $self->$maker($group, $field);
116 *$full_name = Sub::Name::subname($full_name, $accessor);
117 #unless defined &{$class."\:\:$field"}
119 *$full_alias = Sub::Name::subname($full_alias, $alias_accessor);
120 #unless defined &{$class."\:\:$alias"}
126 =head2 mk_group_ro_accessors
130 =item Arguments: $group, @fieldspec
136 Creates a set of read only accessors in a given group. Identical to
137 L</mk_group_accessors> but accessors will throw an error if passed a value
138 rather than setting the value.
142 sub mk_group_ro_accessors {
143 my($self, $group, @fields) = @_;
145 $self->_mk_group_accessors('make_group_ro_accessor', $group, @fields);
148 =head2 mk_group_wo_accessors
152 =item Arguments: $group, @fieldspec
158 Creates a set of write only accessors in a given group. Identical to
159 L</mk_group_accessors> but accessors will throw an error if not passed a
160 value rather than getting the value.
164 sub mk_group_wo_accessors {
165 my($self, $group, @fields) = @_;
167 $self->_mk_group_accessors('make_group_wo_accessor', $group, @fields);
170 =head2 make_group_accessor
174 =item Arguments: $group, $field
176 Returns: $sub (\CODE)
180 Returns a single accessor in a given group; called by mk_group_accessors
181 for each entry in @fieldspec.
185 sub make_group_accessor {
186 my ($class, $group, $field) = @_;
188 my $set = "set_$group";
189 my $get = "get_$group";
191 # eval for faster fastiness
194 return shift->$set('$field', \@_);
197 return shift->$get('$field');
202 =head2 make_group_ro_accessor
206 =item Arguments: $group, $field
208 Returns: $sub (\CODE)
212 Returns a single read-only accessor in a given group; called by
213 mk_group_ro_accessors for each entry in @fieldspec.
217 sub make_group_ro_accessor {
218 my($class, $group, $field) = @_;
220 my $get = "get_$group";
224 my \$caller = caller;
225 Carp::croak(\"'\$caller' cannot alter the value of '$field' on \".
226 \"objects of class '$class'\");
229 return shift->$get('$field');
234 =head2 make_group_wo_accessor
238 =item Arguments: $group, $field
240 Returns: $sub (\CODE)
244 Returns a single write-only accessor in a given group; called by
245 mk_group_wo_accessors for each entry in @fieldspec.
249 sub make_group_wo_accessor {
250 my($class, $group, $field) = @_;
252 my $set = "set_$group";
256 my \$caller = caller;
257 Carp::croak(\"'\$caller' cannot access the value of '$field' on \".
258 \"objects of class '$class'\");
261 return shift->$set('$field', \@_);
270 =item Arguments: $field
276 Simple getter for hash-based objects which returns the value for the field
277 name passed as an argument.
282 return $_[0]->{$_[1]};
289 =item Arguments: $field, $new_value
295 Simple setter for hash-based objects which sets and then returns the value
296 for the field name passed as an argument.
301 return $_[0]->{$_[1]} = $_[2];
309 =item Arguments: $field
315 Simple getter for Classes and hash-based objects which returns the value for
316 the field name passed as an argument. This behaves much like
317 L<Class::Data::Accessor> where the field can be set in a base class,
318 inherited and changed in subclasses, and inherited and changed for object
326 if (Scalar::Util::blessed $_[0]) {
327 my $reftype = Scalar::Util::reftype $_[0];
330 if ($reftype eq 'HASH' && exists $_[0]->{$_[1]}) {
331 return $_[0]->{$_[1]};
332 } elsif ($reftype ne 'HASH') {
333 Carp::croak('Cannot get inherited value on an object instance that is not hash-based');
340 no warnings qw/uninitialized/;
341 return ${$class.'::__cag_'.$_[1]} if defined(${$class.'::__cag_'.$_[1]});
343 # we need to be smarter about recalculation, as @ISA (thus supers) can very well change in-flight
344 my $pkg_gen = mro::get_pkg_gen ($class);
345 if ( ${$class.'::__cag_pkg_gen'} != $pkg_gen ) {
346 @{$class.'::__cag_supers'} = $_[0]->get_super_paths;
347 ${$class.'::__cag_pkg_gen'} = $pkg_gen;
350 foreach (@{$class.'::__cag_supers'}) {
351 return ${$_.'::__cag_'.$_[1]} if defined(${$_.'::__cag_'.$_[1]});
361 =item Arguments: $field, $new_value
367 Simple setter for Classes and hash-based objects which sets and then returns
368 the value for the field name passed as an argument. When called on a hash-based
369 object it will set the appropriate hash key value. When called on a class, it
370 will set a class level variable.
372 B<Note:>: This method will die if you try to set an object variable on a non
378 if (Scalar::Util::blessed $_[0]) {
379 if (Scalar::Util::reftype $_[0] eq 'HASH') {
380 return $_[0]->{$_[1]} = $_[2];
382 Carp::croak('Cannot set inherited value on an object instance that is not hash-based');
387 return ${$_[0].'::__cag_'.$_[1]} = $_[2];
391 =head2 get_component_class
395 =item Arguments: $field
401 Gets the value of the specified component class.
403 __PACKAGE__->mk_group_accessors('component_class' => 'result_class');
405 $self->result_class->method();
408 $self->get_component_class('result_class')->method();
412 sub get_component_class {
413 return $_[0]->get_inherited($_[1]);
416 =head2 set_component_class
420 =item Arguments: $field, $class
426 Inherited accessor that automatically loads the specified class before setting
427 it. This method will die if the specified class could not be loaded.
429 __PACKAGE__->mk_group_accessors('component_class' => 'result_class');
430 __PACKAGE__->result_class('MyClass');
432 $self->result_class->method();
436 sub set_component_class {
439 if (Class::Inspector->installed($_[2]) && !Class::Inspector->loaded($_[2])) {
442 Carp::croak("Could not load $_[1] '$_[2]': ", $@) if $@;
446 return $_[0]->set_inherited($_[1], $_[2]);
449 =head2 get_super_paths
451 Returns a list of 'parent' or 'super' class names that the current class inherited from.
455 sub get_super_paths {
456 my $class = Scalar::Util::blessed $_[0] || $_[0];
458 return @{mro::get_linear_isa($class)};
465 You can speed up accessors of type 'simple' by installing L<Class::XSAccessor>.
466 Note however that the use of this module is disabled by default on Win32
467 systems, as it causes yet untracked segfaults. If you are a Win32 user, and
468 want to try this module with L<Class::XSAccessor>, set
469 C<$Class::Accessor::Grouped::hasXS> to a true value B<before> registering
470 your accessors (e.g. in a C<BEGIN> block)
474 Matt S. Trout <mst@shadowcatsystems.co.uk>
475 Christopher H. Laco <claco@chrislaco.com>
477 With contributions from:
479 Guillermo Roditi <groditi@cpan.org>
481 =head1 COPYRIGHT & LICENSE
483 Copyright (c) 2006-2009 Matt S. Trout <mst@shadowcatsystems.co.uk>
485 This program is free software; you can redistribute it and/or modify
486 it under the same terms as perl itself.