| 1 | package Tie::FieldVals::Row::Join; | ||||||||||
| 2 | 3 | ▶ | use strict; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 3 | 3 | ▶ | use warnings; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 4 | |||||||||||
| 5 | =head1 NAME | ||||||||||
| 6 | |||||||||||
| 7 | Tie::FieldVals::Row::Join - a hash tie for merging rows of Tie::FieldVals data | ||||||||||
| 8 | |||||||||||
| 9 | =head1 VERSION | ||||||||||
| 10 | |||||||||||
| 11 | This describes version B<0.6203> of Tie::FieldVals::Row::Join. | ||||||||||
| 12 | |||||||||||
| 13 | =cut | ||||||||||
| 14 | |||||||||||
| 15 | 3 | our $VERSION = '0.6203'; | |||||||||
| 16 | |||||||||||
| 17 | =head1 SYNOPSIS | ||||||||||
| 18 | |||||||||||
| 19 | use Tie::FieldVals::Row; | ||||||||||
| 20 | use Tie::FieldVals::Row::Join; | ||||||||||
| 21 | |||||||||||
| 22 | # just the keys | ||||||||||
| 23 | my %person_thing; | ||||||||||
| 24 | my $jr = tie %person_thing, 'Tie::FieldVals::Row::Join, | ||||||||||
| 25 | fields=>@keys; | ||||||||||
| 26 | |||||||||||
| 27 | # keys and values | ||||||||||
| 28 | my %person; | ||||||||||
| 29 | my $rr = tie %person_thing, 'Tie::FieldVals::Row, | ||||||||||
| 30 | fields=>@keys; | ||||||||||
| 31 | |||||||||||
| 32 | my %person_thing; | ||||||||||
| 33 | my $jr = tie %person_thing, 'Tie::FieldVals::Row::Join, | ||||||||||
| 34 | row=>$rr; | ||||||||||
| 35 | |||||||||||
| 36 | |||||||||||
| 37 | =head1 DESCRIPTION | ||||||||||
| 38 | |||||||||||
| 39 | This is a Tie object to enable the merging of more than one | ||||||||||
| 40 | Tie::FieldVals::Row hashes into one hash. | ||||||||||
| 41 | |||||||||||
| 42 | =cut | ||||||||||
| 43 | |||||||||||
| 44 | 3 | ▶ | use 5.006; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 45 | 3 | ▶ | use strict; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 46 | 3 | ▶ | use Carp; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 47 | |||||||||||
| 48 | 3 | our @ISA = qw(Tie::FieldVals::Row); | |||||||||
| 49 | |||||||||||
| 50 | # to make taint happy | ||||||||||
| 51 | 3 | $ENV{PATH} = "/bin:/usr/bin:/usr/local/bin"; | |||||||||
| 52 | 3 | $ENV{CDPATH} = ''; | |||||||||
| 53 | 3 | $ENV{BASH_ENV} = ''; | |||||||||
| 54 | |||||||||||
| 55 | # for debugging | ||||||||||
| 56 | 3 | my $DEBUG = 0; | |||||||||
| 57 | |||||||||||
| 58 | =head1 OBJECT METHODS | ||||||||||
| 59 | |||||||||||
| 60 | =head2 append_keys | ||||||||||
| 61 | |||||||||||
| 62 | $row_obj->append_keys(@fields); | ||||||||||
| 63 | |||||||||||
| 64 | Extend the legal fields definition by adding the given fields to it. | ||||||||||
| 65 | Sets the given fields to be undefined. | ||||||||||
| 66 | |||||||||||
| 67 | =cut | ||||||||||
| 68 | 0 | ▶ | sub append_keys ($@) { | ||||||||
|
Subroutine
append_keys: not called CC=4 SCAR=30.0
Pod
documented
| |||||||||||
| 69 | 0 | ▶ | carp &whowasi if $DEBUG; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 70 | 0 | my $self = shift; | |||||||||
| 71 | 0 | my @keys = @_; | |||||||||
| 72 | |||||||||||
| 73 | 0 | foreach my $key (@keys) | |||||||||
| 74 | { | ||||||||||
| 75 | 0 | ▶ | if (!exists $self->{FIELDS}->{$key}) | ||||||||
|
Branch: 1 branch
| |||||||||||
| 76 | { | ||||||||||
| 77 | 0 | $self->{FIELDS}->{$key} = undef; | |||||||||
| 78 | 0 | push @{$self->{OPTIONS}->{fields}}, $key; | |||||||||
| 79 | } | ||||||||||
| 80 | } | ||||||||||
| 81 | |||||||||||
| 82 | } # append_keys | ||||||||||
| 83 | |||||||||||
| 84 | =head2 merge_rows | ||||||||||
| 85 | |||||||||||
| 86 | $row_obj->merge_rows($row_obj2); | ||||||||||
| 87 | |||||||||||
| 88 | Merge a Tie::FieldVals::Row object with this one. The second | ||||||||||
| 89 | row object has different Fields than this one, and this will | ||||||||||
| 90 | extend the legal fields definition by adding the given fields to it, | ||||||||||
| 91 | as well as adding the values of the second row to this row. | ||||||||||
| 92 | |||||||||||
| 93 | =cut | ||||||||||
| 94 | 2 | ▶ | sub merge_rows ($$) { | ||||||||
|
Subroutine
merge_rows: called CC=4 SCAR=13.9
Pod
documented
| |||||||||||
| 95 | 2 | ▶ | carp &whowasi if $DEBUG; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 96 | 2 | my $self = shift; | |||||||||
| 97 | 2 | my $row_obj = shift; | |||||||||
| 98 | |||||||||||
| 99 | 2 | my @keys = @{$row_obj->field_names()}; | |||||||||
| 100 | 2 | foreach my $key (@keys) | |||||||||
| 101 | { | ||||||||||
| 102 | 17 | ▶ | if (!exists $self->{FIELDS}->{$key}) # only add new keys | ||||||||
|
Branch: 1 branch
| |||||||||||
| 103 | { | ||||||||||
| 104 | 16 | $self->{FIELDS}->{$key} = []; | |||||||||
| 105 | 16 | push @{$self->{FIELDS}->{$key}}, @{$row_obj->{FIELDS}->{$key}}; | |||||||||
| 106 | 16 | push @{$self->{OPTIONS}->{fields}}, $key; | |||||||||
| 107 | } | ||||||||||
| 108 | } | ||||||||||
| 109 | |||||||||||
| 110 | } # merge_rows | ||||||||||
| 111 | |||||||||||
| 112 | =head1 TIE-HASH METHODS | ||||||||||
| 113 | |||||||||||
| 114 | =head2 TIEHASH | ||||||||||
| 115 | |||||||||||
| 116 | Create a new instance of the object as tied to a hash. | ||||||||||
| 117 | |||||||||||
| 118 | my %person_thing; | ||||||||||
| 119 | my $jr = tie %person_thing, 'Tie::FieldVals::Row::Join, | ||||||||||
| 120 | fields=>@keys; | ||||||||||
| 121 | |||||||||||
| 122 | my %person; | ||||||||||
| 123 | my $rr = tie %person_thing, 'Tie::FieldVals::Row, | ||||||||||
| 124 | fields=>@keys; | ||||||||||
| 125 | |||||||||||
| 126 | my %person_thing; | ||||||||||
| 127 | my $jr = tie %person_thing, 'Tie::FieldVals::Row::Join, | ||||||||||
| 128 | row=>$rr; | ||||||||||
| 129 | |||||||||||
| 130 | =cut | ||||||||||
| 131 | 1 | ▶ | sub TIEHASH { | ||||||||
|
Subroutine
TIEHASH: called CC=3 SCAR=11.1
| |||||||||||
| 132 | 1 | ▶ | carp &whowasi if $DEBUG; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 133 | 1 | my $class = shift; | |||||||||
| 134 | 1 | my %args = ( | |||||||||
| 135 | fields=>undef, | ||||||||||
| 136 | row=>undef, | ||||||||||
| 137 | @_ | ||||||||||
| 138 | ); | ||||||||||
| 139 | |||||||||||
| 140 | 1 | my $self; | |||||||||
| 141 | 1 | ▶ | if (defined $args{row}) | ||||||||
|
Branch: 1 branch
| |||||||||||
| 142 | { | ||||||||||
| 143 | 1 | my $row_obj = $args{row}; | |||||||||
| 144 | 1 | delete $args{row}; | |||||||||
| 145 | 1 | $self = Tie::FieldVals::Row::TIEHASH($class, | |||||||||
| 146 | fields=>[qw(dummy)]); | ||||||||||
| 147 | # merge the rows | ||||||||||
| 148 | 1 | %{$self->{FIELDS}} = (); | |||||||||
| 149 | 1 | $self->{OPTIONS}->{fields} = []; | |||||||||
| 150 | 1 | $self->merge_rows($row_obj); | |||||||||
| 151 | } | ||||||||||
| 152 | else # just fields | ||||||||||
| 153 | { | ||||||||||
| 154 | 0 | $self = Tie::FieldVals::Row::TIEHASH($class, %args) | |||||||||
| 155 | } | ||||||||||
| 156 | |||||||||||
| 157 | 1 | return $self; | |||||||||
| 158 | } # TIEHASH | ||||||||||
| 159 | |||||||||||
| 160 | 0 | ▶ | sub UNTIE { | ||||||||
|
Subroutine
UNTIE: not called CC=3 SCAR=24.8
| |||||||||||
| 161 | 0 | ▶ | carp &whowasi if $DEBUG; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 162 | 0 | my $self = shift; | |||||||||
| 163 | 0 | my $count = shift; | |||||||||
| 164 | |||||||||||
| 165 | 0 | ▶ | carp "untie attempted while $count inner references still exist" if $count; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 166 | |||||||||||
| 167 | 0 | $self->SUPER::UNTIE($count); | |||||||||
| 168 | } | ||||||||||
| 169 | |||||||||||
| 170 | =head1 PRIVATE METHODS | ||||||||||
| 171 | |||||||||||
| 172 | For developer reference only. | ||||||||||
| 173 | |||||||||||
| 174 | =head2 debug | ||||||||||
| 175 | |||||||||||
| 176 | Set debugging on. | ||||||||||
| 177 | |||||||||||
| 178 | =cut | ||||||||||
| 179 | 0 | ▶ | sub debug { $DEBUG = @_ ? shift : 1 } | ||||||||
|
Subroutine
debug: not called CC=2 SCAR=17.9
Pod
documented
Branch: 1 branch
| |||||||||||
| 180 | |||||||||||
| 181 | =head2 whowasi | ||||||||||
| 182 | |||||||||||
| 183 | For debugging: say who called this | ||||||||||
| 184 | |||||||||||
| 185 | =cut | ||||||||||
| 186 | 0 | ▶ | sub whowasi { (caller(1))[3] . '()' } | ||||||||
|
Subroutine
whowasi: not called CC=1 SCAR=6.9
Pod
documented
| |||||||||||
| 187 | |||||||||||
| 188 | =head1 REQUIRES | ||||||||||
| 189 | |||||||||||
| 190 | Test::More | ||||||||||
| 191 | Carp | ||||||||||
| 192 | |||||||||||
| 193 | =head1 SEE ALSO | ||||||||||
| 194 | |||||||||||
| 195 | perl(1). | ||||||||||
| 196 | L<Tie::FieldVals> | ||||||||||
| 197 | L<Tie::FieldVals::Join> | ||||||||||
| 198 | |||||||||||
| 199 | =head1 BUGS | ||||||||||
| 200 | |||||||||||
| 201 | Please report any bugs or feature requests to the author. | ||||||||||
| 202 | |||||||||||
| 203 | =head1 AUTHOR | ||||||||||
| 204 | |||||||||||
| 205 | Kathryn Andersen (RUBYKAT) | ||||||||||
| 206 | perlkat AT katspace dot com | ||||||||||
| 207 | http://www.katspace.com | ||||||||||
| 208 | |||||||||||
| 209 | =head1 COPYRIGHT AND LICENCE | ||||||||||
| 210 | |||||||||||
| 211 | Copyright (c) 2004 by Kathryn Andersen | ||||||||||
| 212 | |||||||||||
| 213 | This program is free software; you can redistribute it and/or modify it | ||||||||||
| 214 | under the same terms as Perl itself. | ||||||||||
| 215 | |||||||||||
| 216 | |||||||||||
| 217 | =cut | ||||||||||
| 218 | |||||||||||
| 219 | 3 | 1; # End of Tie::FieldVals::Row::Join | |||||||||
| 220 | __END__ | ||||||||||