access...lassic(3pm)
π accessors::classic β the manual, but make it cute
okay bestie, so we're talking about accessors::classic, a Perl thing from the User Contributed Perl Documentation section (that's the 3pm vibe iykyk). it's a pragma, it's iconic, and it makes accessor methods for you. let's get into it π
β¨ NAME β¨
accessors::classic β create 'classic' read/write accessor methods in caller's package.
like. that's the whole job. it pops accessors into your package. no cap. π«Ά
β¨ SYNOPSIS β¨
package Foo;
use accessors::classic qw( foo bar baz );
my $obj = bless {}, 'Foo';
# always return the current value, even on set:
$obj->foo( 'hello ' ) if $obj->bar( 'world' ) eq 'world';
print $obj->foo, $obj->bar, $obj->baz( "!\n" );
okay so look at that little moment right there β $obj->bar( 'world' ) sets the value and hands you back 'world' in the same breath, so the eq 'world' check is true and the if fires. the way that works?? obsessed. π
β¨ DESCRIPTION β¨
so accessors::classic is a pragma that lets you create simple, classic Perl accessors at compile-time. boom. done. before your program even really gets going, the accessors just exist. main character energy fr fr. π
the generated methods look like this:
sub foo {
my $self = shift;
$self->{foo} = shift if (@_);
return $self->{foo};
}
they always return the current value. always always. set it, get it back, it's giving read/write. β¨
heads up bestie: there is no dash ("-") prepended to the property name like there is inaccessors. that's on purpose β it's for backwards compatibility. do not come for me about it, it's intentional. π
β¨ PERFORMANCE β¨
okay so the tea here is that using generated accessors has little-to-no performance hit β and honestly, there's usually a performance gain. slay. π₯
- typically 5-15% faster than hard-coded accessors (like the above example).
- typically 1-15% slower than optimized accessors (which are, and i quote, less readable).
- typically a small performance hit at startup β because the accessors are created at compile-time.
- uses the same anonymous sub to reduce memory consumption (sometimes by 80%?!?! π iconic).
wanna see the receipts? check the benchmark tests included with this distribution for more details. π§
β¨ CAVEATS β¨
okay so here's the boundary, bestie, and it's a hard one:
classes using blessed scalarrefs, arrayrefs, etc. are not supported, for the sake of simplicity. only hashrefs are supported. periodt.
so if your object is a blessed scalarref... it's not happening. sorry babe π
β¨ AUTHOR β¨
Steve Purkis <spurkis@cpan.org> β absolute legend, we love them, we thank them π«Ά
β¨ SEE ALSO β¨
go read these next, bestie, they're the whole friend group:
accessorsaccessors::rwaccessors::roaccessors::chainedbase
and that's the whole page!! π this one came from perl v5.36.0, dated 2022-10-13, and it lives in the User Contributed Perl Documentation section as access...lassic(3pm). mwah, go write some accessors, you've got this πΈβπ¦
π¦ where this page comes from
- ubuntu noble: libaccessors-perl 1.01-4 Β· Artistic-1.0-Perl OR GPL-1.0-or-later
- ubuntu resolute: libaccessors-perl 1.01-4 Β· Artistic-1.0-Perl OR GPL-1.0-or-later
- ubuntu stonking: libaccessors-perl 1.01-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