| 1 | package Hash::AutoHash::AVPairsSingle; | ||||||||||||||
| 2 | 9 | our $VERSION='1.17_01'; | |||||||||||||
| 3 | 9 | $VERSION=eval $VERSION; # I think this is the accepted idiom.. | |||||||||||||
| 4 | |||||||||||||||
| 5 | ################################################################################# | ||||||||||||||
| 6 | # | ||||||||||||||
| 7 | # Author: Nat Goodman | ||||||||||||||
| 8 | # Created: 09-03-05 | ||||||||||||||
| 9 | # $Id: | ||||||||||||||
| 10 | # | ||||||||||||||
| 11 | # AutoHash with single-valued, string or number elements. no references | ||||||||||||||
| 12 | # | ||||||||||||||
| 13 | ################################################################################# | ||||||||||||||
| 14 | 9 | ▶ | use strict; | ||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||
| 15 | 9 | ▶ | use Carp; | ||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||
| 16 | 9 | ▶ | use Hash::AutoHash; | ||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||
| 17 | 9 | ▶ | use base qw(Hash::AutoHash); | ||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||
| 18 | |||||||||||||||
| 19 | 9 | our @NORMAL_EXPORT_OK=@Hash::AutoHash::EXPORT_OK; | |||||||||||||
| 20 | 9 | my $helper_class=__PACKAGE__.'::helper'; | |||||||||||||
| 21 | 9 | our @EXPORT_OK=$helper_class->EXPORT_OK; | |||||||||||||
| 22 | 9 | our @SUBCLASS_EXPORT_OK=$helper_class->SUBCLASS_EXPORT_OK; | |||||||||||||
| 23 | |||||||||||||||
| 24 | ################################################################################# | ||||||||||||||
| 25 | # helper package exists to avoid polluting Hash::AutoHash::Args namespace with | ||||||||||||||
| 26 | # subs that would mask accessor/mutator AUTOLOADs | ||||||||||||||
| 27 | # functions herein (except _new) are exportable by Hash::AutoHash::Args | ||||||||||||||
| 28 | ################################################################################# | ||||||||||||||
| 29 | package Hash::AutoHash::AVPairsSingle::helper; | ||||||||||||||
| 30 | 9 | our $VERSION=$Hash::AutoHash::AVPairsSingle::VERSION; | |||||||||||||
| 31 | 9 | ▶ | use strict; | ||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||
| 32 | 9 | ▶ | use Carp; | ||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||
| 33 | BEGIN { | ||||||||||||||
| 34 | 9 | ▶ | our @ISA=qw(Hash::AutoHash::helper); | ||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||
| 35 | } | ||||||||||||||
| 36 | 9 | ▶ | use Hash::AutoHash qw(autohash_tie); | ||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||
| 37 | |||||||||||||||
| 38 | sub _new { | ||||||||||||||
| 39 | 29 | ▶ | my($helper_class,$class,@args)=@_; | ||||||||||||
|
Subroutine
_new: called CC=1 SCAR=0.0
| |||||||||||||||
| 40 | 29 | my $self=autohash_tie Hash::AutoHash::AVPairsSingle::tie,@args; | |||||||||||||
| 41 | 29 | bless $self,$class; | |||||||||||||
| 42 | } | ||||||||||||||
| 43 | |||||||||||||||
| 44 | ################################################################################# | ||||||||||||||
| 45 | # Tied hash which implements Hash::AutoHash::MultiValued | ||||||||||||||
| 46 | ################################################################################# | ||||||||||||||
| 47 | package Hash::AutoHash::AVPairsSingle::tie; | ||||||||||||||
| 48 | 9 | our $VERSION=$Hash::AutoHash::AVPairsSingle::VERSION; | |||||||||||||
| 49 | 9 | ▶ | use strict; | ||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||
| 50 | 9 | ▶ | use Carp; | ||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||
| 51 | 9 | ▶ | use Tie::Hash; | ||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||
| 52 | 9 | our @ISA=qw(Tie::ExtraHash); | |||||||||||||
| 53 | 9 | ▶ | use constant STORAGE=>0; | ||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||
| 54 | |||||||||||||||
| 55 | sub TIEHASH { | ||||||||||||||
| 56 | 29 | ▶ | my($class,@hash)=@_; | ||||||||||||
|
Subroutine
TIEHASH: called CC=5 SCAR=16.1
| |||||||||||||||
| 57 | 29 | my $self=bless [{}],$class; | |||||||||||||
| 58 | 29 | ▶ | if (@hash==1) { # flatten if ARRAY or HASH | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 59 | 4 | my $hash=shift @hash; | |||||||||||||
| 60 | 4 | ▶ | @hash=('ARRAY' eq ref $hash)? @$hash: ('HASH' eq ref $hash)? %$hash: (); | ||||||||||||
|
Branch: 2 branches
| |||||||||||||||
| 61 | } | ||||||||||||||
| 62 | 29 | ▶ | while (@hash>1) { # store initial values | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 63 | 43 | my($key,$value)=splice @hash,0,2; # shift 1st two elements | |||||||||||||
| 64 | 43 | $self->STORE($key,$value); | |||||||||||||
| 65 | } | ||||||||||||||
| 66 | 29 | $self; | |||||||||||||
| 67 | } | ||||||||||||||
| 68 | sub STORE { | ||||||||||||||
| 69 | 84 | ▶ | my($self,$key,$new)=@_; | ||||||||||||
|
Subroutine
STORE: called CC=2 SCAR=6.9
| |||||||||||||||
| 70 | 84 | ▶ | confess "Trying to store reference as value of attribute $key" if ref $new; | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 71 | 83 | $self->[STORAGE]->{$key}=$new; | |||||||||||||
| 72 | } | ||||||||||||||
| 73 | 9 | 1; | |||||||||||||
| 74 | |||||||||||||||
| 75 | __END__ | ||||||||||||||