File Coverage

blib/lib/Stanza.pm
Criterion Covered Total %
statement 6 40 15.0
branch 0 12 0.0
condition 0 6 0.0
subroutine 2 8 25.0
pod 0 6 0.0
total 8 72 11.1


line stmt bran cond sub pod time code
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     1   6539 use vars qw($VERSION) ;
  1         3  
  1         44  
15 1     1   6 use strict ;
  1         1  
  1         541  
16              
17             our $VERSION = "1.02" ;
18              
19             sub new
20             {
21 0     0 0   my ($thePackage, $theName) = @_ ;
22              
23 0   0       my $theClass = ref($thePackage) || $thePackage ;
24 0   0       my $theParent = ref($thePackage) && $thePackage ;
25              
26 0           my $theStanza =
27             {
28             'name' => $theName,
29             'order' => [],
30             'data' => {}
31             } ;
32              
33 0           bless $theStanza, $theClass ;
34              
35 0 0         if ($theParent)
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             {
50 0 0   0 0   die "Too few arguments to add" if (scalar(@_) < 3) ;
51              
52 0           my ($theObject, $theName, $theData) = @_ ;
53              
54 0           $theObject->{'data'}->{$theName} = $theData ;
55 0           push @{$theObject->{'order'}},$theName ;
  0            
56            
57 0           return $theObject ;
58             } ;
59              
60             sub item
61             {
62 0     0 0   my ($theObject, $theName, $theValue) = @_ ;
63              
64 0 0         if (scalar(@_) > 2)
65             {
66 0 0         if (exists($theObject->{'data'}->{$theName}))
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             {
90 0     0 0   my ($theObject, $theNewStanza) = @_ ;
91              
92 0           foreach ($theNewStanza->order())
93             {
94 0 0         if (defined($theObject->item($_)))
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             {
109 0     0 0   my ($theObject, $theName) = @_ ;
110              
111 0 0         $theObject->{'name'} = $theName if (scalar(@_) > 1) ;
112              
113 0           return $theObject->{'name'} ;
114             } ;
115              
116             sub order
117             {
118 0     0 0   my ($theObject) = @_ ;
119              
120 0           return @{$theObject->{'order'}} ;
  0            
121             } ;
122              
123             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