| 1 | =head1 NAME | ||||||||||
| 2 | |||||||||||
| 3 | accessors - create accessor methods in caller's package. | ||||||||||
| 4 | |||||||||||
| 5 | =head1 SYNOPSIS | ||||||||||
| 6 | |||||||||||
| 7 | package Foo; | ||||||||||
| 8 | use accessors qw( foo bar baz ); | ||||||||||
| 9 | |||||||||||
| 10 | my $obj = bless {}, 'Foo'; | ||||||||||
| 11 | |||||||||||
| 12 | # generates chaining accessors | ||||||||||
| 13 | # that you can set like this: | ||||||||||
| 14 | $obj->foo( 'hello ' ) | ||||||||||
| 15 | ->bar( 'world' ) | ||||||||||
| 16 | ->baz( "!\n" ); | ||||||||||
| 17 | |||||||||||
| 18 | # you get the values by passing no params: | ||||||||||
| 19 | print $obj->foo, $obj->bar, $obj->baz; | ||||||||||
| 20 | |||||||||||
| 21 | =cut | ||||||||||
| 22 | |||||||||||
| 23 | package accessors; | ||||||||||
| 24 | |||||||||||
| 25 | 7 | ▶ | use 5.006; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 26 | 7 | ▶ | use strict; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 27 | 7 | ▶ | use warnings::register; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 28 | |||||||||||
| 29 | 7 | our $VERSION = '1.01'; | |||||||||
| 30 | 7 | our $REVISION = (split(/ /, ' $Revision: 1.22 $ '))[2]; | |||||||||
| 31 | |||||||||||
| 32 | 7 | our $Debug = 0; | |||||||||
| 33 | 7 | our $ExportLevel = 0; | |||||||||
| 34 | 7 | our @InvalidNames = qw( BEGIN CHECK INIT END DESTROY AUTOLOAD ); | |||||||||
| 35 | |||||||||||
| 36 | 7 | ▶ | use constant style => 'chained'; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 37 | |||||||||||
| 38 | 23 | ▶ | sub import { | ||||||||
|
Subroutine
import: called CC=2 SCAR=6.9
| |||||||||||
| 39 | 23 | my $class = shift; | |||||||||
| 40 | 23 | my $callpkg = caller( $class->ExportLevel ); | |||||||||
| 41 | |||||||||||
| 42 | 23 | ▶ | my @properties = @_ or return; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 43 | |||||||||||
| 44 | 16 | $class->create_accessors_for( $callpkg, @properties ); | |||||||||
| 45 | } | ||||||||||
| 46 | |||||||||||
| 47 | 16 | ▶ | sub create_accessors_for { | ||||||||
|
Subroutine
create_accessors_for: called CC=5 SCAR=16.2
Pod
undocumented
| |||||||||||
| 48 | 16 | my $class = shift; | |||||||||
| 49 | 16 | my $callpkg = shift; | |||||||||
| 50 | |||||||||||
| 51 | 16 | ▶ | warn( 'creating ' . $class->style . ' accessors( ', | ||||||||
|
Branch: 1 branch
| |||||||||||
| 52 | join(' ',@_)," ) in pkg '$callpkg'" ) if $class->Debug; | ||||||||||
| 53 | |||||||||||
| 54 | 16 | foreach my $property (@_) { | |||||||||
| 55 | 21 | my $accessor = "$callpkg\::$property"; | |||||||||
| 56 | 21 | ▶ | die( "can't create $accessor - '$property' is not a valid name!" ) | ||||||||
|
Branch: 1 branch
| |||||||||||
| 57 | unless $class->isa_valid_name( $property ); | ||||||||||
| 58 | 13 | ▶ | warn( "creating " . $class->style . " accessor: $accessor\n" ) if | ||||||||
|
Branch: 1 branch
| |||||||||||
| 59 | $class->Debug > 1; | ||||||||||
| 60 | 13 | $class->create_accessor( $accessor, $property ); | |||||||||
| 61 | } | ||||||||||
| 62 | |||||||||||
| 63 | 8 | return $class; | |||||||||
| 64 | } | ||||||||||
| 65 | |||||||||||
| 66 | 5 | ▶ | sub create_accessor { | ||||||||
|
Subroutine
create_accessor: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||
| 67 | 5 | my ($class, $accessor, $property) = @_; | |||||||||
| 68 | 5 | $property = "-$property"; | |||||||||
| 69 | # set/get is slightly faster if we eval instead of using a closure + anon | ||||||||||
| 70 | # sub, but the difference is marginal (~5%), and this uses less memory... | ||||||||||
| 71 | 7 | ▶ | no strict 'refs'; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 72 | 5 | ▶ | *{$accessor} = sub { | ||||||||
|
Subroutine
__ANON__: called CC=2 SCAR=6.9
| |||||||||||
| 73 | (@_ > 1) | ||||||||||
| 74 | ? ($_[0]->{$property} = $_[1], return $_[0]) | ||||||||||
| 75 | 7 | ▶ | : $_[0]->{$property}; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 76 | 5 | }; | |||||||||
| 77 | } | ||||||||||
| 78 | |||||||||||
| 79 | 21 | ▶ | sub isa_valid_name { | ||||||||
|
Subroutine
isa_valid_name: called CC=3 SCAR=11.0
Pod
undocumented
| |||||||||||
| 80 | 21 | my ($class, $property) = @_; | |||||||||
| 81 | 21 | ▶ | return unless $property =~ /^(?!\d)\w+$/; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 82 | 19 | ▶ | return if grep {$property eq $_} $class->InvalidNames; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 83 | 13 | return 1; | |||||||||
| 84 | } | ||||||||||
| 85 | |||||||||||
| 86 | ## | ||||||||||
| 87 | ## on the off-chance that someone will sub-class: | ||||||||||
| 88 | ## | ||||||||||
| 89 | |||||||||||
| 90 | ## don't like studly caps for sub-names, but stick with Exporter-like style... | ||||||||||
| 91 | 29 | ▶ | sub Debug { $Debug; } | ||||||||
|
Subroutine
Debug: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||
| 92 | 23 | ▶ | sub ExportLevel { $ExportLevel } | ||||||||
|
Subroutine
ExportLevel: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||
| 93 | 19 | ▶ | sub InvalidNames { @InvalidNames } | ||||||||
|
Subroutine
InvalidNames: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||
| 94 | |||||||||||
| 95 | 7 | 1; | |||||||||
| 96 | |||||||||||
| 97 | __END__ | ||||||||||