Record.pm

filter: Statementstmt 93.2% 83 / 89 Branchbran 35.7% 5 / 14 Conditioncond 0.0% 0 / 3 MC/DCmcdc 0.0% 0 / 2 Subroutinesub 100% 25 / 25 Podpod 100% 20 / 20 totaltotal 86.9% 133 / 153 CC 14 SCAR 14.8
CC
14
Cov
83%
element
15.2
_getElement
3.6
new
2.0
CRAP 4.4
↑ Summary Element.pm »
1 package DublinCore::Record;
2  
3 =head1 NAME
4  
5 DublinCore::Record - Container for Dublin Core metadata elements
6  
7 =head1 SYNOPSIS
8  
9 use DublinCore::Record;
10
11 my $record = DublinCore::Record->new();
12
13 # later ...
14  
15 # print the title
16 print $record->element( 'title' )->content;
17  
18 ## list context will retrieve all of a particular element
19 foreach my $element ( $record->element( 'Creator' ) ) {
20 print "creator: ", $element->content(), "\n";
21 }
22  
23 ## qualified dublin core
24 my $creation = $record->element( 'Date.created' )->content();
25  
26 =head1 DESCRIPTION
27  
28 DublinCore::Record is an abstract class for manipulating DublinCore metadata.
29 The Dublin Core is a small set of metadata elements for describing information
30 resources. For more information on embedding DublinCore in HTML see RFC 2731
31 L<http://www.ietf.org/rfc/rfc2731> or L<http://www.dublincore.org/documents/dces/>
32  
33 =cut
34  
35 5 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
36 5 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
37  
38 5 ▶use Carp qw( croak );
Subroutine BEGIN: called CC=1 SCAR=0.0
39 5 ▶use DublinCore::Element;
Subroutine BEGIN: called CC=1 SCAR=0.0
40  
41 5 our $VERSION = '0.03';
42 5 our @VALID_ELEMENTS = qw(
43 title
44 creator
45 subject
46 description
47 publisher
48 contributor
49 date
50 type
51 format
52 identifier
53 source
54 language
55 relation
56 coverage
57 rights
58 );
59  
60 =head1 METHODS
61  
62 =head2 new()
63  
64 The constructor. Takes no arguments.
65  
66 $record = DublinCore::Record->new();
67  
68 =cut
69  
70 4 ▶sub new {
Subroutine new: called CC=2 SCAR=6.9
Pod documented
71 4 my $class = shift;
72 4 my $self = {};
73  
74 4 $self->{ "DC_$_" } = [] for @VALID_ELEMENTS;
75  
76 4 bless $self, $class;
77  
78 4 $self->add( @_ );
79  
80 4 return $self;
81 }
82  
83 =head2 add( @elements )
84  
85 Adds valid DublinCore::Element objects to the record.
86  
87 =cut
88  
89 34 ▶sub add {
Subroutine add: called CC=2 SCAR=6.9
Pod documented
90 34 my $self = shift;
91  
92 34 for my $element ( @_ ) {
93 30 push @{ $self->{ 'DC_' . lc( $element->name ) } }, $element;
94 }
95 }
96  
97 =head2 remove( @elements )
98  
99 Removes valid DublinCore::Element object from the record.
100  
101 =cut
102  
103 1 ▶sub remove {
Subroutine remove: called CC=2 SCAR=6.9
Pod documented
104 1 my $self = shift;
105  
106 1 for my $element ( @_ ) {
107 1 my $name = 'DC_' . lc( $element->name );
108 $self->{ $name } = [
109 1 grep { $element ne $_ } @{ $self->{ $name } }
110 ];
111 }
112 }
113  
114 =head2 element()
115  
116 This method will return a relevant DublinCore::Element object. When
117 called in a scalar context element() will return the first relevant element
118 found, and when called in a list context it will return all the relevant
119 elements (since Dublin Core elements are repeatable).
120  
121 ## retrieve first title element
122 my $element = $record->element( 'Title' );
123 my $title = $element->content();
124
125 ## shorthand object chaining to extract element content
126 my $title = $record->element( 'Title' )->content();
127
128 ## retrieve all creator elements
129 @creators = $record->element( 'Creator' );
130  
131 You can also retrieve qualified elements in a similar fashion.
132  
133 my $date = $record->element( 'Date.created' )->content();
134  
135 In order to fascilitate chaining element() will return an empty
136 DublinCore::Element object when the requested element does not
137 exist. You can check if you're getting an empty empty back by using
138 the is_empty() method.
139  
140 if( $record->element( 'title' )->is_empty ) {
141 # no title
142 }
143  
144 =cut
145  
146 1 ▶sub element {
Subroutine element: called CC=8 SCAR=27.2
Pod documented
147 1 my ( $self, $name ) = @_;
148 1 $name = lc( $name );
149  
150 ## must be a valid DC element (with additional qualifier)
151 croak( "invalid Dublin Core element: $name" )
152 1 ▶ if ! grep { $name =~ /^$_/ } @VALID_ELEMENTS;
Branch: 1 branch
BranchTrueFalseTotal
unless grep {$name =~ /^$_/;} @DublinCore::Record::VALID_ELEMENTS 0 1 1
153  
154 ## extract qualifier if present
155 1 my $qualifier;
156 1 ( $name, $qualifier ) = split /\./, $name;
157  
158 1 my @elements = ();
159 1 foreach my $element ( @{ $self->{ "DC_$name" } } ) {
160 0 ▶ if ( $qualifier and $element->qualifier() =~ /$qualifier/i ) {
Branch: 2 branches
BranchTrueFalseTotal
if ($qualifier and $element->qualifier =~ /$qualifier/i) { } 0 0 0
elsif (not $qualifier) { } 0 0 0
Condition0%0/3
$qualifier and $element->qualifier =~ /$qualifier/i
!ll&&!rl&&r
000
MC/DC0%0/2
$qualifier and $element->qualifier =~ /$qualifier/i
$qualifier $element->qualifier =~ /$qualifier/i
Truth table0%0/3
A and B
A= $qualifierB= $element->qualifier =~ /$qualifier/i
ABresult
0X0
100
111
161 0 push( @elements, $element );
162 } elsif ( !$qualifier ) {
163 0 push( @elements, $element );
164 }
165 }
166  
167 0 ▶ if ( wantarray ) { return @elements };
Branch: 1 branch
BranchTrueFalseTotal
if (wantarray) 0 1 1
168 1 ▶ return( $elements[ 0 ] ) if $elements[ 0 ];
Branch: 1 branch
BranchTrueFalseTotal
if $elements[0] 0 1 1
169  
170 ## otherwise return an empty element object to fascilitate
171 ## chaining when the element doesn't exist :
172 ## $dc->element( 'Title' )->content().
173  
174 1 return( DublinCore::Element->new() );
175 }
176  
177 =head2 elements()
178  
179 Returns all the Dublin Core elements found as DublinCore::Element
180 objects which you can then manipulate further.
181  
182 foreach my $element ( $record->elements() ) {
183 print "name=", $element->name(), "\n";
184 print "content=", $element->content(), "\n";
185 }
186  
187 =cut
188  
189 2 ▶sub elements {
Subroutine elements: called CC=2 SCAR=6.9
Pod documented
190 2 my $self = shift;
191 2 my @elements = ();
192 2 foreach my $type ( @VALID_ELEMENTS ) {
193 30 push( @elements, @{ $self->{ "DC_$type" } } );
194 }
195 2 return( @elements );
196 }
197  
198 =head2 title()
199  
200 Returns a DublinCore::Element object for the title element. You can then
201 retrieve content, qualifier, scheme, lang attributes like so.
202  
203 my $title = $record->title();
204 print "content: ", $title->content(), "\n";
205 print "qualifier: ", $title->qualifier(), "\n";
206 print "scheme: ", $title->scheme(), "\n";
207 print "language: ", $title->language(), "\n";
208  
209 Since there can be multiple instances of a particular element type (title,
210 creator, subject, etc) you can retrieve multiple title elements by calling
211 title() in a list context.
212  
213 my @titles = $record->title();
214 foreach my $title ( @titles ) {
215 print "title: ", $title->content(), "\n";
216 }
217  
218 =cut
219  
220 2 ▶sub title {
Subroutine title: called CC=1 SCAR=0.0
Pod documented
221 2 my $self = shift;
222 2 return( $self->_getElement( 'title', wantarray ) );
223 }
224  
225 =head2 creator()
226  
227 Retrieve creator information in the same manner as title().
228  
229 =cut
230  
231 2 ▶sub creator {
Subroutine creator: called CC=1 SCAR=0.0
Pod documented
232 2 my $self = shift;
233 2 return( $self->_getElement( 'creator', wantarray ) );
234 }
235  
236 =head2 subject()
237  
238 Retrieve subject information in the same manner as title().
239  
240 =cut
241  
242 2 ▶sub subject {
Subroutine subject: called CC=1 SCAR=0.0
Pod documented
243 2 my $self = shift;
244 2 return( $self->_getElement( 'subject', wantarray ) );
245 }
246  
247 =head2 description()
248  
249 Retrieve description information in the same manner as title().
250  
251 =cut
252  
253 2 ▶sub description {
Subroutine description: called CC=1 SCAR=0.0
Pod documented
254 2 my $self = shift;
255 2 return( $self->_getElement( 'description', wantarray ) );
256 }
257  
258 =head2 publisher()
259  
260 Retrieve publisher information in the same manner as title().
261  
262 =cut
263  
264 2 ▶sub publisher {
Subroutine publisher: called CC=1 SCAR=0.0
Pod documented
265 2 my $self = shift;
266 2 return( $self->_getElement( 'publisher', wantarray ) );
267 }
268  
269 =head2 contributor()
270  
271 Retrieve contributor information in the same manner as title().
272  
273 =cut
274  
275 3 ▶sub contributor {
Subroutine contributor: called CC=1 SCAR=0.0
Pod documented
276 3 my $self = shift;
277 3 return( $self->_getElement( 'contributor', wantarray ) );
278 }
279  
280 =head2 date()
281  
282 Retrieve date information in the same manner as title().
283  
284 =cut
285  
286 2 ▶sub date {
Subroutine date: called CC=1 SCAR=0.0
Pod documented
287 2 my $self = shift;
288 2 return( $self->_getElement( 'date', wantarray ) );
289 }
290  
291 =head2 type()
292  
293 Retrieve type information in the same manner as title().
294  
295 =cut
296  
297 2 ▶sub type {
Subroutine type: called CC=1 SCAR=0.0
Pod documented
298 2 my $self = shift;
299 2 return( $self->_getElement( 'type', wantarray ) );
300 }
301  
302 =head2 format()
303  
304 Retrieve format information in the same manner as title().
305  
306 =cut
307  
308 2 ▶sub format {
Subroutine format: called CC=1 SCAR=0.0
Pod documented
309 2 my $self = shift;
310 2 return( $self->_getElement( 'format', wantarray ) );
311 }
312  
313 =head2 identifier()
314  
315 Retrieve identifier information in the same manner as title().
316  
317 =cut
318  
319 2 ▶sub identifier {
Subroutine identifier: called CC=1 SCAR=0.0
Pod documented
320 2 my $self = shift;
321 2 return( $self->_getElement( 'identifier', wantarray ) );
322 }
323  
324 =head2 source()
325  
326 Retrieve source information in the same manner as title().
327  
328 =cut
329  
330 2 ▶sub source {
Subroutine source: called CC=1 SCAR=0.0
Pod documented
331 2 my $self = shift;
332 2 return( $self->_getElement( 'source', wantarray ) );
333 }
334  
335 =head2 language()
336  
337 Retrieve language information in the same manner as title().
338  
339 =cut
340  
341 2 ▶sub language {
Subroutine language: called CC=1 SCAR=0.0
Pod documented
342 2 my $self = shift;
343 2 return( $self->_getElement( 'language', wantarray ) );
344 }
345  
346 =head2 relation()
347  
348 Retrieve relation information in the same manner as title().
349  
350 =cut
351  
352 2 ▶sub relation {
Subroutine relation: called CC=1 SCAR=0.0
Pod documented
353 2 my $self = shift;
354 2 return( $self->_getElement( 'relation', wantarray ) );
355 }
356  
357 =head2 coverage()
358  
359 Retrieve coverage information in the same manner as title().
360  
361 =cut
362  
363 2 ▶sub coverage {
Subroutine coverage: called CC=1 SCAR=0.0
Pod documented
364 2 my $self = shift;
365 2 return( $self->_getElement( 'coverage', wantarray ) );
366 }
367  
368 =head2 rights()
369  
370 Retrieve rights information in the same manner as title().
371  
372 =cut
373  
374 2 ▶sub rights {
Subroutine rights: called CC=1 SCAR=0.0
Pod documented
375 2 my $self = shift;
376 2 return( $self->_getElement( 'rights', wantarray ) );
377 }
378  
379 31 ▶sub _getElement {
Subroutine _getElement: called CC=3 SCAR=12.7
380 31 my ( $self, $element, $wantarray ) = @_;
381 31 my $contents = $self->{ "DC_$element" };
382  
383 31 ▶ if ( $wantarray ) {
Branch: 2 branches
BranchTrueFalseTotal
if ($wantarray) { } 0 31 31
elsif (scalar @$contents > 0) { } 31 0 31
384 0 return( @$contents );
385 }
386 elsif ( scalar( @$contents ) > 0 ) {
387 31 return( $contents->[ 0 ] );
388 }
389  
390 0 return DublinCore::Element->new();
391 }
392  
393 =head1 SEE ALSO
394  
395 =over 4
396  
397 =item * DublinCore::Element
398  
399 =item * Dublin Core L<http://www.dublincore.org/>
400  
401 =item * RFC 2731 L<http://www.ietf.org/rfc/rfc2731>
402  
403 =item * perl4lib L<http://www.rice.edu/perl4lib>
404  
405 =back
406  
407 =head1 AUTHOR
408  
409 =over 4
410  
411 =item * Ed Summers E<lt>ehs@pobox.comE<gt>
412  
413 =item * Brian Cassidy E<lt>bricas@cpan.orgE<gt>
414  
415 =back
416  
417 =head1 COPYRIGHT AND LICENSE
418  
419 Copyright 2007 by Ed Summers, Brian Cassidy
420  
421 This library is free software; you can redistribute it and/or modify
422 it under the same terms as Perl itself.
423  
424 =cut
425  
426 5 1;
↑ Summary Element.pm »