1 package Class::Accessor::Grouped;
5 use Class::Inspector ();
10 our $VERSION = '0.09003';
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
20 if (not defined $hasXS) {
23 if ($^O ne 'MSWin32') {
25 require Class::XSAccessor;
36 Class::Accessor::Grouped - Lets you build groups of accessors
42 This class lets you build groups of accessors that will call different
47 =head2 mk_group_accessors
51 =item Arguments: $group, @fieldspec
57 Creates a set of accessors in a given group.
59 $group is the name of the accessor group for the generated accessors; they
60 will call get_$group($field) on get and set_$group($field, $value) on set.
62 If you want to mimic Class::Accessor's mk_accessors $group has to be 'simple'
63 to tell Class::Accessor::Grouped to use its own get_simple and set_simple
66 @fieldspec is a list of field/accessor names; if a fieldspec is a scalar
67 this is used as both field and accessor name, if a listref it is expected to
68 be of the form [ $accessor, $field ].
72 sub mk_group_accessors {
73 my ($self, $group, @fields) = @_;
75 $self->_mk_group_accessors('make_group_accessor', $group, @fields);
82 no warnings 'redefine';
84 sub _mk_group_accessors {
85 my($self, $maker, $group, @fields) = @_;
86 my $class = Scalar::Util::blessed $self || $self;
88 # So we don't have to do lots of lookups inside the loop.
89 $maker = $self->can($maker) unless ref $maker;
93 foreach my $field (@fields) {
94 if( $field eq 'DESTROY' ) {
95 Carp::carp("Having a data accessor named DESTROY in ".
96 "'$class' is unwise.");
101 ($name, $field) = @$field if ref $field;
103 my $alias = "_${name}_accessor";
104 my $full_name = join('::', $class, $name);
105 my $full_alias = join('::', $class, $alias);
106 if ( $hasXS && $group eq 'simple' ) {
107 require Class::XSAccessor;
108 Class::XSAccessor->import({
118 my $accessor = $self->$maker($group, $field);
119 my $alias_accessor = $self->$maker($group, $field);
121 *$full_name = Sub::Name::subname($full_name, $accessor);
122 #unless defined &{$class."\:\:$field"}
124 *$full_alias = Sub::Name::subname($full_alias, $alias_accessor);
125 #unless defined &{$class."\:\:$alias"}
131 =head2 mk_group_ro_accessors
135 =item Arguments: $group, @fieldspec
141 Creates a set of read only accessors in a given group. Identical to
142 L</mk_group_accessors> but accessors will throw an error if passed a value
143 rather than setting the value.
147 sub mk_group_ro_accessors {
148 my($self, $group, @fields) = @_;
150 $self->_mk_group_accessors('make_group_ro_accessor', $group, @fields);
153 =head2 mk_group_wo_accessors
157 =item Arguments: $group, @fieldspec
163 Creates a set of write only accessors in a given group. Identical to
164 L</mk_group_accessors> but accessors will throw an error if not passed a
165 value rather than getting the value.
169 sub mk_group_wo_accessors {
170 my($self, $group, @fields) = @_;
172 $self->_mk_group_accessors('make_group_wo_accessor', $group, @fields);
175 =head2 make_group_accessor
179 =item Arguments: $group, $field
181 Returns: $sub (\CODE)
185 Returns a single accessor in a given group; called by mk_group_accessors
186 for each entry in @fieldspec.
190 sub make_group_accessor {
191 my ($class, $group, $field) = @_;
193 my $set = "set_$group";
194 my $get = "get_$group";
196 # eval for faster fastiness
199 return shift->$set('$field', \@_);
202 return shift->$get('$field');
207 =head2 make_group_ro_accessor
211 =item Arguments: $group, $field
213 Returns: $sub (\CODE)
217 Returns a single read-only accessor in a given group; called by
218 mk_group_ro_accessors for each entry in @fieldspec.
222 sub make_group_ro_accessor {
223 my($class, $group, $field) = @_;
225 my $get = "get_$group";
229 my \$caller = caller;
230 Carp::croak(\"'\$caller' cannot alter the value of '$field' on \".
231 \"objects of class '$class'\");
234 return shift->$get('$field');
239 =head2 make_group_wo_accessor
243 =item Arguments: $group, $field
245 Returns: $sub (\CODE)
249 Returns a single write-only accessor in a given group; called by
250 mk_group_wo_accessors for each entry in @fieldspec.
254 sub make_group_wo_accessor {
255 my($class, $group, $field) = @_;
257 my $set = "set_$group";
261 my \$caller = caller;
262 Carp::croak(\"'\$caller' cannot access the value of '$field' on \".
263 \"objects of class '$class'\");
266 return shift->$set('$field', \@_);
275 =item Arguments: $field
281 Simple getter for hash-based objects which returns the value for the field
282 name passed as an argument.
287 return $_[0]->{$_[1]};
294 =item Arguments: $field, $new_value
300 Simple setter for hash-based objects which sets and then returns the value
301 for the field name passed as an argument.
306 return $_[0]->{$_[1]} = $_[2];
314 =item Arguments: $field
320 Simple getter for Classes and hash-based objects which returns the value for
321 the field name passed as an argument. This behaves much like
322 L<Class::Data::Accessor> where the field can be set in a base class,
323 inherited and changed in subclasses, and inherited and changed for object
331 if (Scalar::Util::blessed $_[0]) {
332 my $reftype = Scalar::Util::reftype $_[0];
335 if ($reftype eq 'HASH' && exists $_[0]->{$_[1]}) {
336 return $_[0]->{$_[1]};
337 } elsif ($reftype ne 'HASH') {
338 Carp::croak('Cannot get inherited value on an object instance that is not hash-based');
345 no warnings qw/uninitialized/;
346 return ${$class.'::__cag_'.$_[1]} if defined(${$class.'::__cag_'.$_[1]});
348 # we need to be smarter about recalculation, as @ISA (thus supers) can very well change in-flight
349 my $pkg_gen = mro::get_pkg_gen ($class);
350 if ( ${$class.'::__cag_pkg_gen'} != $pkg_gen ) {
351 @{$class.'::__cag_supers'} = $_[0]->get_super_paths;
352 ${$class.'::__cag_pkg_gen'} = $pkg_gen;
355 foreach (@{$class.'::__cag_supers'}) {
356 return ${$_.'::__cag_'.$_[1]} if defined(${$_.'::__cag_'.$_[1]});
366 =item Arguments: $field, $new_value
372 Simple setter for Classes and hash-based objects which sets and then returns
373 the value for the field name passed as an argument. When called on a hash-based
374 object it will set the appropriate hash key value. When called on a class, it
375 will set a class level variable.
377 B<Note:>: This method will die if you try to set an object variable on a non
383 if (Scalar::Util::blessed $_[0]) {
384 if (Scalar::Util::reftype $_[0] eq 'HASH') {
385 return $_[0]->{$_[1]} = $_[2];
387 Carp::croak('Cannot set inherited value on an object instance that is not hash-based');
392 return ${$_[0].'::__cag_'.$_[1]} = $_[2];
396 =head2 get_component_class
400 =item Arguments: $field
406 Gets the value of the specified component class.
408 __PACKAGE__->mk_group_accessors('component_class' => 'result_class');
410 $self->result_class->method();
413 $self->get_component_class('result_class')->method();
417 sub get_component_class {
418 return $_[0]->get_inherited($_[1]);
421 =head2 set_component_class
425 =item Arguments: $field, $class
431 Inherited accessor that automatically loads the specified class before setting
432 it. This method will die if the specified class could not be loaded.
434 __PACKAGE__->mk_group_accessors('component_class' => 'result_class');
435 __PACKAGE__->result_class('MyClass');
437 $self->result_class->method();
441 sub set_component_class {
444 if (Class::Inspector->installed($_[2]) && !Class::Inspector->loaded($_[2])) {
447 Carp::croak("Could not load $_[1] '$_[2]': ", $@) if $@;
451 return $_[0]->set_inherited($_[1], $_[2]);
454 =head2 get_super_paths
456 Returns a list of 'parent' or 'super' class names that the current class inherited from.
460 sub get_super_paths {
461 my $class = Scalar::Util::blessed $_[0] || $_[0];
463 return @{mro::get_linear_isa($class)};
470 You can speed up accessors of type 'simple' by installing L<Class::XSAccessor>.
471 Note however that the use of this module is disabled by default on Win32
472 systems, as it causes yet unresolved segfaults. If you are a Win32 user, and
473 want to try this module with L<Class::XSAccessor>, set
474 C<$Class::Accessor::Grouped::hasXS> to a true value B<before> registering
475 your accessors (e.g. in a C<BEGIN> block)
479 Matt S. Trout <mst@shadowcatsystems.co.uk>
480 Christopher H. Laco <claco@chrislaco.com>
482 With contributions from:
484 Guillermo Roditi <groditi@cpan.org>
486 =head1 COPYRIGHT & LICENSE
488 Copyright (c) 2006-2010 Matt S. Trout <mst@shadowcatsystems.co.uk>
490 This program is free software; you can redistribute it and/or modify
491 it under the same terms as perl itself.