line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
1
|
|
|
|
|
|
|
=head1 NAME |
2
|
|
|
|
|
|
|
|
3
|
|
|
|
|
|
|
accessors::ro - create 'classic' read-only accessor methods in caller's package. |
4
|
|
|
|
|
|
|
|
5
|
|
|
|
|
|
|
=head1 SYNOPSIS |
6
|
|
|
|
|
|
|
|
7
|
|
|
|
|
|
|
package Foo; |
8
|
|
|
|
|
|
|
use accessors::ro qw( foo bar baz ); |
9
|
|
|
|
|
|
|
|
10
|
|
|
|
|
|
|
my $obj = bless { foo => 'read only? ' }, 'Foo'; |
11
|
|
|
|
|
|
|
|
12
|
|
|
|
|
|
|
# values are read-only, so set is disabled: |
13
|
|
|
|
|
|
|
print "oh my!\n" if $obj->foo( "set?" ) eq 'read only? '; |
14
|
|
|
|
|
|
|
|
15
|
|
|
|
|
|
|
# if you really need to change the vars, |
16
|
|
|
|
|
|
|
# you must use direct-variable-access: |
17
|
|
|
|
|
|
|
$obj->{bar} = 'i need a drink '; |
18
|
|
|
|
|
|
|
$obj->{baz} = 'now'; |
19
|
|
|
|
|
|
|
|
20
|
|
|
|
|
|
|
# always returns the current value: |
21
|
|
|
|
|
|
|
print $obj->foo, $obj->bar, $obj->baz, "!\n"; |
22
|
|
|
|
|
|
|
|
23
|
|
|
|
|
|
|
=cut |
24
|
|
|
|
|
|
|
|
25
|
|
|
|
|
|
|
package accessors::ro; |
26
|
|
|
|
|
|
|
|
27
|
1
|
|
|
1
|
|
21163
|
use strict; |
|
1
|
|
|
|
|
2
|
|
|
1
|
|
|
|
|
35
|
|
28
|
1
|
|
|
1
|
|
5
|
use warnings::register; |
|
1
|
|
|
|
|
2
|
|
|
1
|
|
|
|
|
165
|
|
29
|
1
|
|
|
1
|
|
6
|
use base qw( accessors ); |
|
1
|
|
|
|
|
1
|
|
|
1
|
|
|
|
|
496
|
|
30
|
|
|
|
|
|
|
|
31
|
|
|
|
|
|
|
our $VERSION = '1.01'; |
32
|
|
|
|
|
|
|
our $REVISION = (split(/ /, ' $Revision: 1.4 $ '))[2]; |
33
|
|
|
|
|
|
|
|
34
|
1
|
|
|
1
|
|
5
|
use constant style => 'ro'; |
|
1
|
|
|
|
|
2
|
|
|
1
|
|
|
|
|
57
|
|
35
|
|
|
|
|
|
|
|
36
|
|
|
|
|
|
|
sub create_accessor { |
37
|
3
|
|
|
3
|
0
|
5
|
my ($class, $accessor, $property) = @_; |
38
|
|
|
|
|
|
|
# get is slightly faster if we eval instead of using a closure + anon |
39
|
|
|
|
|
|
|
# sub, but the difference is marginal (~5%), and this uses less memory... |
40
|
1
|
|
|
1
|
|
5
|
no strict 'refs'; |
|
1
|
|
|
|
|
1
|
|
|
1
|
|
|
|
|
96
|
|
41
|
3
|
|
|
4
|
|
16
|
*{$accessor} = sub { return $_[0]->{$property} }; |
|
3
|
|
|
|
|
18
|
|
|
4
|
|
|
|
|
836
|
|
42
|
|
|
|
|
|
|
} |
43
|
|
|
|
|
|
|
|
44
|
|
|
|
|
|
|
1; |
45
|
|
|
|
|
|
|
|
46
|
|
|
|
|
|
|
__END__ |