| 1 | # | ||||||||||||||||||||||||||||
| 2 | # $Id: Hash.pm 2000 2015-01-13 18:24:09Z gomor $ | ||||||||||||||||||||||||||||
| 3 | # | ||||||||||||||||||||||||||||
| 4 | package Class::Gomor::Hash; | ||||||||||||||||||||||||||||
| 5 | 7 | ▶ | use strict; use warnings; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 6 | |||||||||||||||||||||||||||||
| 7 | 7 | our $VERSION = '1.03'; | |||||||||||||||||||||||||||
| 8 | |||||||||||||||||||||||||||||
| 9 | 7 | ▶ | use Class::Gomor; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 10 | 7 | ▶ | use base qw(Class::Gomor); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 11 | |||||||||||||||||||||||||||||
| 12 | 7 | ▶ | use Data::Dumper; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 13 | |||||||||||||||||||||||||||||
| 14 | 4 | ▶ | sub new { | ||||||||||||||||||||||||||
|
Subroutine
new: called CC=3 SCAR=11.2
Pod
documented
| |||||||||||||||||||||||||||||
| 15 | 4 | my $self = shift; | |||||||||||||||||||||||||||
| 16 | 4 | ▶ | my $class = ref($self) || $self; | ||||||||||||||||||||||||||
|
Condition33%1/3
ref $self || $self
MC/DC0%0/2
ref $self || $self
ref $self
$self
Truth table33%1/3
A || B
A= ref $selfB= $self
| |||||||||||||||||||||||||||||
| 17 | 4 | my %h = @_; | |||||||||||||||||||||||||||
| 18 | 4 | ▶ | $class->cgCheckParams(\%h, $class->cgGetAttributes) | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 19 | unless $Class::Gomor::NoCheck; | ||||||||||||||||||||||||||||
| 20 | 4 | bless(\%h, $class); | |||||||||||||||||||||||||||
| 21 | } | ||||||||||||||||||||||||||||
| 22 | |||||||||||||||||||||||||||||
| 23 | # Just for compatibility with Class::Gomor::Array | ||||||||||||||||||||||||||||
| 24 | # And in order to make it easy to switch for one to another | ||||||||||||||||||||||||||||
| 25 | 0 | ▶ | sub cgGetIndice { shift; shift } | ||||||||||||||||||||||||||
|
Subroutine
cgGetIndice: not called CC=1 SCAR=6.9
Pod
documented
| |||||||||||||||||||||||||||||
| 26 | 0 | ▶ | sub cgBuildIndices {} | ||||||||||||||||||||||||||
|
Subroutine
cgBuildIndices: not called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||
| 27 | |||||||||||||||||||||||||||||
| 28 | 11 | ▶ | sub cgFullClone { | ||||||||||||||||||||||||||
|
Subroutine
cgFullClone: called CC=6 SCAR=18.5
Pod
documented
| |||||||||||||||||||||||||||||
| 29 | 11 | my $self = shift; | |||||||||||||||||||||||||||
| 30 | 11 | my ($n) = @_; | |||||||||||||||||||||||||||
| 31 | 11 | ▶ | return $self->SUPER::cgFullClone($n) if $n; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 32 | 10 | ▶ | my $class = ref($self) || $self; | ||||||||||||||||||||||||||
|
Condition33%1/3
ref $self || $self
MC/DC0%0/2
ref $self || $self
ref $self
$self
Truth table33%1/3
A || B
A= ref $selfB= $self
| |||||||||||||||||||||||||||||
| 33 | 10 | my %new; | |||||||||||||||||||||||||||
| 34 | 10 | for my $k (keys %$self) { | |||||||||||||||||||||||||||
| 35 | 30 | my $v = $self->{$k}; | |||||||||||||||||||||||||||
| 36 | (ref($v) && UNIVERSAL::isa($v, 'Class::Gomor')) | ||||||||||||||||||||||||||||
| 37 | ? $new{$k} = $v->cgFullClone | ||||||||||||||||||||||||||||
| 38 | 30 | ▶ | : $new{$k} = $v; | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%2/3
ref $v and UNIVERSAL::isa($v, "Class::Gomor")
MC/DC0%0/2
ref $v and UNIVERSAL::isa($v, "Class::Gomor")
ref $v
UNIVERSAL::isa($v, "Class::Gomor")
Truth table66%2/3
A and B
A= ref $vB= UNIVERSAL::isa($v, "Class::Gomor")
| |||||||||||||||||||||||||||||
| 39 | } | ||||||||||||||||||||||||||||
| 40 | 10 | bless(\%new, $class); | |||||||||||||||||||||||||||
| 41 | } | ||||||||||||||||||||||||||||
| 42 | |||||||||||||||||||||||||||||
| 43 | 0 | ▶ | sub cgDumper { Dumper(shift()) } | ||||||||||||||||||||||||||
|
Subroutine
cgDumper: not called CC=1 SCAR=6.9
Pod
documented
| |||||||||||||||||||||||||||||
| 44 | |||||||||||||||||||||||||||||
| 45 | 17 | ▶ | sub _cgAccessorScalar { | ||||||||||||||||||||||||||
|
Subroutine
_cgAccessorScalar: called CC=2 SCAR=6.9
| |||||||||||||||||||||||||||||
| 46 | 17 | my ($self, $sca) = (shift, shift); | |||||||||||||||||||||||||||
| 47 | @_ ? $self->{$sca} = shift | ||||||||||||||||||||||||||||
| 48 | 17 | ▶ | : $self->{$sca}; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 49 | } | ||||||||||||||||||||||||||||
| 50 | |||||||||||||||||||||||||||||
| 51 | 8 | ▶ | sub _cgAccessorArray { | ||||||||||||||||||||||||||
|
Subroutine
_cgAccessorArray: called CC=2 SCAR=6.9
| |||||||||||||||||||||||||||||
| 52 | 8 | my ($self, $ary) = (shift, shift); | |||||||||||||||||||||||||||
| 53 | @_ ? $self->{$ary} = shift | ||||||||||||||||||||||||||||
| 54 | 8 | ▶ | : @{$self->{$ary}}; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 55 | } | ||||||||||||||||||||||||||||
| 56 | |||||||||||||||||||||||||||||
| 57 | 7 | 1; | |||||||||||||||||||||||||||
| 58 | |||||||||||||||||||||||||||||
| 59 | =head1 NAME | ||||||||||||||||||||||||||||
| 60 | |||||||||||||||||||||||||||||
| 61 | Class::Gomor::Hash - class and object builder, hash version | ||||||||||||||||||||||||||||
| 62 | |||||||||||||||||||||||||||||
| 63 | =head1 SYNPOSIS | ||||||||||||||||||||||||||||
| 64 | |||||||||||||||||||||||||||||
| 65 | # Create a base class in BaseClass.pm | ||||||||||||||||||||||||||||
| 66 | package My::BaseClass; | ||||||||||||||||||||||||||||
| 67 | |||||||||||||||||||||||||||||
| 68 | require Class::Gomor::Hash; | ||||||||||||||||||||||||||||
| 69 | our @ISA = qw(Class::Gomor::Hash); | ||||||||||||||||||||||||||||
| 70 | |||||||||||||||||||||||||||||
| 71 | our @AS = qw(attribute1 attribute2); | ||||||||||||||||||||||||||||
| 72 | our @AA = qw(attribute3 attribute4); | ||||||||||||||||||||||||||||
| 73 | our @AO = qw(other); | ||||||||||||||||||||||||||||
| 74 | |||||||||||||||||||||||||||||
| 75 | # You should initialize yourself array attributes | ||||||||||||||||||||||||||||
| 76 | sub new { shift->SUPER::new(attribute3 => [], attribute4 => [], @_) } | ||||||||||||||||||||||||||||
| 77 | |||||||||||||||||||||||||||||
| 78 | # Create accessors | ||||||||||||||||||||||||||||
| 79 | My::BaseClass->cgBuildAccessorsScalar(\@AS); | ||||||||||||||||||||||||||||
| 80 | My::BaseClass->cgBuildAccessorsArray(\@AA); | ||||||||||||||||||||||||||||
| 81 | |||||||||||||||||||||||||||||
| 82 | sub other { | ||||||||||||||||||||||||||||
| 83 | my $self = shift; | ||||||||||||||||||||||||||||
| 84 | @_ ? $self->{'other'} = [ split(/\n/, shift) ] | ||||||||||||||||||||||||||||
| 85 | : @{$self->{'other'}}; | ||||||||||||||||||||||||||||
| 86 | } | ||||||||||||||||||||||||||||
| 87 | |||||||||||||||||||||||||||||
| 88 | 1; | ||||||||||||||||||||||||||||
| 89 | |||||||||||||||||||||||||||||
| 90 | # Create a subclass in SubClass.pm | ||||||||||||||||||||||||||||
| 91 | package My::SubClass; | ||||||||||||||||||||||||||||
| 92 | |||||||||||||||||||||||||||||
| 93 | require My::BaseClass; | ||||||||||||||||||||||||||||
| 94 | our @ISA = qw(My::BaseClass); | ||||||||||||||||||||||||||||
| 95 | |||||||||||||||||||||||||||||
| 96 | our @AS = qw(subclassAttribute); | ||||||||||||||||||||||||||||
| 97 | |||||||||||||||||||||||||||||
| 98 | My::SubClass->cgBuildAccessorsScalar(\@AS); | ||||||||||||||||||||||||||||
| 99 | |||||||||||||||||||||||||||||
| 100 | sub new { | ||||||||||||||||||||||||||||
| 101 | shift->SUPER::new( | ||||||||||||||||||||||||||||
| 102 | attribute1 => 'val1', | ||||||||||||||||||||||||||||
| 103 | attribute2 => 'val2', | ||||||||||||||||||||||||||||
| 104 | attribute3 => [ 'val3', ], | ||||||||||||||||||||||||||||
| 105 | attribute4 => [ 'val4', ], | ||||||||||||||||||||||||||||
| 106 | other => [ 'none', ], | ||||||||||||||||||||||||||||
| 107 | subclassAttribute => 'subVal', | ||||||||||||||||||||||||||||
| 108 | ); | ||||||||||||||||||||||||||||
| 109 | } | ||||||||||||||||||||||||||||
| 110 | |||||||||||||||||||||||||||||
| 111 | 1; | ||||||||||||||||||||||||||||
| 112 | |||||||||||||||||||||||||||||
| 113 | # A program using those classes | ||||||||||||||||||||||||||||
| 114 | |||||||||||||||||||||||||||||
| 115 | my $new = My::SubClass->new; | ||||||||||||||||||||||||||||
| 116 | |||||||||||||||||||||||||||||
| 117 | my $val1 = $new->attribute1; | ||||||||||||||||||||||||||||
| 118 | my @values3 = $new->attribute3; | ||||||||||||||||||||||||||||
| 119 | my @otherOld = $new->other; | ||||||||||||||||||||||||||||
| 120 | |||||||||||||||||||||||||||||
| 121 | $new->other("str1\nstr2\nstr3"); | ||||||||||||||||||||||||||||
| 122 | my @otherNew = $new->other; | ||||||||||||||||||||||||||||
| 123 | print "@otherNew\n"; | ||||||||||||||||||||||||||||
| 124 | |||||||||||||||||||||||||||||
| 125 | $new->attribute2('newValue'); | ||||||||||||||||||||||||||||
| 126 | $new->attribute4([ 'newVal1', 'newVal2', ]); | ||||||||||||||||||||||||||||
| 127 | |||||||||||||||||||||||||||||
| 128 | =head1 DESCRIPTION | ||||||||||||||||||||||||||||
| 129 | |||||||||||||||||||||||||||||
| 130 | This class is a subclass from B<Class::Gomor>. It implements objects as hash references, and inherits methods from B<Class::Gomor>. | ||||||||||||||||||||||||||||
| 131 | |||||||||||||||||||||||||||||
| 132 | =head1 GLOBAL VARIABLE | ||||||||||||||||||||||||||||
| 133 | |||||||||||||||||||||||||||||
| 134 | See B<Class::Gomor>. | ||||||||||||||||||||||||||||
| 135 | |||||||||||||||||||||||||||||
| 136 | =head1 METHODS | ||||||||||||||||||||||||||||
| 137 | |||||||||||||||||||||||||||||
| 138 | =over 4 | ||||||||||||||||||||||||||||
| 139 | |||||||||||||||||||||||||||||
| 140 | =item B<new> (hash) | ||||||||||||||||||||||||||||
| 141 | |||||||||||||||||||||||||||||
| 142 | Object constructor. This is where user passed attributes (hash argument) are checked against valid attributes (gathered by B<cgGetAttributes> method). Valid attributes are those that exists (doh!), and have not an undef value. The default is to check this, you can avoid it by setting B<$NoCheck> global variable (see perldoc B<Class::Gomor>). | ||||||||||||||||||||||||||||
| 143 | |||||||||||||||||||||||||||||
| 144 | =item B<cgBuildIndices> | ||||||||||||||||||||||||||||
| 145 | |||||||||||||||||||||||||||||
| 146 | This method does nothing. It only exists to make it more easy to switch between B<Class::Gomor::Array> and B<Class::Gomor::Hash>. | ||||||||||||||||||||||||||||
| 147 | |||||||||||||||||||||||||||||
| 148 | =item B<cgBuildAccessorsScalar> (array ref) | ||||||||||||||||||||||||||||
| 149 | |||||||||||||||||||||||||||||
| 150 | =item B<cgBuildAccessorsArray> (array ref) | ||||||||||||||||||||||||||||
| 151 | |||||||||||||||||||||||||||||
| 152 | See B<Class::Gomor>. | ||||||||||||||||||||||||||||
| 153 | |||||||||||||||||||||||||||||
| 154 | =item B<cgGetIndice> (scalar) | ||||||||||||||||||||||||||||
| 155 | |||||||||||||||||||||||||||||
| 156 | This method does nearly nothing. It only returns the passed-in scalar parameter (so the syntax is the same as in B<Class::Gomor::Array>). It only exists to make it more easy to switch between B<Class::Gomor::Array> and B<Class::Gomor::Hash>. | ||||||||||||||||||||||||||||
| 157 | |||||||||||||||||||||||||||||
| 158 | =item B<cgClone> [ (scalar) ] | ||||||||||||||||||||||||||||
| 159 | |||||||||||||||||||||||||||||
| 160 | You can clone one of your objects by calling this method. An optional parameter may be used to create multiple clones. Cloning will occure only on the first level attributes, that is, if you have attributes containing other objects, they will not be cloned. | ||||||||||||||||||||||||||||
| 161 | |||||||||||||||||||||||||||||
| 162 | =item B<cgFullClone> [ (scalar) ] | ||||||||||||||||||||||||||||
| 163 | |||||||||||||||||||||||||||||
| 164 | This method is the same as B<cgClone>, but will clone all attributes recursively, but only if they are subclassed from B<Class::Gomor>. So, objects created with other modules than B<Class::Gomor::Array> or B<Class::Gomor::Hash> will not be cloned. | ||||||||||||||||||||||||||||
| 165 | |||||||||||||||||||||||||||||
| 166 | Another thing to note, there is no catch for cycling references (when you link two objects with each others). You have been warned. | ||||||||||||||||||||||||||||
| 167 | |||||||||||||||||||||||||||||
| 168 | =item B<cgDumper> | ||||||||||||||||||||||||||||
| 169 | |||||||||||||||||||||||||||||
| 170 | Will return a string as with B<Data::Dumper> Dumper method. This is less useful for hashref objects, because they already include attributes names. | ||||||||||||||||||||||||||||
| 171 | |||||||||||||||||||||||||||||
| 172 | =back | ||||||||||||||||||||||||||||
| 173 | |||||||||||||||||||||||||||||
| 174 | =head1 SEE ALSO | ||||||||||||||||||||||||||||
| 175 | |||||||||||||||||||||||||||||
| 176 | L<Class::Gomor> | ||||||||||||||||||||||||||||
| 177 | |||||||||||||||||||||||||||||
| 178 | =head1 AUTHOR | ||||||||||||||||||||||||||||
| 179 | |||||||||||||||||||||||||||||
| 180 | Patrice E<lt>GomoRE<gt> Auffret | ||||||||||||||||||||||||||||
| 181 | |||||||||||||||||||||||||||||
| 182 | =head1 COPYRIGHT AND LICENSE | ||||||||||||||||||||||||||||
| 183 | |||||||||||||||||||||||||||||
| 184 | Copyright (c) 2004-2015, Patrice E<lt>GomoRE<gt> Auffret | ||||||||||||||||||||||||||||
| 185 | |||||||||||||||||||||||||||||
| 186 | You may distribute this module under the terms of the Artistic license. | ||||||||||||||||||||||||||||
| 187 | See LICENSE.Artistic file in the source distribution archive. | ||||||||||||||||||||||||||||
| 188 | |||||||||||||||||||||||||||||
| 189 | =cut | ||||||||||||||||||||||||||||