~ubuntu-branches/ubuntu/trusty/libaccessors-perl/trusty

« back to all changes in this revision

Viewing changes to lib/accessors/ro.pm

  • Committer: Package Import Robot
  • Author(s): Salvatore Bonaccorso
  • Date: 2011-10-16 07:58:01 UTC
  • Revision ID: package-import@ubuntu.com-20111016075801-4z7dy8dvd7275k1x
Tags: upstream-1.01
ImportĀ upstreamĀ versionĀ 1.01

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
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
use strict;
 
28
use warnings::register;
 
29
use base qw( accessors );
 
30
 
 
31
our $VERSION  = '1.01';
 
32
our $REVISION = (split(/ /, ' $Revision: 1.4 $ '))[2];
 
33
 
 
34
use constant style => 'ro';
 
35
 
 
36
sub create_accessor {
 
37
    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
    no strict 'refs';
 
41
    *{$accessor} = sub { return $_[0]->{$property} };
 
42
}
 
43
 
 
44
1;
 
45
 
 
46
__END__
 
47
 
 
48
=head1 DESCRIPTION
 
49
 
 
50
The B<accessors::ro> pragma lets you create simple I<classic> read-only
 
51
accessors at compile-time.
 
52
 
 
53
The generated methods look like this:
 
54
 
 
55
  sub foo {
 
56
      my $self = shift;
 
57
      return $self->{foo};
 
58
  }
 
59
 
 
60
They I<always> return the current value, just like L<accessors::ro>.
 
61
 
 
62
=head1 PERFORMANCE
 
63
 
 
64
There is B<little-to-no performace hit> when using generated accessors; in
 
65
fact there is B<usually a performance gain>.
 
66
 
 
67
=over 4
 
68
 
 
69
=item *
 
70
 
 
71
typically I<5-15% faster> than hard-coded accessors (like the above example).
 
72
 
 
73
=item *
 
74
 
 
75
typically I<0-15% slower> than I<optimized> accessors (less readable).
 
76
 
 
77
=item *
 
78
 
 
79
typically a I<small> performance hit at startup (accessors are created at
 
80
compile-time).
 
81
 
 
82
=item *
 
83
 
 
84
uses the same anonymous sub to reduce memory consumption (sometimes by 80%).
 
85
 
 
86
=back
 
87
 
 
88
See the benchmark tests included with this distribution for more details.
 
89
 
 
90
=head1 CAVEATS
 
91
 
 
92
Classes using blessed scalarrefs, arrayrefs, etc. are not supported for sake
 
93
of simplicity.  Only hashrefs are supported.
 
94
 
 
95
=head1 AUTHOR
 
96
 
 
97
Steve Purkis <spurkis@cpan.org>
 
98
 
 
99
=head1 SEE ALSO
 
100
 
 
101
L<accessors>,
 
102
L<accessors::rw>,
 
103
L<accessors::classic>,
 
104
L<accessors::chained>,
 
105
L<base>
 
106
 
 
107
=cut