1accessors::classic(3) User Contributed Perl Documentationaccessors::classic(3)
2
3
4
6 accessors::classic - create 'classic' read/write accessor methods in
7 caller's package.
8
10 package Foo;
11 use accessors::classic qw( foo bar baz );
12
13 my $obj = bless {}, 'Foo';
14
15 # always return the current value, even on set:
16 $obj->foo( 'hello ' ) if $obj->bar( 'world' ) eq 'world';
17
18 print $obj->foo, $obj->bar, $obj->baz( "!\n" );
19
21 The accessors::classic pragma lets you create simple classic Perl
22 accessors at compile-time.
23
24 The generated methods look like this:
25
26 sub foo {
27 my $self = shift;
28 $self->{foo} = shift if (@_);
29 return $self->{foo};
30 }
31
32 They always return the current value.
33
34 Note that there is no dash ("-") prepended to the property name as
35 there are in accessors. This is for backwards compatability.
36
38 There is little-to-no performace hit when using generated accessors; in
39 fact there is usually a performance gain.
40
41 • typically 5-15% faster than hard-coded accessors (like the above
42 example).
43
44 • typically 1-15% slower than optimized accessors (less readable).
45
46 • typically a small performance hit at startup (accessors are created
47 at compile-time).
48
49 • uses the same anonymous sub to reduce memory consumption (sometimes
50 by 80%).
51
52 See the benchmark tests included with this distribution for more
53 details.
54
56 Classes using blessed scalarrefs, arrayrefs, etc. are not supported for
57 sake of simplicity. Only hashrefs are supported.
58
60 Steve Purkis <spurkis@cpan.org>
61
63 accessors, accessors::rw, accessors::ro, accessors::chained, base
64
65
66
67perl v5.34.0 2021-07-23 accessors::classic(3)