| 1 | package Accessor; | ||||||||||
| 2 | |||||||||||
| 3 | =head1 NAME | ||||||||||
| 4 | |||||||||||
| 5 | Accessor - base class for accessor-pattern classes | ||||||||||
| 6 | |||||||||||
| 7 | =head1 USAGE | ||||||||||
| 8 | |||||||||||
| 9 | my $h = new Hash(asd => 111); | ||||||||||
| 10 | |||||||||||
| 11 | warn $h->asd(); # get values | ||||||||||
| 12 | warn $h->asd('ad'); # set values | ||||||||||
| 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.3 | ||||||||||
| 26 | |||||||||||
| 27 | =head1 AUTHOR | ||||||||||
| 28 | |||||||||||
| 29 | All code writen by Ivan Trunaev. | ||||||||||
| 30 | |||||||||||
| 31 | Contacts <ivan@trunaev.ru> | ||||||||||
| 32 | |||||||||||
| 33 | =cut | ||||||||||
| 34 | |||||||||||
| 35 | |||||||||||
| 36 | 1 | ▶ | use strict; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 37 | 1 | ▶ | use warnings; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 38 | |||||||||||
| 39 | 1 | our $VERSION = '0.3'; | |||||||||
| 40 | |||||||||||
| 41 | 1 | our $AUTOLOAD; | |||||||||
| 42 | |||||||||||
| 43 | 0 | ▶ | sub AUTOLOAD { | ||||||||
|
Subroutine
AUTOLOAD: not called CC=4 SCAR=27.1
| |||||||||||
| 44 | 0 | my $self = shift; | |||||||||
| 45 | 0 | my $val = shift; | |||||||||
| 46 | |||||||||||
| 47 | 0 | my $name = $AUTOLOAD; | |||||||||
| 48 | 0 | ▶ | return if $name =~ /^.*::[A-Z]+$/; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 49 | 0 | $name =~ s/^.*:://; # strip fully-qualified portion | |||||||||
| 50 | |||||||||||
| 51 | 0 | ▶ | my $sub = sub { | ||||||||
|
Subroutine
__ANON__: not called CC=2 SCAR=17.9
| |||||||||||
| 52 | 0 | my $self = shift; | |||||||||
| 53 | 0 | ▶ | if (scalar @_) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 54 | 0 | my $val = shift; | |||||||||
| 55 | 0 | $self->{$name} = $val; | |||||||||
| 56 | } | ||||||||||
| 57 | 0 | return $self->{$name}; | |||||||||
| 58 | 0 | }; | |||||||||
| 59 | |||||||||||
| 60 | 0 | ▶ | return unless $self->{$name}; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 61 | |||||||||||
| 62 | 1 | ▶ | no strict 'refs'; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 63 | 0 | *{$AUTOLOAD} = $sub; | |||||||||
| 64 | |||||||||||
| 65 | 0 | ▶ | return $val ? $sub->($self, $val) : $sub->($self); | ||||||||
|
Branch: 1 branch
| |||||||||||
| 66 | } | ||||||||||
| 67 | |||||||||||
| 68 | 1 | 1; | |||||||||