accessors::classic(3pm)

accessors::classic3pm

Create 'classic' read/write accessor methods in caller's package.

Usage

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

Overview

The accessors::classic pragma lets you create simple classic Perl accessors at compile-time.

The generated methods look like this:

  sub foo {
      my $self = shift;
      $self->{foo} = shift if (@_);
      return $self->{foo};
  }

They always return the current value.

Note that there is no dash ("-") prepended to the property name as there are in accessors. This is for backwards compatibility.

Performance

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 1-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.

Caveats

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

Releases

7 releases since Debian 7 (Wheezy); the text changed 3 times.

  1. 7–8
  2. 9–10
  3. 11
  4. 12–13

Author

Steve Purkis <[email protected]>

From libaccessors-perl 1.01-4 in Debian 13 (Trixie). Parsed by carta from the page's man source.