| 1 | # $Id: /mirror/coderepos/lang/perl/Class-Data-ConfigHash/trunk/lib/Class/Data/ConfigHash.pm 69719 2008-08-27T02:20:26.319462Z daisuke $ | ||||||||||||||||||||||||||||
| 2 | |||||||||||||||||||||||||||||
| 3 | package Class::Data::ConfigHash; | ||||||||||||||||||||||||||||
| 4 | 2 | ▶ | use strict; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 5 | 2 | ▶ | use warnings; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 6 | 2 | ▶ | use base qw(Class::Data::Inheritable); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 7 | 2 | our $VERSION = '0.00002'; | |||||||||||||||||||||||||||
| 8 | |||||||||||||||||||||||||||||
| 9 | 2 | __PACKAGE__->mk_classdata(_config => {}); | |||||||||||||||||||||||||||
| 10 | |||||||||||||||||||||||||||||
| 11 | 16 | ▶ | sub config { | ||||||||||||||||||||||||||
|
Subroutine
config: called CC=6 SCAR=17.9
Pod
documented
| |||||||||||||||||||||||||||||
| 12 | 16 | my $self = shift; | |||||||||||||||||||||||||||
| 13 | 16 | my $config_sub = $self->can('_config'); | |||||||||||||||||||||||||||
| 14 | 16 | ▶ | my $config = $self->$config_sub() || {}; | ||||||||||||||||||||||||||
|
Condition50%1/2
$self->$config_sub || {}
MC/DC0%0/1
$self->$config_sub || {}
$self->$config_sub
Truth table50%1/2
A
A= $self->$config_sub
| |||||||||||||||||||||||||||||
| 15 | 16 | ▶ | if (@_) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 16 | 5 | ▶ | my $newconfig = { %{@_ > 1 ? {@_} : ($_[0] || {})} }; | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%2/2
$_[0] or {}
MC/DC100%1/1
$_[0] or {}
$_[0]
Truth table100%2/2
A
A= $_[0]
| |||||||||||||||||||||||||||||
| 17 | 5 | $self->_config( | |||||||||||||||||||||||||||
| 18 | $self->merge_config_hashes( $config, $newconfig ) | ||||||||||||||||||||||||||||
| 19 | ); | ||||||||||||||||||||||||||||
| 20 | } else { | ||||||||||||||||||||||||||||
| 21 | # this is a bit of a kludge, required to make | ||||||||||||||||||||||||||||
| 22 | # __PACKAGE__->config->{foo} = 'bar'; | ||||||||||||||||||||||||||||
| 23 | # work in a subclass. Calling the Class::Data::Inheritable setter | ||||||||||||||||||||||||||||
| 24 | # will create a new _config method in the current class if it's | ||||||||||||||||||||||||||||
| 25 | # currently inherited from the superclass. So, the can() call will | ||||||||||||||||||||||||||||
| 26 | # return a different subref in that case and that means we know to | ||||||||||||||||||||||||||||
| 27 | # copy and reset the value stored in the class data. | ||||||||||||||||||||||||||||
| 28 | |||||||||||||||||||||||||||||
| 29 | 11 | $self->_config( $config ); | |||||||||||||||||||||||||||
| 30 | |||||||||||||||||||||||||||||
| 31 | 11 | ▶ | if ((my $config_sub_now = $self->can('_config')) ne $config_sub) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 32 | |||||||||||||||||||||||||||||
| 33 | 1 | $config = $self->merge_config_hashes( $config, {} ); | |||||||||||||||||||||||||||
| 34 | 1 | $self->$config_sub_now( $config ); | |||||||||||||||||||||||||||
| 35 | } | ||||||||||||||||||||||||||||
| 36 | } | ||||||||||||||||||||||||||||
| 37 | 16 | return $config; | |||||||||||||||||||||||||||
| 38 | } | ||||||||||||||||||||||||||||
| 39 | |||||||||||||||||||||||||||||
| 40 | sub merge_config_hashes | ||||||||||||||||||||||||||||
| 41 | 6 | ▶ | { | ||||||||||||||||||||||||||
|
Subroutine
merge_config_hashes: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||
| 42 | 6 | my ($self, $lefthash, $righthash) = @_; | |||||||||||||||||||||||||||
| 43 | 6 | return __merge_hashes($lefthash, $righthash); | |||||||||||||||||||||||||||
| 44 | } | ||||||||||||||||||||||||||||
| 45 | |||||||||||||||||||||||||||||
| 46 | sub __merge_hashes | ||||||||||||||||||||||||||||
| 47 | 6 | ▶ | { | ||||||||||||||||||||||||||
|
Subroutine
__merge_hashes: called CC=9 SCAR=24.0
| |||||||||||||||||||||||||||||
| 48 | # XXX - If Catalyst is in effect, we might just as well use | ||||||||||||||||||||||||||||
| 49 | # Catalyst::Utils::merge_hashes, I suppose. | ||||||||||||||||||||||||||||
| 50 | 6 | my ( $lefthash, $righthash ) = @_; | |||||||||||||||||||||||||||
| 51 | |||||||||||||||||||||||||||||
| 52 | 6 | ▶ | if ( !defined $righthash ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 53 | 0 | return $lefthash; | |||||||||||||||||||||||||||
| 54 | } | ||||||||||||||||||||||||||||
| 55 | |||||||||||||||||||||||||||||
| 56 | 6 | ▶ | if ( !defined $lefthash ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 57 | 0 | return $righthash; | |||||||||||||||||||||||||||
| 58 | } | ||||||||||||||||||||||||||||
| 59 | |||||||||||||||||||||||||||||
| 60 | 6 | my %merged = %{$lefthash}; | |||||||||||||||||||||||||||
| 61 | 6 | for my $key ( keys %{$righthash} ) { | |||||||||||||||||||||||||||
| 62 | 6 | ▶ | my $right_ref = ( ref $righthash->{$key} || '' ) eq 'HASH'; | ||||||||||||||||||||||||||
|
Condition100%2/2
ref $righthash->{$key} || ''
MC/DC100%1/1
ref $righthash->{$key} || ''
ref $righthash->{$key}
Truth table100%2/2
A
A= ref $righthash->{$key}
| |||||||||||||||||||||||||||||
| 63 | my $left_ref = | ||||||||||||||||||||||||||||
| 64 | 6 | ▶ | ( ( exists $lefthash->{$key} && ref $lefthash->{$key} ) || '' ) eq | ||||||||||||||||||||||||||
|
Condition60%3/5
exists $lefthash->{$key} && ref $lefthash->{$key} || ''
exists $lefthash->{$key} and ref $lefthash->{$key}
MC/DC0%0/2
exists $lefthash->{$key} && ref $lefthash->{$key} || ''
exists $lefthash->{$key}
ref $lefthash->{$key}
Truth table66%2/3
A and B
A= exists $lefthash->{$key}B= ref $lefthash->{$key}
| |||||||||||||||||||||||||||||
| 65 | 'HASH'; | ||||||||||||||||||||||||||||
| 66 | 6 | ▶ | if ( $right_ref and $left_ref ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%2/3
$right_ref and $left_ref
MC/DC0%0/2
$right_ref and $left_ref
$right_ref
$left_ref
Truth table66%2/3
A and B
A= $right_refB= $left_ref
| |||||||||||||||||||||||||||||
| 67 | $merged{$key} = | ||||||||||||||||||||||||||||
| 68 | 0 | __merge_hashes( $lefthash->{$key}, $righthash->{$key} ); | |||||||||||||||||||||||||||
| 69 | } | ||||||||||||||||||||||||||||
| 70 | else { | ||||||||||||||||||||||||||||
| 71 | 6 | $merged{$key} = $righthash->{$key}; | |||||||||||||||||||||||||||
| 72 | } | ||||||||||||||||||||||||||||
| 73 | } | ||||||||||||||||||||||||||||
| 74 | |||||||||||||||||||||||||||||
| 75 | 6 | return \%merged; | |||||||||||||||||||||||||||
| 76 | } | ||||||||||||||||||||||||||||
| 77 | |||||||||||||||||||||||||||||
| 78 | 2 | 1; | |||||||||||||||||||||||||||
| 79 | |||||||||||||||||||||||||||||
| 80 | __END__ | ||||||||||||||||||||||||||||