| 1 | # | ||||||||||||||||||||
| 2 | # Revision History: | ||||||||||||||||||||
| 3 | # | ||||||||||||||||||||
| 4 | # 21-Dec-2002 Dick Munroe (munroe@csworks.com) | ||||||||||||||||||||
| 5 | # Finish Documentation. | ||||||||||||||||||||
| 6 | # | ||||||||||||||||||||
| 7 | # 20-May-2003 Dick Munroe (munroe@csworks.com) | ||||||||||||||||||||
| 8 | # Make sure package variables don't leak. | ||||||||||||||||||||
| 9 | # Make the test harness happy. | ||||||||||||||||||||
| 10 | # | ||||||||||||||||||||
| 11 | |||||||||||||||||||||
| 12 | package Stanza ; | ||||||||||||||||||||
| 13 | |||||||||||||||||||||
| 14 | 1 | ▶ | use vars qw($VERSION) ; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 15 | 1 | ▶ | use strict ; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 16 | |||||||||||||||||||||
| 17 | 1 | our $VERSION = "1.02" ; | |||||||||||||||||||
| 18 | |||||||||||||||||||||
| 19 | sub new | ||||||||||||||||||||
| 20 | 0 | ▶ | { | ||||||||||||||||||
|
Subroutine
new: not called CC=5 SCAR=34.0
Pod
undocumented
| |||||||||||||||||||||
| 21 | 0 | my ($thePackage, $theName) = @_ ; | |||||||||||||||||||
| 22 | |||||||||||||||||||||
| 23 | 0 | ▶ | my $theClass = ref($thePackage) || $thePackage ; | ||||||||||||||||||
|
Condition0%0/3
ref $thePackage || $thePackage
MC/DC0%0/2
ref $thePackage || $thePackage
ref $thePackage
$thePackage
Truth table0%0/3
A || B
A= ref $thePackageB= $thePackage
| |||||||||||||||||||||
| 24 | 0 | ▶ | my $theParent = ref($thePackage) && $thePackage ; | ||||||||||||||||||
|
Condition0%0/3
ref $thePackage && $thePackage
MC/DC0%0/2
ref $thePackage && $thePackage
ref $thePackage
$thePackage
Truth table0%0/3
A && B
A= ref $thePackageB= $thePackage
| |||||||||||||||||||||
| 25 | |||||||||||||||||||||
| 26 | 0 | my $theStanza = | |||||||||||||||||||
| 27 | { | ||||||||||||||||||||
| 28 | 'name' => $theName, | ||||||||||||||||||||
| 29 | 'order' => [], | ||||||||||||||||||||
| 30 | 'data' => {} | ||||||||||||||||||||
| 31 | } ; | ||||||||||||||||||||
| 32 | |||||||||||||||||||||
| 33 | 0 | bless $theStanza, $theClass ; | |||||||||||||||||||
| 34 | |||||||||||||||||||||
| 35 | 0 | ▶ | if ($theParent) | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 36 | { | ||||||||||||||||||||
| 37 | 0 | $theStanza->name($theParent->name()) ; | |||||||||||||||||||
| 38 | |||||||||||||||||||||
| 39 | 0 | foreach ($theParent->order()) | |||||||||||||||||||
| 40 | { | ||||||||||||||||||||
| 41 | 0 | $theStanza->add($_, $theParent->item($_)) ; | |||||||||||||||||||
| 42 | } ; | ||||||||||||||||||||
| 43 | } ; | ||||||||||||||||||||
| 44 | |||||||||||||||||||||
| 45 | 0 | return $theStanza ; | |||||||||||||||||||
| 46 | } ; | ||||||||||||||||||||
| 47 | |||||||||||||||||||||
| 48 | sub add | ||||||||||||||||||||
| 49 | 0 | ▶ | { | ||||||||||||||||||
|
Subroutine
add: not called CC=2 SCAR=17.9
Pod
undocumented
| |||||||||||||||||||||
| 50 | 0 | ▶ | die "Too few arguments to add" if (scalar(@_) < 3) ; | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 51 | |||||||||||||||||||||
| 52 | 0 | my ($theObject, $theName, $theData) = @_ ; | |||||||||||||||||||
| 53 | |||||||||||||||||||||
| 54 | 0 | $theObject->{'data'}->{$theName} = $theData ; | |||||||||||||||||||
| 55 | 0 | push @{$theObject->{'order'}},$theName ; | |||||||||||||||||||
| 56 | |||||||||||||||||||||
| 57 | 0 | return $theObject ; | |||||||||||||||||||
| 58 | } ; | ||||||||||||||||||||
| 59 | |||||||||||||||||||||
| 60 | sub item | ||||||||||||||||||||
| 61 | 0 | ▶ | { | ||||||||||||||||||
|
Subroutine
item: not called CC=3 SCAR=24.8
Pod
undocumented
| |||||||||||||||||||||
| 62 | 0 | my ($theObject, $theName, $theValue) = @_ ; | |||||||||||||||||||
| 63 | |||||||||||||||||||||
| 64 | 0 | ▶ | if (scalar(@_) > 2) | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 65 | { | ||||||||||||||||||||
| 66 | 0 | ▶ | if (exists($theObject->{'data'}->{$theName})) | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 67 | { | ||||||||||||||||||||
| 68 | 0 | $theObject->{'data'}->{$theName} = $theValue ; | |||||||||||||||||||
| 69 | } | ||||||||||||||||||||
| 70 | else | ||||||||||||||||||||
| 71 | { | ||||||||||||||||||||
| 72 | 0 | $theObject->add($theName, $theValue) ; | |||||||||||||||||||
| 73 | } ; | ||||||||||||||||||||
| 74 | } ; | ||||||||||||||||||||
| 75 | |||||||||||||||||||||
| 76 | 0 | return $theObject->{'data'}->{$theName} ; | |||||||||||||||||||
| 77 | } ; | ||||||||||||||||||||
| 78 | |||||||||||||||||||||
| 79 | # | ||||||||||||||||||||
| 80 | # Merge the contents of two stanzas. | ||||||||||||||||||||
| 81 | # | ||||||||||||||||||||
| 82 | # If an item exists in both stanzas, replace it in the | ||||||||||||||||||||
| 83 | # target. If it doesn't exist in the target, add it to | ||||||||||||||||||||
| 84 | # the target. This updates the order array to hold the | ||||||||||||||||||||
| 85 | # order of addition stable. | ||||||||||||||||||||
| 86 | # | ||||||||||||||||||||
| 87 | |||||||||||||||||||||
| 88 | sub merge | ||||||||||||||||||||
| 89 | 0 | ▶ | { | ||||||||||||||||||
|
Subroutine
merge: not called CC=3 SCAR=24.8
Pod
undocumented
| |||||||||||||||||||||
| 90 | 0 | my ($theObject, $theNewStanza) = @_ ; | |||||||||||||||||||
| 91 | |||||||||||||||||||||
| 92 | 0 | foreach ($theNewStanza->order()) | |||||||||||||||||||
| 93 | { | ||||||||||||||||||||
| 94 | 0 | ▶ | if (defined($theObject->item($_))) | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 95 | { | ||||||||||||||||||||
| 96 | 0 | $theObject->item($_,$theNewStanza->item($_)) ; | |||||||||||||||||||
| 97 | } | ||||||||||||||||||||
| 98 | else | ||||||||||||||||||||
| 99 | { | ||||||||||||||||||||
| 100 | 0 | $theObject->add($_,$theNewStanza->item($_)) ; | |||||||||||||||||||
| 101 | } ; | ||||||||||||||||||||
| 102 | } ; | ||||||||||||||||||||
| 103 | |||||||||||||||||||||
| 104 | 0 | return $theObject ; | |||||||||||||||||||
| 105 | } ; | ||||||||||||||||||||
| 106 | |||||||||||||||||||||
| 107 | sub name | ||||||||||||||||||||
| 108 | 0 | ▶ | { | ||||||||||||||||||
|
Subroutine
name: not called CC=2 SCAR=17.9
Pod
undocumented
| |||||||||||||||||||||
| 109 | 0 | my ($theObject, $theName) = @_ ; | |||||||||||||||||||
| 110 | |||||||||||||||||||||
| 111 | 0 | ▶ | $theObject->{'name'} = $theName if (scalar(@_) > 1) ; | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 112 | |||||||||||||||||||||
| 113 | 0 | return $theObject->{'name'} ; | |||||||||||||||||||
| 114 | } ; | ||||||||||||||||||||
| 115 | |||||||||||||||||||||
| 116 | sub order | ||||||||||||||||||||
| 117 | 0 | ▶ | { | ||||||||||||||||||
|
Subroutine
order: not called CC=1 SCAR=6.9
Pod
undocumented
| |||||||||||||||||||||
| 118 | 0 | my ($theObject) = @_ ; | |||||||||||||||||||
| 119 | |||||||||||||||||||||
| 120 | 0 | return @{$theObject->{'order'}} ; | |||||||||||||||||||
| 121 | } ; | ||||||||||||||||||||
| 122 | |||||||||||||||||||||
| 123 | 1 | 1 ; | |||||||||||||||||||
| 124 | |||||||||||||||||||||
| 125 | =pod | ||||||||||||||||||||
| 126 | |||||||||||||||||||||
| 127 | =head1 NAME | ||||||||||||||||||||
| 128 | |||||||||||||||||||||
| 129 | Stanza - Container for holding data parsed from stanza | ||||||||||||||||||||
| 130 | files. | ||||||||||||||||||||
| 131 | |||||||||||||||||||||
| 132 | =head1 SYNOPSIS | ||||||||||||||||||||
| 133 | |||||||||||||||||||||
| 134 | # Instantiate or clone a copy of a stanza. | ||||||||||||||||||||
| 135 | # | ||||||||||||||||||||
| 136 | my $theStanza = new Stanza('Stanza Name') ; | ||||||||||||||||||||
| 137 | |||||||||||||||||||||
| 138 | # Add a datum to a Stanza. | ||||||||||||||||||||
| 139 | # | ||||||||||||||||||||
| 140 | $theStanza->add('newDatum', 'value') ; | ||||||||||||||||||||
| 141 | |||||||||||||||||||||
| 142 | # Fetch or store an item in a Stanza. | ||||||||||||||||||||
| 143 | # | ||||||||||||||||||||
| 144 | my $theOldValue = $theStanza->item('newDatum') ; | ||||||||||||||||||||
| 145 | $theStanza->item('newDatum', 'newValue') ; | ||||||||||||||||||||
| 146 | |||||||||||||||||||||
| 147 | # Merge the contents of two stanzas. | ||||||||||||||||||||
| 148 | # | ||||||||||||||||||||
| 149 | $theNewStanza->merge($theOldStanza) ; | ||||||||||||||||||||
| 150 | |||||||||||||||||||||
| 151 | # Get/set the name of the stanza. | ||||||||||||||||||||
| 152 | # | ||||||||||||||||||||
| 153 | $theStanza->name() ; | ||||||||||||||||||||
| 154 | $theStanza->name('newName') ; | ||||||||||||||||||||
| 155 | |||||||||||||||||||||
| 156 | # Get the order in which data were added to the stanza. | ||||||||||||||||||||
| 157 | # | ||||||||||||||||||||
| 158 | foreach ($theStanza->order()) | ||||||||||||||||||||
| 159 | { | ||||||||||||||||||||
| 160 | ... ; | ||||||||||||||||||||
| 161 | } ; | ||||||||||||||||||||
| 162 | |||||||||||||||||||||
| 163 | =head1 DESCRIPTION | ||||||||||||||||||||
| 164 | |||||||||||||||||||||
| 165 | The Stanza class provides a syntax free container for holding | ||||||||||||||||||||
| 166 | stanza datum/value pairs. As a consequence, StanzaFile formatting | ||||||||||||||||||||
| 167 | must be done in the classes using the Stanza class, not by | ||||||||||||||||||||
| 168 | the Stanza class or sub-classes thereof. | ||||||||||||||||||||
| 169 | |||||||||||||||||||||
| 170 | =head1 EXAMPLES | ||||||||||||||||||||
| 171 | |||||||||||||||||||||
| 172 | =head1 BUGS | ||||||||||||||||||||
| 173 | |||||||||||||||||||||
| 174 | None known. | ||||||||||||||||||||
| 175 | |||||||||||||||||||||
| 176 | =head1 WARNINGS | ||||||||||||||||||||
| 177 | |||||||||||||||||||||
| 178 | =head1 AUTHOR | ||||||||||||||||||||
| 179 | |||||||||||||||||||||
| 180 | Dick Munroe (munroe@csworks). I'm looking for work. If you hear | ||||||||||||||||||||
| 181 | of anything that might be of interest to a VERY senior engineer/architect | ||||||||||||||||||||
| 182 | drop me a note. See http://www.acornsw.com/resume/dick.html for | ||||||||||||||||||||
| 183 | details. | ||||||||||||||||||||
| 184 | |||||||||||||||||||||
| 185 | =head1 SEE ALSO | ||||||||||||||||||||
| 186 | |||||||||||||||||||||
| 187 | =cut | ||||||||||||||||||||
| 188 | |||||||||||||||||||||