Class::...Grouped(3)
π
Class::...Grouped(3) β User Contributed Perl Documentation π
okay bestie, real talk: the header says Class::...Grouped(3) and it's from the User Contributed Perl Documentation, but the module actually doing the slaying here is Class::Accessor::Grouped. This is part 1, so get comfy. π
β¨ NAME β¨
Class::Accessor::Grouped β lets you build groups of accessors. That's the whole vibe. π
β¨ SYNOPSIS β¨
use base 'Class::Accessor::Grouped';
# make basic accessors for objects
__PACKAGE__->mk_group_accessors(simple => qw(id name email));
# make accessor that works for objects and classes
__PACKAGE__->mk_group_accessors(inherited => 'awesome_level');
# make an accessor which calls a custom pair of getters/setters
sub get_column { ... this will be called when you do $obj->name() ... }
sub set_column { ... this will be called when you do $obj->name('foo') ... }
__PACKAGE__->mk_group_accessors(column => 'name');
π DESCRIPTION π
This class lets you build groups of accessors that call different getters and setters. The docs for this module still need a lot of work β literally, volunteers welcome >.> β but in the meantime you can peek at this post for more info: http://lo-f.at/glahn/2009/08/WritingPowerfulAccessorsForPerlClasses.html. π¦
π Notes on accessor names π
So, generally, method names in Perl are considered identifiers, and they need to conform to the identifier spec of qr/\A[A-Z_a-z][0-9A-Z_a-z]*\z/. You can invoke methods with non-standard names like $obj->${\"anything goes"}, but you can't properly declare those methods without Sub::Name. π
Because this module has to work identically with and without its optional dependencies, starting with version 0.10008, trying to declare an accessor with a non-standard name is a fatal error. (Those operations would silently succeed since version 0.08004 as long as Sub::Name was present, or otherwise they'd result in a syntax error during a string eval.) π
Unfortunately, over the years since 0.08004, a pretty large body of code accumulated out in the wild that does try to declare accessors with funny names. One notable perpetrator is DBIx::Class::Schema::Loader, which under certain conditions could create accessors of the "column" group that start with numbers and/or some other punctuation. The proper way would be to declare columns with the "accessor" attribute set to "undef". π
So there's an escape mechanism via the environment variable CAG_ILLEGAL_ACCESSOR_NAME_OK. When it's set to a true value, one warning is issued per class on attempts to declare an accessor with a non-conforming name, and as long as Sub::Name is available, all accessors will be properly created. β¨
Regardless of this setting, accessor names containing nulls "\0" are disallowed, due to various deficiencies in perl itself. π«Ά
If your code base has too many instances of illegal accessor declarations and a fix is not feasible due to time constraints, you can disable the warnings altogether by setting $ENV{CAG_ILLEGAL_ACCESSOR_NAME_OK} to "DO_NOT_WARN" β observe capitalization, bestie. π
π METHODS π
β¨ mk_group_accessors β¨
__PACKAGE__->mk_group_accessors(simple => 'hair_length', [ hair_color => 'hc' ]);
- Arguments:
$group,@fieldspec - Returns: none
Creates a set of accessors in a given group. π
$group is the name of the accessor group for the generated accessors; they will call get_$group($field) on get and set_$group($field, $value) on set.
If you want to mimic Class::Accessor's mk_accessors, $group has to be 'simple' to tell Class::Accessor::Grouped to use its own get_simple and set_simple methods. π
@fieldspec is a list of field/accessor names. If a fieldspec is a scalar, it's used as both the field and the accessor name. If it's a listref, it's expected to be of the form [ $accessor, $field ]. β¨
πΈ mk_group_ro_accessors πΈ
__PACKAGE__->mk_group_ro_accessors(simple => 'birthdate', [ social_security_number => 'ssn' ]);
- Arguments:
$group,@fieldspec - Returns: none
Creates a set of read only accessors in a given group. Identical to "mk_group_accessors", but the accessors will throw an error if passed a value rather than setting the value. π₯
π¦ mk_group_wo_accessors π¦
__PACKAGE__->mk_group_wo_accessors(simple => 'lie', [ subject => 'subj' ]);
- Arguments:
$group,@fieldspec - Returns: none
Creates a set of write only accessors in a given group. Identical to "mk_group_accessors", but the accessors will throw an error if not passed a value rather than getting the value. π
π get_simple π
- Arguments:
$field - Returns:
$value
Simple getter for hash-based objects which returns the value for the field name passed as an argument. π
π set_simple π
- Arguments:
$field,$new_value - Returns:
$new_value
Simple setter for hash-based objects which sets and then returns the value for the field name passed as an argument. β¨
π get_inherited π
- Arguments:
$field - Returns:
$value
Simple getter for Classes and hash-based objects which returns the value for the field name passed as an argument. This behaves much like Class::Data::Accessor, where the field can be set in a base class, inherited and changed in subclasses, and inherited and changed for object instances. π
π set_inherited π
- Arguments:
$field,$new_value - Returns:
$new_value
Simple setter for Classes and hash-based objects which sets and then returns the value for the field name passed as an argument. When called on a hash-based object it will set the appropriate hash key value. When called on a class, it will set a class level variable. π
Note:: This method will die if you try to set an object variable on a non hash-based object. π
π get_component_class π
- Arguments:
$field - Returns:
$value
Gets the value of the specified component class. π«Ά
__PACKAGE__->mk_group_accessors('component_class' => 'result_class');
$self->result_class->method();
## same as
$self->get_component_class('result_class')->method();
π set_component_class π
- Arguments:
$field,$class - Returns:
$new_value
Inherited accessor that automatically loads the specified class before setting it. This method will die if the specified class could not be loaded. π
__PACKAGE__->mk_group_accessors('component_class' => 'result_class');
__PACKAGE__->result_class('MyClass');
$self->result_class->method();
π οΈ INTERNAL METHODS π οΈ
These methods are documented for clarity, but they are never meant to be called directly, and they're not really meant for overriding either. π
π§ get_super_paths π§
Returns a list of 'parent' or 'super' class names that the current class inherited from. This is what drives the traversal done by "get_inherited". β¨
π§ make_group_accessor π§
__PACKAGE__->make_group_accessor('simple', 'hair_length', 'hair_length');
__PACKAGE__->make_group_accessor('simple', 'hc', 'hair_color');
- Arguments:
$group,$field,$accessor - Returns:
\&accessor_coderef ?
Called by mk_group_accessors for each entry in @fieldspec. Either returns a coderef which will be installed at &__PACKAGE__::$accessor, or returns undef if it elects to install the coderef on its own. π
π§ make_group_ro_accessor π§
__PACKAGE__->make_group_ro_accessor('simple', 'birthdate', 'birthdate');
__PACKAGE__->make_group_ro_accessor('simple', 'ssn', 'social_security_number');
- Arguments:
$group,$field,$accessor - Returns:
\&accessor_coderef ?
Called by mk_group_ro_accessors for each entry in @fieldspec. Either returns a coderef which will be installed at &__PACKAGE__::$accessor, or returns undef if it elects to install the coderef on its own. πΈ
π§ make_group_wo_accessor π§
__PACKAGE__->make_group_wo_accessor('simple', 'lie', 'lie');
__PACKAGE__->make_group_wo_accessor('simple', 'subj', 'subject');
- Arguments:
$group,$field,$accessor - Returns:
\&accessor_coderef ?
Called by mk_group_wo_accessors for each entry in @fieldspec. Either returns a coderef which will be installed at &__PACKAGE__::$accessor, or returns undef if it elects to install the coderef on its own. π¦
π PERFORMANCE π
okay bestie, real talk: to provide total flexibility, Class::Accessor::Grouped calls methods internally while performing get/set actions, which makes it noticeably slower than similar modules. lowkey that's the tradeoff, and it's giving flexible but not free energy.
To compensate, this module will automatically use the insanely fast Class::XSAccessor to generate the "simple"-group accessors if this module is available on your system. slay π
π Benchmark π
This is the benchmark of 200 get/get/set/get/set cycles on perl 5.16.2 with thread support, showcasing how this module's simple (CAG_S), inherited (CAG_INH) and inherited with parent-class data (CAG_INHP) accessors stack up against most popular accessor builders: Moose, Moo, Mo, Mouse (both pure-perl and XS variant), Object::Tiny::RW (OTRW), Class::Accessor (CA), Class::Accessor::Lite (CAL), Class::Accessor::Fast (CAF), Class::Accessor::Fast::XS (CAF_XS) and Class::XSAccessor (XSA). obsessed with this leaderboard moment π
Rate CAG_INHP CAG_INH CA CAG_S CAF moOse OTRW CAL mo moUse HANDMADE moo CAF_XS moUse_XS XSA
CAG_INHP 287.021+-0.02/s -- -0.3% -10.0% -37.1% -53.1% -53.6% -53.7% -54.1% -56.9% -59.0% -59.6% -59.8% -78.7% -81.9% -83.5%
CAG_INH 288.025+-0.031/s 0.3% -- -9.7% -36.9% -52.9% -53.5% -53.5% -53.9% -56.7% -58.8% -59.5% -59.7% -78.6% -81.9% -83.5%
CA 318.967+-0.047/s 11.1% 10.7% -- -30.1% -47.9% -48.5% -48.5% -49.0% -52.1% -54.4% -55.1% -55.3% -76.3% -79.9% -81.7%
CAG_S 456.107+-0.054/s 58.9% 58.4% 43.0% -- -25.4% -26.3% -26.4% -27.0% -31.5% -34.8% -35.8% -36.1% -66.1% -71.3% -73.9%
CAF 611.745+-0.099/s 113.1% 112.4% 91.8% 34.1% -- -1.2% -1.2% -2.1% -8.1% -12.6% -14.0% -14.3% -54.5% -61.5% -64.9%
moOse 619.051+-0.059/s 115.7% 114.9% 94.1% 35.7% 1.2% -- -0.1% -1.0% -7.0% -11.6% -12.9% -13.3% -54.0% -61.0% -64.5%
OTRW 619.475+-0.1/s 115.8% 115.1% 94.2% 35.8% 1.3% 0.1% -- -0.9% -6.9% -11.5% -12.9% -13.2% -54.0% -61.0% -64.5%
CAL 625.106+-0.085/s 117.8% 117.0% 96.0% 37.1% 2.2% 1.0% 0.9% -- -6.1% -10.7% -12.1% -12.5% -53.5% -60.6% -64.2%
mo 665.44+-0.12/s 131.8% 131.0% 108.6% 45.9% 8.8% 7.5% 7.4% 6.5% -- -4.9% -6.4% -6.8% -50.5% -58.1% -61.9%
moUse 699.9+-0.15/s 143.9% 143.0% 119.4% 53.5% 14.4% 13.1% 13.0% 12.0% 5.2% -- -1.6% -2.0% -48.0% -55.9% -59.9%
HANDMADE 710.98+-0.16/s 147.7% 146.8% 122.9% 55.9% 16.2% 14.9% 14.8% 13.7% 6.8% 1.6% -- -0.4% -47.2% -55.2% -59.2%
moo 714.04+-0.13/s 148.8% 147.9% 123.9% 56.6% 16.7% 15.3% 15.3% 14.2% 7.3% 2.0% 0.4% -- -46.9% -55.0% -59.1%
CAF_XS 1345.55+-0.051/s 368.8% 367.2% 321.8% 195.0% 120.0% 117.4% 117.2% 115.3% 102.2% 92.2% 89.3% 88.4% -- -15.3% -22.9%
moUse_XS 1588+-0.036/s 453.3% 451.3% 397.9% 248.2% 159.6% 156.5% 156.3% 154.0% 138.6% 126.9% 123.4% 122.4% 18.0% -- -9.0%
XSA 1744.67+-0.052/s 507.9% 505.7% 447.0% 282.5% 185.2% 181.8% 181.6% 179.1% 162.2% 149.3% 145.4% 144.3% 29.7% 9.9% --
The benchmarking program is available in the root of the repository: http://search.cpan.org/dist/Class-Accessor-Grouped/. periodt.
π§ Notes on Class::XSAccessor π§
You can force (or disable) the use of Class::XSAccessor before creating a particular "simple" accessor by either manipulating the global variable $Class::Accessor::Grouped::USE_XS to true or false (preferably with localization, or you can do so before runtime via the CAG_USE_XS environment variable). iykyk π¦
Since Class::XSAccessor has no knowledge of get_simple and set_simple, this module does its best to detect if you are overriding one of these methods and will fall back to using the perl version of the accessor in order to maintain consistency. However be aware that if you enable use of Class::XSAccessor (automatically or explicitly), create an object, invoke a simple accessor on that object, and then manipulate the symbol table to install a get/set_simple override - you get to keep all the pieces. π
fr fr, that last bit is the caution tape: you get to keep all the pieces. no cap.
π AUTHORS π
These two legends wrote it:
- Matt S. Trout
<mst@shadowcatsystems.co.uk> - Christopher H. Laco
<claco@chrislaco.com>
π CONTRIBUTORS π
and these icons contributed:
- Caelum: Rafael Kitover
<rkitover@cpan.org> - frew: Arthur Axel "fREW" Schmidt
<frioux@gmail.com> - groditi: Guillermo Roditi
<groditi@cpan.org> - Jason Plum
<jason.plum@bmmsi.com> - ribasushi: Peter Rabbitson
<ribasushi@cpan.org>
π COPYRIGHT & LICENSE π
Copyright (c) 2006-2010 Matt S. Trout <mst@shadowcatsystems.co.uk> This program is free software; you can redistribute it and/or modify it under the same terms as perl itself.
okay bestie, that's the whole page π source/version is perl v5.42.0, dated 2019-08-20. go be iconic with your accessors β¨π«Ά
π¦ where this page comes from
- opensuse-leap 16.0: perl-Class-Accessor-Grouped 0.10014-bp160.1.11 Β· Artistic-1.0 OR GPL-1.0-or-later
- π girlified by deepseek-ai/DeepSeek-V4.1-Flash-1017 on : a language model's rewrite of the original, under its terms