1 package Class::Accessor::Grouped;
9 our $VERSION = '0.09006';
10 $VERSION = eval $VERSION;
12 # when changing minimum version don't forget to adjust L</PERFROMANCE> as well
13 our $__minimum_xsa_version = '1.06';
16 # the unless defined is here so that we can override the value
17 # before require/use, *regardless* of the state of $ENV{CAG_USE_XS}
18 $USE_XS = $ENV{CAG_USE_XS}
19 unless defined $USE_XS;
21 my ($xsa_loaded, $xsa_autodetected);
24 return if $xsa_loaded++;
25 require Class::XSAccessor;
26 Class::XSAccessor->VERSION($__minimum_xsa_version);
30 if (defined $USE_XS) {
31 $load_xsa->() if ($USE_XS && ! $xsa_loaded);
35 $xsa_autodetected = 1;
38 # Class::XSAccessor is segfaulting on win32, in some
39 # esoteric heavily-threaded scenarios
40 # Win32 users can set $USE_XS/CAG_USE_XS to try to use it anyway
41 if ($^O ne 'MSWin32') {
43 eval { $load_xsa->(); $USE_XS = 1 };
49 my $maker_type_map = {
52 cag => 'make_group_accessor',
56 cag => 'make_group_ro_accessor',
60 cag => 'make_group_wo_accessor',
64 # When installing an XSA simple accessor, we need to make sure we are not
65 # short-circuiting a (compile or runtime) get_simple/set_simple override.
66 # What we do here is install a lazy first-access check, which will decide
67 # the ultimate coderef being placed in the accessor slot
69 my $no_xsa_classes_warned;
70 my $add_xs_accessor = sub {
71 my ($class, $group, $field, $name, $type) = @_;
73 Class::XSAccessor->import({
76 $maker_type_map->{$type}{xsa} => {
81 my $xs_cref = $class->can($name);
84 my $cag_method = $maker_type_map->{$type}{cag};
86 $class->$cag_method ($group, $field, $name, $type);
89 # can't use pkg_gen to track this stuff, as it doesn't
90 # detect superclass mucking
91 my $original_getter = __PACKAGE__->can ("get_$group");
92 my $original_setter = __PACKAGE__->can ("set_$group");
96 my $current_class = (ref $self) || $self;
100 $current_class->can("get_$group") == $original_getter
102 $current_class->can("set_$group") == $original_setter
104 # nothing has changed, might as well use the XS crefs
105 # (if one changes methods that far into runtime - look pieces!)
106 $final_cref = $xs_cref;
109 $final_cref = $pp_cref;
110 if ($USE_XS and ! $xsa_autodetected and ! $no_xsa_classes_warned->{$current_class}++) {
111 warn 'Explicitly requested use of Class::XSAccessor disabled for objects of class'
112 . " '$current_class' due to an overriden get_$group and/or set_$group\n";
116 my $fq_meth = "${current_class}::${name}";
119 no warnings qw/redefine/;
120 *$fq_meth = Sub::Name::subname($fq_meth, $final_cref);
128 Class::Accessor::Grouped - Lets you build groups of accessors
134 This class lets you build groups of accessors that will call different
139 =head2 mk_group_accessors
143 =item Arguments: $group, @fieldspec
149 Creates a set of accessors in a given group.
151 $group is the name of the accessor group for the generated accessors; they
152 will call get_$group($field) on get and set_$group($field, $value) on set.
154 If you want to mimic Class::Accessor's mk_accessors $group has to be 'simple'
155 to tell Class::Accessor::Grouped to use its own get_simple and set_simple
158 @fieldspec is a list of field/accessor names; if a fieldspec is a scalar
159 this is used as both field and accessor name, if a listref it is expected to
160 be of the form [ $accessor, $field ].
164 sub mk_group_accessors {
165 my ($self, $group, @fields) = @_;
167 $self->_mk_group_accessors('make_group_accessor', $group, @fields);
174 no warnings 'redefine';
176 sub _mk_group_accessors {
177 my($self, $maker, $group, @fields) = @_;
178 my $class = Scalar::Util::blessed $self || $self;
180 # So we don't have to do lots of lookups inside the loop.
181 $maker = $self->can($maker) unless ref $maker;
184 if( $_ eq 'DESTROY' ) {
185 Carp::carp("Having a data accessor named DESTROY in ".
186 "'$class' is unwise.");
189 my ($name, $field) = (ref $_)
194 my $alias = "_${name}_accessor";
196 for my $meth ($name, $alias) {
198 # the maker may elect to not return anything, meaning it already
199 # installed the coderef for us
200 my $cref = $self->$maker($group, $field, $meth)
203 my $fq_meth = join('::', $class, $meth);
205 *$fq_meth = Sub::Name::subname($fq_meth, $cref);
206 #unless defined &{$class."\:\:$field"}
212 =head2 mk_group_ro_accessors
216 =item Arguments: $group, @fieldspec
222 Creates a set of read only accessors in a given group. Identical to
223 L</mk_group_accessors> but accessors will throw an error if passed a value
224 rather than setting the value.
228 sub mk_group_ro_accessors {
229 my($self, $group, @fields) = @_;
231 $self->_mk_group_accessors('make_group_ro_accessor', $group, @fields);
234 =head2 mk_group_wo_accessors
238 =item Arguments: $group, @fieldspec
244 Creates a set of write only accessors in a given group. Identical to
245 L</mk_group_accessors> but accessors will throw an error if not passed a
246 value rather than getting the value.
250 sub mk_group_wo_accessors {
251 my($self, $group, @fields) = @_;
253 $self->_mk_group_accessors('make_group_wo_accessor', $group, @fields);
256 =head2 make_group_accessor
260 =item Arguments: $group, $field, $method
262 Returns: \&accessor_coderef ?
266 Called by mk_group_accessors for each entry in @fieldspec. Either returns
267 a coderef which will be installed at C<&__PACKAGE__::$method>, or returns
268 C<undef> if it elects to install the coderef on its own.
272 sub make_group_accessor {
273 my ($class, $group, $field, $name) = @_;
275 if ( $group eq 'simple' && $use_xs->() ) {
276 return $add_xs_accessor->(@_, 'rw');
279 my $set = "set_$group";
280 my $get = "get_$group";
284 # eval for faster fastiness
285 my $code = eval "sub {
287 return shift->$set('$field', \@_);
290 return shift->$get('$field');
293 Carp::croak $@ if $@;
298 =head2 make_group_ro_accessor
302 =item Arguments: $group, $field, $method
304 Returns: \&accessor_coderef ?
308 Called by mk_group_ro_accessors for each entry in @fieldspec. Either returns
309 a coderef which will be installed at C<&__PACKAGE__::$method>, or returns
310 C<undef> if it elects to install the coderef on its own.
314 sub make_group_ro_accessor {
315 my($class, $group, $field, $name) = @_;
317 if ( $group eq 'simple' && $use_xs->() ) {
318 return $add_xs_accessor->(@_, 'ro');
321 my $get = "get_$group";
325 my $code = eval "sub {
327 my \$caller = caller;
328 Carp::croak(\"'\$caller' cannot alter the value of '$field' on \".
329 \"objects of class '$class'\");
332 return shift->$get('$field');
335 Carp::croak $@ if $@;
340 =head2 make_group_wo_accessor
344 =item Arguments: $group, $field, $method
346 Returns: \&accessor_coderef ?
350 Called by mk_group_wo_accessors for each entry in @fieldspec. Either returns
351 a coderef which will be installed at C<&__PACKAGE__::$method>, or returns
352 C<undef> if it elects to install the coderef on its own.
356 sub make_group_wo_accessor {
357 my($class, $group, $field, $name) = @_;
359 if ( $group eq 'simple' && $use_xs->() ) {
360 return $add_xs_accessor->(@_, 'wo')
363 my $set = "set_$group";
367 my $code = eval "sub {
369 my \$caller = caller;
370 Carp::croak(\"'\$caller' cannot access the value of '$field' on \".
371 \"objects of class '$class'\");
374 return shift->$set('$field', \@_);
377 Carp::croak $@ if $@;
386 =item Arguments: $field
392 Simple getter for hash-based objects which returns the value for the field
393 name passed as an argument.
398 return $_[0]->{$_[1]};
405 =item Arguments: $field, $new_value
411 Simple setter for hash-based objects which sets and then returns the value
412 for the field name passed as an argument.
417 return $_[0]->{$_[1]} = $_[2];
425 =item Arguments: $field
431 Simple getter for Classes and hash-based objects which returns the value for
432 the field name passed as an argument. This behaves much like
433 L<Class::Data::Accessor> where the field can be set in a base class,
434 inherited and changed in subclasses, and inherited and changed for object
442 if ( ($class = ref $_[0]) && Scalar::Util::blessed $_[0]) {
443 if (Scalar::Util::reftype $_[0] eq 'HASH') {
444 return $_[0]->{$_[1]} if exists $_[0]->{$_[1]};
447 Carp::croak('Cannot get inherited value on an object instance that is not hash-based');
455 no warnings qw/uninitialized/;
457 my $cag_slot = '::__cag_'. $_[1];
458 return ${$class.$cag_slot} if defined(${$class.$cag_slot});
460 # we need to be smarter about recalculation, as @ISA (thus supers) can very well change in-flight
461 my $cur_gen = mro::get_pkg_gen ($class);
462 if ( $cur_gen != ${$class.'::__cag_pkg_gen__'} ) {
463 @{$class.'::__cag_supers__'} = $_[0]->get_super_paths;
464 ${$class.'::__cag_pkg_gen__'} = $cur_gen;
467 for (@{$class.'::__cag_supers__'}) {
468 return ${$_.$cag_slot} if defined(${$_.$cag_slot});
478 =item Arguments: $field, $new_value
484 Simple setter for Classes and hash-based objects which sets and then returns
485 the value for the field name passed as an argument. When called on a hash-based
486 object it will set the appropriate hash key value. When called on a class, it
487 will set a class level variable.
489 B<Note:>: This method will die if you try to set an object variable on a non
495 if (Scalar::Util::blessed $_[0]) {
496 if (Scalar::Util::reftype $_[0] eq 'HASH') {
497 return $_[0]->{$_[1]} = $_[2];
499 Carp::croak('Cannot set inherited value on an object instance that is not hash-based');
504 return ${$_[0].'::__cag_'.$_[1]} = $_[2];
508 =head2 get_component_class
512 =item Arguments: $field
518 Gets the value of the specified component class.
520 __PACKAGE__->mk_group_accessors('component_class' => 'result_class');
522 $self->result_class->method();
525 $self->get_component_class('result_class')->method();
529 sub get_component_class {
530 return $_[0]->get_inherited($_[1]);
533 =head2 set_component_class
537 =item Arguments: $field, $class
543 Inherited accessor that automatically loads the specified class before setting
544 it. This method will die if the specified class could not be loaded.
546 __PACKAGE__->mk_group_accessors('component_class' => 'result_class');
547 __PACKAGE__->result_class('MyClass');
549 $self->result_class->method();
553 sub set_component_class {
556 require Class::Inspector;
557 if (Class::Inspector->installed($_[2]) && !Class::Inspector->loaded($_[2])) {
560 Carp::croak("Could not load $_[1] '$_[2]': ", $@) if $@;
564 return $_[0]->set_inherited($_[1], $_[2]);
567 =head2 get_super_paths
569 Returns a list of 'parent' or 'super' class names that the current class inherited from.
573 sub get_super_paths {
574 return @{mro::get_linear_isa( ref($_[0]) || $_[0] )};
581 To provide total flexibility L<Class::Accessor::Grouped> calls methods
582 internally while performing get/set actions, which makes it noticeably
583 slower than similar modules. To compensate, this module will automatically
584 use the insanely fast L<Class::XSAccessor> to generate the C<simple>-group
585 accessors, if L<< Class::XSAccessor >= 1.06|Class::XSAccessor >> is
586 available on your system.
590 This is the result of a set/get/set loop benchmark on perl 5.12.1 with
591 thread support, showcasing most popular accessor builders: L<Moose>, L<Mouse>,
592 L<CAF|Class::Accessor::Fast>, L<CAF_XS|Class::Accessor::Fast::XS>
593 and L<XSA|Class::XSAccessor>:
595 Rate CAG moOse CAF HANDMADE CAF_XS moUse_XS CAG_XS XSA
596 CAG 1777/s -- -27% -29% -36% -62% -67% -72% -73%
597 moOse 2421/s 36% -- -4% -13% -48% -55% -61% -63%
598 CAF 2511/s 41% 4% -- -10% -47% -53% -60% -61%
599 HANDMADE 2791/s 57% 15% 11% -- -41% -48% -56% -57%
600 CAF_XS 4699/s 164% 94% 87% 68% -- -13% -25% -28%
601 moUse_XS 5375/s 203% 122% 114% 93% 14% -- -14% -18%
602 CAG_XS 6279/s 253% 159% 150% 125% 34% 17% -- -4%
603 XSA 6515/s 267% 169% 159% 133% 39% 21% 4% --
605 Benchmark program is available in the root of the
606 L<repository|http://search.cpan.org/dist/Class-Accessor-Grouped/>:
608 =head2 Notes on Class::XSAccessor
610 While L<Class::XSAccessor> works surprisingly well for the amount of black
611 magic it tries to pull off, it's still black magic. At present (Sep 2010)
612 the module is known to have problems on Windows under heavy thread-stress
613 (e.g. Win32+Apache+mod_perl). Thus for the time being L<Class::XSAccessor>
614 will not be used automatically if you are running under C<MSWin32>.
616 You can force the use of L<Class::XSAccessor> before creating a particular
617 C<simple> accessor by either manipulating the global variable
618 C<$Class::Accessor::Grouped::USE_XS>, or you can do so before runtime via the
619 C<CAG_USE_XS> environment variable.
623 Matt S. Trout <mst@shadowcatsystems.co.uk>
625 Christopher H. Laco <claco@chrislaco.com>
629 Caelum: Rafael Kitover <rkitover@cpan.org>
631 groditi: Guillermo Roditi <groditi@cpan.org>
633 Jason Plum <jason.plum@bmmsi.com>
635 ribasushi: Peter Rabbitson <ribasushi@cpan.org>
638 =head1 COPYRIGHT & LICENSE
640 Copyright (c) 2006-2010 Matt S. Trout <mst@shadowcatsystems.co.uk>
642 This program is free software; you can redistribute it and/or modify
643 it under the same terms as perl itself.