GSP
Quick Navigator

Search Site

Unix VPS
A - Starter
B - Basic
C - Preferred
D - Commercial
MPS - Dedicated
Previous VPSs
* Sign Up! *

Support
Contact Us
Online Help
Handbooks
Domain Status
Man Pages

FAQ
Virtual Servers
Pricing
Billing
Technical

Network
Facilities
Connectivity
Topology Map

Miscellaneous
Server Agreement
Year 2038
Credits
 

USA Flag

 

 

Man Pages
accessors::ro(3) User Contributed Perl Documentation accessors::ro(3)

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

  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";

The accessors::ro pragma lets you create simple classic read-only accessors at compile-time.

The generated methods look like this:

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

They always return the current value, just like accessors::ro.

There is little-to-no performace hit when using generated accessors; in fact there is usually a performance gain.
  • 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.

Classes using blessed scalarrefs, arrayrefs, etc. are not supported for sake of simplicity. Only hashrefs are supported.

Steve Purkis <spurkis@cpan.org>

accessors, accessors::rw, accessors::classic, accessors::chained, base
2022-04-07 perl v5.32.1

Search for    or go to Top of page |  Section 3 |  Main Index

Powered by GSP Visit the GSP FreeBSD Man Page Interface.
Output converted with ManDoc.