accessors::ro(3pm)

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

πŸ’– accessors::ro πŸ’–

okay so bestie, this is the manual page for accessors::ro, a lil Perl pragma that lives in the User Contributed Perl Documentation corner of the internet. she's got one job and she does it slayfully: making read-only accessor methods in your caller's package. no cap. ✨


πŸŽ€ NAME πŸŽ€

accessors::ro β€” create 'classic' read-only accessor methods in caller's package.


🌸 SYNOPSIS 🌸

package Foo;
use accessors::ro qw( foo bar baz );

my $obj = bless { foo => 'read only? ' }, 'Foo';

# values are read-only, so set is disabled:
print "oh my!\n" if $obj->foo( "set?" ) eq 'read only? ';

# if you really need to change the vars,
# you must use direct-variable-access:
$obj->{bar} = 'i need a drink ';
$obj->{baz} = 'now';

# always returns the current value:
print $obj->foo, $obj->bar, $obj->baz, "!\n";

πŸ¦‹ DESCRIPTION πŸ¦‹

so the accessors::ro pragma is literally the girl who shows up at compile-time and hands you simple classic read-only accessors. that's it. that's her whole deal. πŸ’…

the methods she generates look exactly like this:

sub foo {
    my $self = shift;
    return $self->{foo};
}

and they always return the current value, just like accessors::ro. periodt. 🫢


πŸ’œ PERFORMANCE πŸ’œ

ngl there is little-to-no performace hit when you use the generated accessors β€” lowkey there's usually a performance gain. main character energy fr fr.

  • typically 5-15% faster than hard-coded accessors (like the above example). πŸ”₯
  • typically 0-15% slower than optimized accessors (less readable).
  • typically a small performance hit at startup (accessors are created at compile-time).
  • uses the same anonymous sub to reduce memory consumption (sometimes by 80%).

see the benchmark tests included with this distribution for more details, bestie. πŸ“Š


⚠️ CAVEATS ⚠️

classes using blessed scalarrefs, arrayrefs, etc. are not supported β€” for sake of simplicity. only hashrefs are supported. so if your object is giving anything other than hashref... it's not gonna work, sorry gorgeous. 😭


πŸ‘‘ AUTHOR πŸ‘‘

Steve Purkis <spurkis@cpan.org>


🌈 SEE ALSO 🌈

  • accessors
  • accessors::rw
  • accessors::classic
  • accessors::chained
  • base

that's the whole vibe, bestie β€” go write some read-only accessors and eat. πŸ’…βœ¨

this page ships with perl v5.36.0 and is dated 2022-10-13. iconic. no notes. πŸŽ€πŸ’–
πŸ“¦ where this page comes from