access...lassic(3pm)

πŸ“¦
version 1.01 Β· 5 versions
, in ubuntu noble, ubuntu resolute, ubuntu stonking

πŸŽ€ 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 in accessors. 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:

  • accessors
  • accessors::rw
  • accessors::ro
  • accessors::chained
  • base

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