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
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;
# ^-- removes all of the syntax bits from your namespace
my $obj = YourPackage->new(foo => 4, bar => 2);
# NOTE I'm thinking of deprecating the get_foo() usage
warn "foo ", $obj->foo;
AboutThis module provides an extremely small-footprint accessor/mutator declaration scheme for fast and convenient object attribute setup. It is intended as a simple and speedy mechanism for preventing hash-key typos rather than a full-blown object system with type checking and so on.
The accessor methods appear as a hidden parent class of your package and generally try to stay out of the way. The accessors and mutators generated are of the form "foo()" and "set_foo()", respectively.
FrontendUnlike other class-modifying code, this is not designed to be inherited from. Instead, you simply use it and get an invisible subclass containing your accessors. If you use the 'no' syntax (to call unimport), you are left with a squeaky-clean namespace.
After 'use' and before 'no', the following pieces of syntax are available.
withAdd a 'standard' method to your class.
roRead-only properties (accessors only.)
ro qw(foo bar baz);
rwDefine read-write (accessor + mutator) properties.
rw qw(foo bar baz);
lvProperties with lvalue accessors.
lv qw(thing deal stuff);
riImmutable properties. Once set, further calls to the mutator throw errors.
ri qw(foo bar baz);
rsRead-only properties with a secret mutator.
rs foo => \(my $set_foo);
loRead-only list properties. These are stored as an array-ref, but the accessor returns a list.
lo qw(foo bar baz);
lwRead-write list properties. The mutator takes a list.
This defaults to create foo()|get_foo(), set_foo(), and add_foo() methods. Other features are possible here, but currently experimental.
lsList property with a secret mutator.
ls foo => \(my $set_foo);
thisA shortcut for your classname. Useful for e.g. defining one constant in terms of another.
getterDefine a custom getter.
setterDefine a custom setter.
constantA class constant.
constant foo => 7;
ro_cRead-only class method.
rw_cA read-write class method, with a default.
rw_c foo => 9;
rs_cA class method with a secret setter.
rs_c bar => \(my $set_bar) => 12;
inSpecify the destination package. You need to set this before defining anything else (but it is usually best to just not set it.)
akaAdd an alias for an existing method.
aka have_method => 'want_method', 'and_also_want';
UtilitiesThis introspection stuff is unreliable --- don't use it.
@attribs = Class::Accessor::Classy->find_accessors($class);
@classlist = Class::Accessor::Classy->find_subclasses($class);
SubclassableCustomized subclasses may override these methods to create a new kind of accessor generator.
You do not subclass Class::Accessor::Classy to construct your objects.
If you are just creating MyObject, you are not inheriting any of these methods.
The rest of this documentation only pertains to you if you are trying to create something like Class::Accessor::Classy::MyWay.
- Read these as: $CAC = 'Class::Accessor::Classy'; (or whatever subclass you're creating.)
my %exports = $CAC->exports;
create_packageCreates 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 );
$CAC->install_sub($class, $name, $subref, $note);
$CAC->annotate($class, $name, $note);
my %notes = $CAC->get_notes;
_getterReturns a compiled getter subref corresponding to whether or not the class has a '--get' method.
_setterReturns a compiled setter subref corresponding to whether or not the class has a '--set' method.
make_immutableCreates immutable (one-time-only) setters.
my @names = $CAC->make_secrets($class, @list);
make_aliasesConstructs 'get_' aliases for a @list of accessors.
make_akaCreate a list of alias methods which runtime refer to $realname.
$CAC->make_aka($where, $realname, @aliases);
my $subref = $package->do_eval($string, @checks);
$CAC->make_array_method( class => $class, item => $name, functions => [@functions], secret => $bool, );
If secret is true, will return the list of names.
my %subs = $CAC->_get_array_subs($name);
$CAC->make_class_data($mode, $class, $key, $value);
If mode is 'rs', returns the secret setter name.
AUTHOREric Wilhelm @ <ewilhelm at cpan dot org>
BUGSIf 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.
COPYRIGHTCopyright (C) 2006-2007 Eric L. Wilhelm, All Rights Reserved.
NO WARRANTYAbsolutely, 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.
LICENSEThis program is free software; you can redistribute it and/or modify it under the same terms as Perl itself.