| 1 | package Accessor; | ||||||||||
| 2 | |||||||||||
| 3 | =head1 NAME | ||||||||||
| 4 | |||||||||||
| 5 | Accessor - base class for accessor-pattern classes | ||||||||||
| 6 | |||||||||||
| 7 | =head1 USAGE | ||||||||||
| 8 | |||||||||||
| 9 | my $obj = new Hash(myattr => 1); | ||||||||||
| 10 | |||||||||||
| 11 | say $obj->myattr(); # get myattr, say 1 | ||||||||||
| 12 | say $obj->myattr(0); # set myattr to 0 | ||||||||||
| 13 | |||||||||||
| 14 | package Hash; | ||||||||||
| 15 | |||||||||||
| 16 | use base 'Accessor'; | ||||||||||
| 17 | |||||||||||
| 18 | sub new { | ||||||||||
| 19 | my $class = shift; | ||||||||||
| 20 | return bless {@_}, $class; | ||||||||||
| 21 | } | ||||||||||
| 22 | |||||||||||
| 23 | =head1 VERSION | ||||||||||
| 24 | |||||||||||
| 25 | 0.5 | ||||||||||
| 26 | |||||||||||
| 27 | =head1 AUTHOR | ||||||||||
| 28 | |||||||||||
| 29 | Developed by Ivan Trunaev | ||||||||||
| 30 | |||||||||||
| 31 | Contacts <ivan@trunaev.ru> | ||||||||||
| 32 | |||||||||||
| 33 | =cut | ||||||||||
| 34 | |||||||||||
| 35 | |||||||||||
| 36 | 3 | ▶ | use strict; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 37 | 3 | ▶ | use warnings; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 38 | |||||||||||
| 39 | 3 | our $VERSION = '0.5'; | |||||||||
| 40 | |||||||||||
| 41 | 3 | our $AUTOLOAD; | |||||||||
| 42 | |||||||||||
| 43 | 6 | ▶ | sub AUTOLOAD { | ||||||||
|
Subroutine
AUTOLOAD: called CC=2 SCAR=6.9
| |||||||||||
| 44 | 6 | my $self = shift; | |||||||||
| 45 | |||||||||||
| 46 | 6 | my $name = $AUTOLOAD; | |||||||||
| 47 | 6 | ▶ | return if $name =~ /^.*::[A-Z]+$/; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 48 | 2 | $name =~ s/^.*:://; # strip fully-qualified portion | |||||||||
| 49 | |||||||||||
| 50 | |||||||||||
| 51 | 5 | ▶ | my $sub = sub { | ||||||||
|
Subroutine
__ANON__: called CC=2 SCAR=6.9
| |||||||||||
| 52 | 5 | my $self = shift; | |||||||||
| 53 | 5 | ▶ | if (scalar @_) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 54 | 2 | my $val = shift; | |||||||||
| 55 | 2 | $self->{$name} = $val; | |||||||||
| 56 | } | ||||||||||
| 57 | 5 | return $self->{$name}; | |||||||||
| 58 | 2 | }; | |||||||||
| 59 | |||||||||||
| 60 | 3 | ▶ | no strict 'refs'; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 61 | 2 | *{$AUTOLOAD} = $sub; | |||||||||
| 62 | |||||||||||
| 63 | 2 | return $sub->($self, @_); | |||||||||
| 64 | } | ||||||||||
| 65 | |||||||||||
| 66 | 3 | 1; | |||||||||