| 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 | ▶ | use strict; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 22 | 2 | ▶ | use warnings::register; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 23 | 2 | ▶ | use base qw( accessors ); | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 24 | |||||||||||
| 25 | 2 | our $VERSION = '1.01'; | |||||||||
| 26 | 2 | our $REVISION = (split(/ /, ' $Revision: 1.5 $ '))[2]; | |||||||||
| 27 | |||||||||||
| 28 | 2 | ▶ | use constant style => 'classic'; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 29 | |||||||||||
| 30 | 5 | ▶ | sub create_accessor { | ||||||||
|
Subroutine
create_accessor: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||
| 31 | 5 | 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 | ▶ | no strict 'refs'; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 35 | 5 | ▶ | *{$accessor} = sub { | ||||||||
|
Subroutine
__ANON__: called CC=2 SCAR=6.9
| |||||||||||
| 36 | 6 | ▶ | (@_ > 1) ? $_[0]->{$property} = $_[1] : $_[0]->{$property}; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 37 | } | ||||||||||
| 38 | 5 | } | |||||||||
| 39 | |||||||||||
| 40 | 2 | 1; | |||||||||
| 41 | |||||||||||
| 42 | __END__ | ||||||||||