1accessors::ro(3) User Contributed Perl Documentation accessors::ro(3)
2
3
4
6 accessors::ro - create 'classic' read-only accessor methods in caller's
7 package.
8
10 package Foo;
11 use accessors::ro qw( foo bar baz );
12
13 my $obj = bless { foo => 'read only? ' }, 'Foo';
14
15 # values are read-only, so set is disabled:
16 print "oh my!\n" if $obj->foo( "set?" ) eq 'read only? ';
17
18 # if you really need to change the vars,
19 # you must use direct-variable-access:
20 $obj->{bar} = 'i need a drink ';
21 $obj->{baz} = 'now';
22
23 # always returns the current value:
24 print $obj->foo, $obj->bar, $obj->baz, "!\n";
25
27 The accessors::ro pragma lets you create simple classic read-only
28 accessors at compile-time.
29
30 The generated methods look like this:
31
32 sub foo {
33 my $self = shift;
34 return $self->{foo};
35 }
36
37 They always return the current value, just like accessors::ro.
38
40 There is little-to-no performace hit when using generated accessors; in
41 fact there is usually a performance gain.
42
43 · typically 5-15% faster than hard-coded accessors (like the above
44 example).
45
46 · typically 0-15% slower than optimized accessors (less readable).
47
48 · typically a small performance hit at startup (accessors are created
49 at compile-time).
50
51 · uses the same anonymous sub to reduce memory consumption (sometimes
52 by 80%).
53
54 See the benchmark tests included with this distribution for more
55 details.
56
58 Classes using blessed scalarrefs, arrayrefs, etc. are not supported for
59 sake of simplicity. Only hashrefs are supported.
60
62 Steve Purkis <spurkis@cpan.org>
63
65 accessors, accessors::rw, accessors::classic, accessors::chained, base
66
67
68
69perl v5.28.0 2018-07-15 accessors::ro(3)