Class:...:Classy(3pm)
π NAME π
okay bestie, welcome to the Class::Accessor::Classy page β it's a 3pm manual, from the User Contributed Perl Documentation crew, and it's literally about accessors with minimal inheritance. no cap. π¦
Class::Accessor::Classy β accessors with minimal inheritance β¨
π SYNOPSIS π
package YourPackage;
use Class::Accessor::Classy;
with qw(new); # with a new() method
ro qw(foo); # read-only
rw qw(bar); # read-write
rs baz => \ (my $set_baz); # read-only, plus a secret writer
# alternatively:
my $set_bip = rs 'bip';
ro_c suitcase => 'red'; # read-only class data
rw_c hat => 'black'; # read-write class data
rs_c socks => \ (my $set_socks) => undef;
# alternative secret writer syntax
my $set_shoes = rs_c shoes => undef;
# also class read-only:
constant seven => 7;
constant eight => this->seven + 1;
no Class::Accessor::Classy;
# ^-- removes all of the syntax bits from your namespace
package whatever;
YourPackage->set_hat(undef);
my $obj = YourPackage->new(foo => 4, bar => 2);
# NOTE I'm thinking of deprecating the get_foo() usage
warn "foo ", $obj->foo;
YourPackage->$set_socks("tube");
π About π
This module is giving extremely small-footprint accessor/mutator declaration energy for fast and convenient object attribute setup. It's meant to be a simple and speedy way to stop yourself from typo-ing your hash keys β not a full-blown object system with type checking and all that drama. π
The accessor methods show up as a hidden parent class of your package and generally try to stay out of the way. The accessors and mutators it generates are of the form foo() and set_foo(), respectively. slay.
β¨ Frontend β¨
Unlike other class-modifying code, this one is not designed to be inherited from. Instead you just use it and get an invisible subclass holding all your accessors. And if you use the 'no' syntax (to call unimport), you're left with a squeaky-clean namespace. π
After 'use' and before 'no', the following pieces of syntax are available:
π« with
Add a 'standard' method to your class.
new
π« ro
Read-only properties (accessors only.)
ro qw(foo bar baz);
π« rw
Define read-write (accessor + mutator) properties.
rw qw(foo bar baz);
π« lv
Properties with lvalue accessors.
lv qw(thing deal stuff);
π« ri
Immutable properties. Once set, further calls to the mutator throw errors. π
ri qw(foo bar baz);
π« rs
Read-only properties with a secret mutator. π€«
rs foo => \(my $set_foo);
π« lo
Read-only list properties. These are stored as an array-ref, but the accessor returns a list.
lo qw(foo bar baz);
π« lw
Read-write list properties. The mutator takes a list.
lw 'foo';
This defaults to create foo()|get_foo(), set_foo(), and add_foo() methods. Other features are possible here, but currently experimental. π§ͺ
π« ls
List property with a secret mutator.
ls foo => \(my $set_foo);
π« this
A shortcut for your classname. Useful for e.g. defining one constant in terms of another.
this->some_class_method;
π« getter
Define a custom getter.
π« setter
Define a custom setter.
π« constant
A class constant.
constant foo => 7;
π« ro_c
Read-only class method.
π« rw_c
A read-write class method, with a default.
rw_c foo => 9;
π« rs_c
A class method with a secret setter.
rs_c bar => \(my $set_bar) => 12;
π« in
Specify the destination package. You need to set this before defining anything else (but it is usually best to just not set it.)
in 'why_be_so_secretive';
π« aka
Add an alias for an existing method.
aka have_method => 'want_method', 'and_also_want';
π¦ Utilities π¦
This introspection stuff is unreliable -- don't use it.
π find_accessors
@attribs = Class::Accessor::Classy->find_accessors($class);
π find_subclasses
@classlist = Class::Accessor::Classy->find_subclasses($class);
π Subclassable π
Customized subclasses may override these methods to create a new kind of accessor generator.
β NOTE β
You do not subclassClass::Accessor::Classyto construct your objects. If you are just creatingMyObject, you are not inheriting any of these methods. The rest of this documentation only pertains to you if you are trying to create something likeClass::Accessor::Classy::MyWay.
β notation: β
Read these as: $CAC = 'Class::Accessor::Classy'; (or whatever subclass you're creating.)
π exports
my %exports = $CAC->exports;
π import
$CAC->import;
π unimport
$CAC->unimport;
π create_package
Creates and returns the package in which the accessors will live. Also pushes the created accessor package into the caller's @ISA.
If it already exists, simply returns the cached value.
my $package = $CAC->create_package(
class => $caller,
in => $package, # optional
);
π install_sub
$CAC->install_sub($class, $name, $subref, $note);
π annotate
$CAC->annotate($class, $name, $note);
π get_notes
my %notes = $CAC->get_notes;
π make_standards
$CAC->make_standards($class, @list);
π _getter
Returns a compiled getter subref corresponding to whether or not the class has a '--get' method.
$CAC->_getter($class, $item);
π make_getters
$CAC->make_getters($class, @list);
π make_lv_getters
$CAC->make_lv_getters($class, @list);
π _setter
Returns a compiled setter subref corresponding to whether or not the class has a '--set' method.
$CAC->_setter($class, $item);
π make_setters
$CAC->make_setters($class, @list);
π make_immutable
Creates immutable (one-time-only) setters.
CAC->make_immutable($class, @list);
π make_secrets
my @names = $CAC->make_secrets($class, @list);
π make_aliases
Constructs 'get_' aliases for a @list of accessors.
$CAC->make_aliases($class, @list);
π make_aka
Create a list of alias methods which runtime refer to $realname.
$CAC->make_aka($where, $realname, @aliases);
π do_eval
my $subref = $package->do_eval($string, @checks);
πΈ List Accessors πΈ
π make_array_method
$CAC->make_array_method(
class => $class,
item => $name,
functions => [@functions],
secret => $bool,
);
If secret is true, will return the list of names.
π _get_array_subs
my %subs = $CAC->_get_array_subs($name);
π Class Accessors π
π make_class_data
$CAC->make_class_data($mode, $class, $key, $value);
If mode is 'rs', returns the secret setter name.
π«Ά AUTHOR π«Ά
Eric Wilhelm @ <ewilhelm at cpan dot org>
π BUGS π
If you found this module on CPAN, please report any bugs or feature requests through the web interface at <http://rt.cpan.org>. I will be notified, and then you'll automatically be notified of progress on your bug as I make changes. π₯
If you pulled this development version from my /svn/, please contact me directly.
π COPYRIGHT π
Copyright (C) 2006-2007 Eric L. Wilhelm, All Rights Reserved.
π NO WARRANTY π
Absolutely, positively NO WARRANTY, neither express or implied, is offered with this software. You use this software at your own risk. In case of loss, no person or entity owes you anything whatseover. You have been warned. π
β LICENSE β
This program is free software; you can redistribute it and/or modify it under the same terms as Perl itself.
and that's the whole page bestie, mwah π
β¨ β brought to you by perl v5.36.0, dated 2022-11-20, from the Class:...:Classy(3pm) manual. go forth and typo-proof your hash keys. π¦π
π¦ where this page comes from
- debian bookworm: libclass-accessor-classy-perl 0.9.1-4 Β· Artistic-1.0-Perl OR GPL-1.0-or-later
- debian trixie: libclass-accessor-classy-perl 0.9.1-4 Β· Artistic-1.0-Perl OR GPL-1.0-or-later
- ubuntu noble: libclass-accessor-classy-perl 0.9.1-4 Β· Artistic-1.0-Perl OR GPL-1.0-or-later
- ubuntu resolute: libclass-accessor-classy-perl 0.9.1-4 Β· Artistic-1.0-Perl OR GPL-1.0-or-later
- ubuntu stonking: libclass-accessor-classy-perl 0.9.1-4 Β· Artistic-1.0-Perl OR GPL-1.0-or-later
- π girlified by deepseek-ai/DeepSeek-V4.1-Flash on : a language model's rewrite of the original, under its terms