| 1 | package HTML::Template::HashWrapper; | ||||||||||
| 2 | 1 | ▶ | use strict; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 3 | 1 | ▶ | use Carp 'croak'; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 4 | |||||||||||
| 5 | 1 | our $VERSION = '1.3'; | |||||||||
| 6 | |||||||||||
| 7 | 10 | ▶ | sub new { | ||||||||
|
Subroutine
new: called CC=4 SCAR=13.9
Pod
undocumented
| |||||||||||
| 8 | 10 | my $class = shift; | |||||||||
| 9 | 10 | my $wrapped = shift; | |||||||||
| 10 | 10 | ▶ | unless ( UNIVERSAL::isa($wrapped, 'HASH') ) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 11 | 4 | croak "Wrapped object is not a hash reference"; | |||||||||
| 12 | } | ||||||||||
| 13 | 6 | my %args = @_; | |||||||||
| 14 | 6 | my $at_isa = $class; | |||||||||
| 15 | 6 | my $pkgname = $class->_GENERATE_PACKAGENAME(); | |||||||||
| 16 | 6 | ▶ | if ( UNIVERSAL::isa( $wrapped, 'UNIVERSAL' ) ) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 17 | # $wrapped is already blessed: add its ref to @ISA | ||||||||||
| 18 | 2 | $at_isa .= " " . ref($wrapped); | |||||||||
| 19 | } | ||||||||||
| 20 | 1 | ▶ | eval "{package $pkgname; use strict; our \@ISA=qw($at_isa); 1;}"; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
Subroutine
BEGIN: called CC=1 SCAR=0.0
Subroutine
BEGIN: called CC=1 SCAR=0.0
Subroutine
BEGIN: called CC=1 SCAR=0.0
Subroutine
BEGIN: called CC=1 SCAR=0.0
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 21 | 6 | ▶ | die $@ if $@; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 22 | 6 | return bless $wrapped, $pkgname; | |||||||||
| 23 | } | ||||||||||
| 24 | |||||||||||
| 25 | # If you don't like my anonymous packagename generation, you can roll your own. | ||||||||||
| 26 | 6 | ▶ | sub _GENERATE_PACKAGENAME { | ||||||||
|
Subroutine
_GENERATE_PACKAGENAME: called CC=1 SCAR=0.0
| |||||||||||
| 27 | 6 | my $class = shift; | |||||||||
| 28 | 6 | my $uniq = "ANON_".$$.time().int(rand()*10000); | |||||||||
| 29 | 6 | my $pkgname = "$ {class}::$ {uniq}"; | |||||||||
| 30 | 6 | return $pkgname; | |||||||||
| 31 | } | ||||||||||
| 32 | |||||||||||
| 33 | # todo: according to H::T, param() can also support: | ||||||||||
| 34 | # set multiple params: hash input | ||||||||||
| 35 | # set multuple params from a hashref input | ||||||||||
| 36 | |||||||||||
| 37 | # Standard behavior: $self is a hashref | ||||||||||
| 38 | 24 | ▶ | sub param { | ||||||||
|
Subroutine
param: called CC=3 SCAR=11.0
Pod
undocumented
| |||||||||||
| 39 | 24 | my $self = shift; | |||||||||
| 40 | 24 | my($name, $value) = @_; | |||||||||
| 41 | 24 | ▶ | if ( defined($name) ) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 42 | 16 | ▶ | if (defined($value)) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 43 | 4 | return $self->{$name} = $value; | |||||||||
| 44 | } else { | ||||||||||
| 45 | 12 | return $self->{$name}; | |||||||||
| 46 | } | ||||||||||
| 47 | } else { | ||||||||||
| 48 | 8 | return keys %{$self}; | |||||||||
| 49 | } | ||||||||||
| 50 | } | ||||||||||
| 51 | |||||||||||
| 52 | |||||||||||
| 53 | 1 | 1; | |||||||||
| 54 | |||||||||||
| 55 | package HTML::Template::HashWrapper::Plain; | ||||||||||
| 56 | 1 | ▶ | use strict; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 57 | 1 | ▶ | use Carp 'croak'; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 58 | 1 | our @ISA=('HTML::Template::HashWrapper'); # everything is overridden, though | |||||||||
| 59 | |||||||||||
| 60 | 8 | ▶ | sub new { | ||||||||
|
Subroutine
new: called CC=2 SCAR=6.9
| |||||||||||
| 61 | 8 | my $class = shift; | |||||||||
| 62 | 8 | my $target = shift; | |||||||||
| 63 | 8 | ▶ | unless ( UNIVERSAL::isa($target, 'HASH') ) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 64 | 4 | croak "Wrapped object is not a hash reference"; | |||||||||
| 65 | } | ||||||||||
| 66 | 4 | return bless { _ref => $target }, $class; | |||||||||
| 67 | } | ||||||||||
| 68 | |||||||||||
| 69 | # Un-reblessing behavior: $self contains a reference to the reference | ||||||||||
| 70 | 24 | ▶ | sub param { | ||||||||
|
Subroutine
param: called CC=3 SCAR=11.0
| |||||||||||
| 71 | 24 | my $self = shift; | |||||||||
| 72 | 24 | my($name, $value) = @_; | |||||||||
| 73 | 24 | ▶ | if ( defined($name) ) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 74 | 16 | ▶ | if (defined($value)) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 75 | 4 | return $self->{_ref}->{$name} = $value; | |||||||||
| 76 | } else { | ||||||||||
| 77 | 12 | return $self->{_ref}->{$name}; | |||||||||
| 78 | } | ||||||||||
| 79 | } else { | ||||||||||
| 80 | 8 | return keys %{ $self->{_ref} }; | |||||||||
| 81 | } | ||||||||||
| 82 | } | ||||||||||
| 83 | |||||||||||
| 84 | 1 | 1; | |||||||||
| 85 | __END__ | ||||||||||