File Coverage

blib/lib/accessors/classic.pm
Criterion Covered Total %
statement 19 19 100.0
branch 2 2 100.0
condition n/a
subroutine 7 7 100.0
pod 0 1 0.0
total 28 29 96.5


line stmt bran cond sub pod time code
1             =head1 NAME
2              
3             accessors::classic - create 'classic' read/write accessor methods in caller's package.
4              
5             =head1 SYNOPSIS
6              
7             package Foo;
8             use accessors::classic qw( foo bar baz );
9              
10             my $obj = bless {}, 'Foo';
11              
12             # always return the current value, even on set:
13             $obj->foo( 'hello ' ) if $obj->bar( 'world' ) eq 'world';
14              
15             print $obj->foo, $obj->bar, $obj->baz( "!\n" );
16              
17             =cut
18              
19             package accessors::classic;
20              
21 2     2   20725 use strict;
  2         5  
  2         71  
22 2     2   10 use warnings::register;
  2         4  
  2         297  
23 2     2   12 use base qw( accessors );
  2         5  
  2         1062  
24              
25             our $VERSION = '1.01';
26             our $REVISION = (split(/ /, ' $Revision: 1.5 $ '))[2];
27              
28 2     2   10 use constant style => 'classic';
  2         5  
  2         126  
29              
30             sub create_accessor {
31 5     5 0 7 my ($class, $accessor, $property) = @_;
32             # set/get is slightly faster if we eval instead of using a closure + anon
33             # sub, but the difference is marginal (~5%), and this uses less memory...
34 2     2   9 no strict 'refs';
  2         3  
  2         151  
35 5         33 *{$accessor} = sub {
36 6 100   6   1922 (@_ > 1) ? $_[0]->{$property} = $_[1] : $_[0]->{$property};
37             }
38 5         29 }
39              
40             1;
41              
42             __END__