PropertyList.pm

filter: Statementstmt 84.5% 323 / 382 Branchbran 64.9% 74 / 114 Conditioncond 54.5% 6 / 11 MC/DCmcdc 14.2% 1 / 7 Subroutinesub 86.8% 86 / 99 Podpod 100% 21 / 21 totaltotal 80.6% 511 / 634 CC 65 SCAR 14.6
CC
65
Cov
79%
create_from_hash
20.0
create_from
20.0
read_next
12.1
CRAP 4.3
« PropertyList/WriteBinary.pm ↑ Summary PropertyList/ReadBinary.pm »
1 24 ▶use v5.10;
Subroutine BEGIN: called CC=1 SCAR=0.0
2  
3 package Mac::PropertyList;
4 24 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
5  
6 24 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 24 ▶no warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
8  
9 24 ▶use vars qw($ERROR);
Subroutine BEGIN: called CC=1 SCAR=0.0
10 24 ▶use Carp qw(croak carp);
Subroutine BEGIN: called CC=1 SCAR=0.0
11 24 ▶use Data::Dumper;
Subroutine BEGIN: called CC=1 SCAR=0.0
12 24 ▶use HTML::Entities;
Subroutine BEGIN: called CC=1 SCAR=0.0
13 24 ▶use XML::Entities;
Subroutine BEGIN: called CC=1 SCAR=0.0
14  
15 24 ▶BEGIN {
Subroutine BEGIN: called CC=1 SCAR=0.0
16 %HTML::Entities::char2entity = %{
17 # XML::Entities::Data::char2entity('all');
18 # We explicitly do not want *all* here. 'all' in the XML::Entities module
19 # is JUST PLAIN WRONG, as these are HTML entities that are NOT part of XML.
20  
21 24 {
22 24 '&' => '&',
23 '<' => '&lt;',
24 '>' => '&gt;',
25 "'" => "&apos;",
26 '"' => '&quot;',
27 }
28 };
29 }
30  
31 24 ▶use Exporter qw(import);
Subroutine BEGIN: called CC=1 SCAR=0.0
32  
33 24 our @EXPORT_OK = qw(
34 parse_plist
35 parse_plist_fh
36 parse_plist_file
37 plist_as_string
38 create_from_hash
39 create_from_array
40 create_from_string
41 );
42  
43 24 our %EXPORT_TAGS = (
44 'all' => \@EXPORT_OK,
45 );
46  
47 24 our $VERSION = '1.608';
48  
49 =encoding utf8
50  
51 =head1 NAME
52  
53 Mac::PropertyList - work with Mac plists at a low level
54  
55 =head1 SYNOPSIS
56  
57 use Mac::PropertyList qw(:all);
58  
59 my $data = parse_plist( $text );
60 my $perl = $data->as_perl;
61  
62 # == OR ==
63 my $data = parse_plist_file( $filename );
64  
65 # == OR ==
66 open my( $fh ), $filename or die "...";
67 my $data = parse_plist_fh( $fh );
68  
69  
70 my $text = plist_as_string( $data );
71  
72 my $plist = create_from_hash( \%hash );
73 my $plist = create_from_array( \@array );
74  
75 my $plist = Mac::PropertyList::dict->new( \%hash );
76  
77 my $perl = $plist->as_perl;
78  
79 =head1 DESCRIPTION
80  
81 This module is a low-level interface to the Mac OS X Property List
82 (plist) format in either XML or binary. You probably shouldn't use
83 this in applications–build interfaces on top of this so you don't have
84 to put all the heinous multi-level object stuff where people have to
85 look at it.
86  
87 You can parse a plist file and get back a data structure. You can take
88 that data structure and get back the plist as XML. If you want to
89 change the structure inbetween that's your business. :)
90  
91 You don't need to be on Mac OS X to use this. It simply parses and
92 manipulates a text format that Mac OS X uses.
93  
94 If you need to work with the old ASCII or newer JSON formet, you can
95 use the B<plutil> tool that comes with MacOS X:
96  
97 % plutil -convert xml1 -o ExampleBinary.xml.plist ExampleBinary.plist
98  
99 Or, you can extend this module to handle those formats (and send a pull
100 request).
101  
102 =head2 The Property List format
103  
104 The MacOS X Property List format is simple XML. You can read the DTD
105 to get the details.
106  
107 http://www.apple.com/DTDs/PropertyList-1.0.dtd
108  
109 One big problem exists—its dict type uses a flat structure to list
110 keys and values so that values are only associated with their keys by
111 their position in the file rather than by the structure of the DTD.
112 This problem is the major design hinderance in this module. A smart
113 XML format would have made things much easier.
114  
115 If the parse_plist encounters an empty key tag in a dict structure
116 (i.e. C<< <key></key> >> ) the function croaks.
117  
118 =head2 The Mac::PropertyList classes
119  
120 A plist can have one or more of any of the plist objects, and we have
121 to remember the type of thing so we can go back to the XML format.
122 Perl treats numbers and strings the same, but the plist format
123 doesn't.
124  
125 Therefore, everything C<Mac::PropertyList> creates is an object of some
126 sort. Container objects like C<Mac::PropertyList::array> and
127 C<Mac::PropertyList::dict> hold other objects.
128  
129 There are several types of objects:
130  
131 Mac::PropertyList::string
132 Mac::PropertyList::data
133 Mac::PropertyList::real
134 Mac::PropertyList::integer
135 Mac::PropertyList::uid
136 Mac::PropertyList::date
137 Mac::PropertyList::array
138 Mac::PropertyList::dict
139 Mac::PropertyList::true
140 Mac::PropertyList::false
141  
142 Note that the Xcode property list editor abstracts the C<true> and
143 C<false> objects as just C<Boolean>. They are separate tags in the
144 plist format though.
145  
146 =over 4
147  
148 =item new( VALUE )
149  
150 Create the object.
151  
152 =item value
153  
154 Access the value of the object. At the moment you cannot change the
155 value
156  
157 =item type
158  
159 Access the type of the object (string, data, etc)
160  
161 =item write
162  
163 Create a string version of the object, recursively if necessary.
164  
165 =item as_perl
166  
167 Turn the plist data structure, which is decorated with extra
168 information, into a lean Perl data structure without the value type
169 information or blessed objects.
170  
171 =back
172  
173 =cut
174  
175 24 ▶my $Debug = $ENV{PLIST_DEBUG} || 0;
Condition50%1/2
$ENV{'PLIST_DEBUG'} || 0
l!l
024
MC/DC0%0/1
$ENV{'PLIST_DEBUG'} || 0
$ENV{'PLIST_DEBUG'}
Truth table50%1/2
A
A= $ENV{'PLIST_DEBUG'}
Aresult
11
00
176  
177 24 my %Readers = (
178 "dict" => \&read_dict,
179 "string" => \&read_string,
180 "date" => \&read_date,
181 "real" => \&read_real,
182 "integer" => \&read_integer,
183 "array" => \&read_array,
184 "data" => \&read_data,
185 "true" => \&read_true,
186 "false" => \&read_false,
187 );
188  
189 24 my $Options = {ignore => ['<true/>', '<false/>']};
190  
191 =head1 FUNCTIONS
192  
193 These functions are available for individual or group import. Nothing
194 will be imported unless you ask for it.
195  
196 use Mac::PropertyList qw( parse_plist );
197  
198 use Mac::PropertyList qw( :all );
199  
200 =head2 Things that parse
201  
202 =over 4
203  
204 =item parse_plist( TEXT )
205  
206 Parse the XML plist in TEXT and return the C<Mac::PropertyList>
207 object.
208  
209 =cut
210  
211 # This will change to parse_plist_ref when we create the dispatcher
212  
213 49 ▶sub parse_plist {
Subroutine parse_plist: called CC=3 SCAR=11.0
Pod documented
214 49 my $text = shift;
215  
216 49 my $plist = do {
217 49 ▶ if( $text =~ /\A<\?xml/ ) { # XML plists
Branch: 2 branches
BranchTrueFalseTotal
if ($text =~ /\A<\?xml/) { } 38 11 49
elsif ($text =~ /\Abplist/) { } 6 5 11
218 38 $text =~ s/<!--(?:[\d\D]*?)-->//g;
219 # we can handle either 0.9 or 1.0
220 38 $text =~ s|^<\?xml.*?>\s*<!DOC.*>\s*<plist.*?>\s*||;
221 38 $text =~ s|\s*</plist>\s*$||;
222  
223 38 my $text_source = Mac::PropertyList::TextSource->new( $text );
224 38 read_next( $text_source );
225 }
226 elsif( $text =~ /\Abplist/ ) { # binary plist
227 6 require Mac::PropertyList::ReadBinary;
228 6 my $parser = Mac::PropertyList::ReadBinary->new( \$text );
229 6 $parser->plist;
230 }
231 else {
232 5 croak( "This doesn't look like a valid plist format!" );
233 }
234 };
235 }
236  
237 =item parse_plist_fh( FILEHANDLE )
238  
239 Parse the XML plist from FILEHANDLE and return the C<Mac::PropertyList>
240 data structure. Returns false if the arguments is not a reference.
241  
242 You can do this in a couple of ways. You can open the file with a
243 lexical filehandle (since Perl 5.6).
244  
245 open my( $fh ), $file or die "...";
246 parse_plist_fh( $fh );
247  
248 Or, you can use a bareword filehandle and pass a reference to its
249 typeglob. I don't recommmend this unless you are using an older
250 Perl.
251  
252 open FILE, $file or die "...";
253 parse_plist_fh( \*FILE );
254  
255 =cut
256  
257 2 ▶sub parse_plist_fh {
Subroutine parse_plist_fh: called CC=1 SCAR=0.0
Pod documented
258 2 my $fh = shift;
259  
260 2 my $text = do { local $/; <$fh> };
261  
262 2 parse_plist( $text );
263 }
264  
265 =item parse_plist_file( FILE_PATH )
266  
267 Parse the XML plist in FILE_PATH and return the C<Mac::PropertyList>
268 data structure. Returns false if the file does not exist.
269  
270 Alternately, you can pass a filehandle reference, but that just
271 calls C<parse_plist_fh> for you.
272  
273 =cut
274  
275 12 ▶sub parse_plist_file {
Subroutine parse_plist_file: called CC=3 SCAR=11.2
Pod documented
276 12 my $file = shift;
277  
278 0 ▶ if( ref $file ) { return parse_plist_fh( $file ) }
Branch: 1 branch
BranchTrueFalseTotal
if (ref $file) 0 12 12
279  
280 12 ▶ unless( -e $file ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (-e $file) 1 11 12
281 1 croak( "parse_plist_file: file [$file] does not exist!" );
282 0 return;
283 }
284  
285 11 my $text = do { local $/; open my($fh), '<:raw', $file; <$fh> };
286  
287 11 parse_plist( $text );
288 }
289  
290 =item create_from_hash( HASH_REF )
291  
292 Create a plist dictionary from the hash reference.
293  
294 The values of the hash can only be simple scalars–not references.
295 Reference values are silently ignored.
296  
297 Returns a string representing the hash in the plist format.
298  
299 =cut
300  
301 0 ▶sub create_from_hash {
Subroutine create_from_hash: not called CC=4 SCAR=30.0
Pod documented
302 0 my $hash = shift;
303  
304 0 ▶ unless( ref $hash eq ref {} ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (ref $hash eq ref {}) 0 0 0
305 0 carp "create_from_hash did not get an hash reference";
306 0 return;
307 }
308  
309 0 my $string = XML_head() . Mac::PropertyList::dict->write_open . "\n";
310  
311 0 foreach my $key ( keys %$hash ) {
312 0 ▶ next if ref $hash->{$key};
Branch: 1 branch
BranchTrueFalseTotal
if ref $hash->{$key} 0 0 0
313  
314 0 my $bit = Mac::PropertyList::dict->write_key( $key ) . "\n";
315 0 my $value = Mac::PropertyList::string->new( $hash->{$key} );
316  
317 0 $bit .= $value->write . "\n";
318  
319 0 $bit =~ s/^/\t/gm;
320  
321 0 $string .= $bit;
322 }
323  
324 0 $string .= Mac::PropertyList::dict->write_close . "\n" . XML_foot();
325  
326 0 return $string;
327 }
328  
329 =item create_from_array( ARRAY_REF )
330  
331 Create a plist array from the array reference.
332  
333 The values of the array can only be simple scalars–not references.
334 Reference values are silently ignored.
335  
336 Returns a string representing the array in the plist format.
337  
338 =cut
339  
340 0 ▶sub create_from_array {
Subroutine create_from_array: not called CC=3 SCAR=24.8
Pod documented
341 0 my $array = shift;
342  
343 0 ▶ unless( ref $array eq ref [] ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (ref $array eq ref []) 0 0 0
344 0 carp "create_from_array did not get an array reference";
345 0 return;
346 }
347  
348 0 my $string = XML_head() . Mac::PropertyList::array->write_open . "\n";
349  
350 0 foreach my $element ( @$array ) {
351 0 my $value = Mac::PropertyList::string->new( $element );
352  
353 0 my $bit .= $value->write . "\n";
354 0 $bit =~ s/^/\t/gm;
355  
356 0 $string .= $bit;
357 }
358  
359 0 $string .= Mac::PropertyList::array->write_close . "\n" . XML_foot();
360  
361 0 return $string;
362 }
363  
364 =item create_from_string( STRING )
365  
366 Returns a string representing the string in the plist format.
367  
368 =cut
369  
370 1 ▶sub create_from_string {
Subroutine create_from_string: called CC=2 SCAR=8.4
Pod documented
371 1 my $string = shift;
372  
373 1 ▶ unless( ! ref $string ) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $string) 0 1 1
374 0 carp "create_from_string did not get a string";
375 0 return;
376 }
377  
378 return
379 1 XML_head() .
380 Mac::PropertyList::string->new( $string )->write .
381 "\n" . XML_foot();
382 }
383  
384 =item create_from
385  
386 Dispatches to either C<create_from_array>, C<create_from_hash>, or
387 C<create_from_string> based on the argument. If none of those fit,
388 this C<croak>s.
389  
390 =cut
391  
392 0 ▶sub create_from {
Subroutine create_from: not called CC=4 SCAR=30.0
Pod documented
393 0 my $thingy = shift;
394  
395 0 return do {
396 0 ▶ if( ref $thingy eq ref [] ) { &create_from_array }
Branch: 3 branches
BranchTrueFalseTotal
if (ref $thingy eq ref []) { } 0 0 0
elsif (ref $thingy eq ref {}) { } 0 0 0
elsif (!ref($thingy) eq ref {}) { } 0 0 0
397 0 elsif( ref $thingy eq ref {} ) { &create_from_hash }
398 0 elsif( ! ref $thingy eq ref {} ) { &create_from_string }
399 else {
400 0 croak "Did not recognize argument! Must be a string, or reference to a hash or array";
401 }
402 };
403 }
404  
405 =item read_string
406  
407 =item read_data
408  
409 =item read_integer
410  
411 =item read_date
412  
413 =item read_real
414  
415 =item read_true
416  
417 =item read_false
418  
419 Reads a certain sort of property list data
420  
421 =cut
422  
423 73 ▶sub read_string { Mac::PropertyList::string ->new( XML::Entities::decode( 'all', $_[0] ) ) }
Subroutine read_string: called CC=1 SCAR=0.0
Pod documented
424 24 ▶sub read_integer { Mac::PropertyList::integer->new( $_[0] ) }
Subroutine read_integer: called CC=1 SCAR=0.0
Pod documented
425 2 ▶sub read_date { Mac::PropertyList::date ->new( $_[0] ) }
Subroutine read_date: called CC=1 SCAR=0.0
Pod documented
426 3 ▶sub read_real { Mac::PropertyList::real ->new( $_[0] ) }
Subroutine read_real: called CC=1 SCAR=0.0
Pod documented
427 3 ▶sub read_true { Mac::PropertyList::true ->new }
Subroutine read_true: called CC=1 SCAR=0.0
Pod documented
428 0 ▶sub read_false { Mac::PropertyList::false ->new }
Subroutine read_false: not called CC=1 SCAR=6.9
Pod documented
429  
430 =item read_next
431  
432 Read the next data item
433  
434 =cut
435  
436 161 ▶sub read_next {
Subroutine read_next: called CC=11 SCAR=25.0
Pod documented
437 161 my $source = shift;
438  
439 161 local $_ = '';
440 161 my $value;
441  
442 161 ▶ while( not defined $value ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $value) 680 161 841
443 680 ▶ croak "Couldn't read anything!" if $source->eof;
Branch: 1 branch
BranchTrueFalseTotal
if $source->eof 0 680 680
444 680 $_ .= $source->get_line;
445 680 ▶ if( s[^\s* < (string|date|real|integer|data) > \s*(.*?)\s* </\1> ][]sx ) {
Branch: 6 branches
BranchTrueFalseTotal
if (s[^\s* < (string|date|real|integer|data) > \s*(.*?)\s* </\1> ][]sx) { } 118 562 680
elsif (s[^\s* < string / > ][]x) { } 2 560 562
elsif (s/^\s* < (dict|array) > //x) { } 38 522 560
elsif (s[^\s* < dict / > ][]x) { } 0 522 522
elsif (s[^\s* < array / > ][]x) { } 0 522 522
elsif (s[^\s* < (true|false) /> ][]x) { } 3 519 522
446 118 $value = $Readers{$1}->( $2 );
447 }
448 elsif( s[^\s* < string / > ][]x ){
449 2 $value = $Readers{'string'}->( '' );
450 }
451 elsif( s[^\s* < (dict|array) > ][]x ) {
452 # We need to put back the unprocessed text if
453 # any because the <dict> and <array> readers
454 # need to see it.
455 38 ▶ $source->put_line( $_ ) if defined $_ && '' ne $_;
Branch: 1 branch
BranchTrueFalseTotal
if defined $_ and "" ne $_ 38 0 38
Condition33%1/3
defined $_ and "" ne $_
!ll&&!rl&&r
0038
MC/DC0%0/2
defined $_ and "" ne $_
defined $_ "" ne $_
Truth table33%1/3
A and B
A= defined $_B= "" ne $_
ABresult
0X0
100
111
456 38 $_ = '';
457 38 $value = $Readers{$1}->( $source );
458 }
459 # these next two are some wierd cases i found in the iPhoto Prefs
460 elsif( s[^\s* < dict / > ][]x ) {
461 0 $value = Mac::PropertyList::dict->new();
462 }
463 elsif( s[^\s* < array / > ][]x ) {
464 0 $value = Mac::PropertyList::array->new();
465 }
466 elsif( s[^\s* < (true|false) /> ][]x ) {
467 3 $value = $Readers{$1}->();
468 }
469 }
470 161 $source->put_line($_);
471 161 return $value;
472 }
473  
474 =item read_dict
475  
476 Read a dictionary
477  
478 =cut
479  
480 24 ▶sub read_dict {
Subroutine read_dict: called CC=7 SCAR=20.7
Pod documented
481 24 my $source = shift;
482  
483 24 my %hash;
484 24 local $_ = $source->get_line;
485 24 ▶ while( not s|^\s*</dict>|| ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (s[^\s*</dict>][]) 77 24 101
486 77 my $key;
487 77 ▶ while (not defined $key) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $key) 77 77 154
488 77 ▶ if (s[^\s*<key>(.*?)</key>][]s) {
Branch: 1 branch
BranchTrueFalseTotal
if (s[^\s*<key>(.*?)</key>][]s) { } 77 0 77
489 77 $key = HTML::Entities::decode($1);
490 # Bring this back if you want this behavior:
491 # croak "Key is empty string!" if $key eq '';
492 }
493 else {
494 0 ▶ croak "Could not read key!" if $source->eof;
Branch: 1 branch
BranchTrueFalseTotal
if $source->eof 0 0 0
495 0 $_ .= $source->get_line;
496 }
497 }
498  
499 77 $source->put_line( $_ );
500 77 $hash{ $key } = read_next( $source );
501 77 $_ = $source->get_line;
502 }
503  
504 24 $source->put_line( $_ );
505 24 ▶ if ( 1 == keys %hash && exists $hash{'CF$UID'} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (1 == keys %hash and exists $hash{'CF$UID'}) { } 0 24 24
Condition66%2/3
1 == keys %hash and exists $hash{"CF\$UID"}
!ll&&!rl&&r
1770
MC/DC0%0/2
1 == keys %hash and exists $hash{"CF\$UID"}
1 == keys %hash exists $hash{"CF\$UID"}
Truth table66%2/3
A and B
A= 1 == keys %hashB= exists $hash{"CF\$UID"}
ABresult
0X0
100
111
506 # This is how plutil represents a UID in XML.
507 0 return Mac::PropertyList::uid->integer( $hash{'CF$UID'}->value );
508 }
509 else {
510 24 return Mac::PropertyList::dict->new( \%hash );
511 }
512 }
513  
514 =item read_array
515  
516 Read an array
517  
518 =cut
519  
520 14 ▶sub read_array {
Subroutine read_array: called CC=2 SCAR=6.9
Pod documented
521 14 my $source = shift;
522  
523 14 my @array = ();
524  
525 14 local $_ = $source->get_line;
526 14 ▶ while( not s|^\s*</array>|| ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (s[^\s*</array>][]) 46 14 60
527 46 $source->put_line( $_ );
528 46 push @array, read_next( $source );
529 46 $_ = $source->get_line;
530 }
531  
532 14 $source->put_line( $_ );
533 14 return Mac::PropertyList::array->new( \@array );
534 }
535  
536 18 ▶sub read_data {
Subroutine read_data: called CC=1 SCAR=0.0
Pod documented
537 18 my $string = shift;
538  
539 18 require MIME::Base64;
540  
541 18 $string = MIME::Base64::decode_base64($string);
542  
543 18 return Mac::PropertyList::data->new( $string );
544 }
545  
546 =back
547  
548 =head2 Things that write
549  
550 =over 4
551  
552 =item XML_head
553  
554 Returns a string that represents the start of the PList XML.
555  
556 =cut
557  
558 8 ▶sub XML_head () {
Subroutine XML_head: called CC=1 SCAR=0.0
Pod documented
559 8 <<"XML";
560 <?xml version="1.0" encoding="UTF-8"?>
561 <!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
562 <plist version="1.0">
563 XML
564 }
565  
566 =item XML_foot
567  
568 Returns a string that represents the end of the PList XML.
569  
570 =cut
571  
572 8 ▶sub XML_foot () {
Subroutine XML_foot: called CC=1 SCAR=0.0
Pod documented
573 8 <<"XML";
574 </plist>
575 XML
576 }
577  
578 =item plist_as_string
579  
580 Return the plist data structure as XML in the Mac Property List format.
581  
582 =cut
583  
584 7 ▶sub plist_as_string {
Subroutine plist_as_string: called CC=1 SCAR=0.0
Pod documented
585 7 my $object = CORE::shift;
586  
587 7 my $string = XML_head();
588  
589 7 $string .= $object->write . "\n";
590  
591 7 $string .= XML_foot();
592  
593 7 return $string;
594 }
595  
596 =item plist_as_perl
597  
598 Return the plist data structure as an unblessed Perl data structure.
599 There won't be any C<Mac::PropertyList> objects in the results. This
600 is really just C<as_perl>.
601  
602 =cut
603  
604 0 ▶sub plist_as_perl { $_[0]->as_perl }
Subroutine plist_as_perl: not called CC=1 SCAR=6.9
Pod documented
605  
606 =back
607  
608 =cut
609  
610 # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # #
611 package Mac::PropertyList::Source;
612 38 ▶sub new {
Subroutine new: called CC=1 SCAR=0.0
613 38 my $self = bless { buffer => [], source => $_[1] }, $_[0];
614 38 return $self;
615 }
616  
617 680 ▶sub eof { (not @{$_[0]->{buffer}}) and $_[0]->source_eof }
Subroutine eof: called CC=2 SCAR=6.9
Branch: 1 branch
BranchTrueFalseTotal
not @{$_[0]{"buffer"};} and $_[0]->source_eof 557 123 680
618  
619 841 ▶sub get_line {
Subroutine get_line: called CC=4 SCAR=13.9
620 841 my $self = CORE::shift;
621  
622 # I'm not particularly happy with what I wrote here, but that's why
623 # you shouldn't write your own buffering code! I might have left over
624 # text in the buffer. This could be stuff a higher level looked at and
625 # put back with put_line. If there's text there, grab that.
626 #
627 # But here's the tricky part. If that next part of the text looks like
628 # a "blank" line, grab the next next thing and append that.
629 #
630 # And, if there's nothing in the buffer, ask for more text from
631 # get_source_line. Follow the same rules. IF you get back something that
632 # looks like a blank line, ask for another and append it.
633 #
634 # This means that a returned line might have come partially from the
635 # buffer and partially from a fresh read.
636 #
637 # At some point you should have something that doesn't look like a
638 # blank line and the while() will break out. Return what you do.
639 #
640 # Previously, I wasn't appending to $_ so newlines were disappearing
641 # as each next read replaced the value in $_. Yuck.
642  
643 841 local $_ = '';
644 841 ▶ while (defined $_ && /^[\r\n\s]*$/) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $_ and /^[\r\n\s]*$/) 1078 841 1919
Condition66%2/3
defined $_ and /^[\r\n\s]*$/
!ll&&!rl&&r
08411078
MC/DC50%1/2
defined $_ and /^[\r\n\s]*$/
defined $_ /^[\r\n\s]*$/
Truth table66%2/3
A and B
A= defined $_B= /^[\r\n\s]*$/
ABresult
0X0
100
111
645 1078 ▶ if( @{$self->{buffer}} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (@{$self->{'buffer'};}) { } 293 785 1078
646 293 $_ .= shift @{$self->{buffer}};
647 }
648 else {
649 785 $_ .= $self->get_source_line;
650 }
651 }
652  
653 841 return $_;
654 }
655  
656 360 ▶sub put_line { unshift @{$_[0]->{buffer}}, $_[1] }
Subroutine put_line: called CC=1 SCAR=0.0
657  
658 # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # #
659 package Mac::PropertyList::LineListSource;
660 24 ▶use base qw(Mac::PropertyList::Source);
Subroutine BEGIN: called CC=1 SCAR=0.0
661  
662 0 ▶sub get_source_line { return shift @{$_->{source}} if @{$_->{source}} }
Subroutine get_source_line: not called CC=2 SCAR=17.9
Branch: 1 branch
BranchTrueFalseTotal
if @{$_->{"source"};} 0 0 0
663  
664 0 ▶sub source_eof { not @{$_[0]->{source}} }
Subroutine source_eof: not called CC=1 SCAR=6.9
665  
666 # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # #
667 package Mac::PropertyList::TextSource;
668 24 ▶use base qw(Mac::PropertyList::Source);
Subroutine BEGIN: called CC=1 SCAR=0.0
669  
670 785 ▶sub get_source_line {
Subroutine get_source_line: called CC=1 SCAR=0.0
671 785 my $self = CORE::shift;
672 785 $self->{source} =~ s/(.*(\r|\n|$))//;
673 785 $1;
674 }
675  
676 557 ▶sub source_eof { not $_[0]->{source} }
Subroutine source_eof: called CC=1 SCAR=0.0
677  
678 # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # #
679 package Mac::PropertyList::Item;
680 0 ▶sub type_value { ( $_[0]->type, $_[0]->value ) }
Subroutine type_value: not called CC=1 SCAR=6.9
681  
682 732 ▶sub value {
Subroutine value: called CC=5 SCAR=16.2
683 732 my $ref = $_[0]->type;
684  
685 732 do {
686 732 ▶ if( $ref eq 'array' ) { wantarray ? @{ $_[0] } : $_[0] }
Branch: 3 branches
BranchTrueFalseTotal
if ($ref eq 'array') { } 44 688 732
elsif ($ref eq 'dict') { } 137 551 688
wantarray ? : 5 39 44
687 0 ▶ elsif( $ref eq 'dict' ) { wantarray ? %{ $_[0] } : $_[0] }
Branch: 1 branch
BranchTrueFalseTotal
wantarray ? : 0 137 137
688 551 else { ${ $_[0] } }
689 };
690 }
691  
692 825 ▶sub type { my $r = ref $_[0] ? ref $_[0] : $_[0]; $r =~ s/.*:://; $r; }
Subroutine type: called CC=2 SCAR=7.1
Branch: 1 branch
BranchTrueFalseTotal
ref $_[0] ? : 825 0 825
693  
694 724 ▶sub new {
Subroutine new: called CC=1 SCAR=0.0
695 724 bless $_[1], $_[0]
696 }
697  
698 34 ▶sub write_open { $_[0]->write_either(); }
Subroutine write_open: called CC=1 SCAR=0.0
699 34 ▶sub write_close { $_[0]->write_either('/'); }
Subroutine write_close: called CC=1 SCAR=0.0
700  
701 68 ▶sub write_either {
Subroutine write_either: called CC=2 SCAR=6.9
702 68 ▶ my $slash = defined $_[1] ? '/' : '';
Branch: 1 branch
BranchTrueFalseTotal
defined $_[1] ? : 34 34 68
703  
704 68 my $type = $_[0]->type;
705  
706 68 "<$slash$type>";
707 }
708  
709 3 ▶sub write_empty { my $type = $_[0]->type; "<$type/>"; }
Subroutine write_empty: called CC=1 SCAR=0.0
710  
711 # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # #
712 package Mac::PropertyList::Container;
713 24 ▶use base qw(Mac::PropertyList::Item);
Subroutine BEGIN: called CC=1 SCAR=0.0
714  
715 117 ▶sub new {
Subroutine new: called CC=4 SCAR=13.9
716 117 my $class = CORE::shift;
717 117 my $item = CORE::shift;
718  
719 117 ▶ if( ref $item ) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $item) 113 4 117
720 113 return bless $item, $class;
721 }
722  
723 4 my $empty = do {
724 4 ▶ if( $class =~ m/array$/ ) { [] }
Branch: 2 branches
BranchTrueFalseTotal
if ($class =~ /array$/) { } 2 2 4
elsif ($class =~ /dict$/) { } 2 0 2
725 2 elsif( $class =~ m/dict$/ ) { {} }
726 };
727  
728 4 $class->SUPER::new( $empty );
729 }
730  
731 # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # #
732 package Mac::PropertyList::array;
733 24 ▶use base qw(Mac::PropertyList::Container);
Subroutine BEGIN: called CC=1 SCAR=0.0
734  
735 0 ▶sub shift { CORE::shift @{ $_[0]->value } }
Subroutine shift: not called CC=1 SCAR=6.9
736 0 ▶sub unshift { }
Subroutine unshift: not called CC=1 SCAR=0.0
737 0 ▶sub pop { CORE::pop @{ $_[0]->value } }
Subroutine pop: not called CC=1 SCAR=6.9
738 0 ▶sub push { }
Subroutine push: not called CC=1 SCAR=0.0
739 0 ▶sub splice { }
Subroutine splice: not called CC=1 SCAR=0.0
740 3 ▶sub count { return scalar @{ $_[0]->value } }
Subroutine count: called CC=1 SCAR=0.0
741 17 ▶sub _elements { @{ $_[0]->value } } # the raw, unprocessed elements
Subroutine _elements: called CC=1 SCAR=0.0
742 2 ▶sub values {
Subroutine values: called CC=2 SCAR=7.1
743 2 my @v = map { $_->value } $_[0]->_elements;
744 2 ▶ wantarray ? @v : \@v
Branch: 1 branch
BranchTrueFalseTotal
wantarray ? : 2 0 2
745 }
746  
747 3 ▶sub as_basic_data {
Subroutine as_basic_data: called CC=2 SCAR=7.0
748 3 my $self = CORE::shift;
749 return
750 [ map
751 {
752 3 ▶ eval { $_->can('as_basic_data') } ? $_->as_basic_data : $_
Branch: 1 branch
BranchTrueFalseTotal
eval { do { $_->can('as_basic_data') } } ? : 7 0 7
753 } @$self
754 ];
755 }
756  
757 3 ▶sub write {
Subroutine write: called CC=2 SCAR=6.9
758 3 my $self = CORE::shift;
759  
760 3 my $string = $self->write_open . "\n";
761  
762 3 foreach my $element ( @$self ) {
763 11 my $bit = $element->write;
764  
765 11 $bit =~ s/^/\t/gm;
766  
767 11 $string .= $bit . "\n";
768 }
769  
770 3 $string .= $self->write_close;
771  
772 3 return $string;
773 }
774  
775 15 ▶sub as_perl {
Subroutine as_perl: called CC=1 SCAR=0.0
776 15 my $self = CORE::shift;
777  
778 15 my @array = map { $_->as_perl } $self->_elements;
779  
780 15 return \@array;
781 }
782  
783 # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # #
784 package Mac::PropertyList::dict;
785 24 ▶use base qw(Mac::PropertyList::Container);
Subroutine BEGIN: called CC=1 SCAR=0.0
786  
787 59 ▶sub new {
Subroutine new: called CC=1 SCAR=0.0
788 59 $_[0]->SUPER::new( $_[1] );
789 }
790  
791 1 ▶sub delete { delete ${ $_[0]->value }{$_[1]} }
Subroutine delete: called CC=1 SCAR=0.0
792 6 ▶sub exists { exists ${ $_[0]->value }{$_[1]} ? 1 : 0 }
Subroutine exists: called CC=2 SCAR=6.9
Branch: 1 branch
BranchTrueFalseTotal
exists ${$_[0]->value;}{$_[1]} ? : 4 2 6
793 5 ▶sub count { scalar CORE::keys %{ $_[0]->value } }
Subroutine count: called CC=1 SCAR=0.0
794  
795 137 ▶sub value {
Subroutine value: called CC=3 SCAR=11.1
796 137 my $self = shift;
797 137 my $key = shift;
798  
799 do
800 137 {
801 137 ▶ if( defined $key ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $key) { } 78 59 137
802 78 my $hash = $self->SUPER::value;
803  
804 78 ▶ if( exists $hash->{$key} ) { $hash->{$key}->value }
Branch: 1 branch
BranchTrueFalseTotal
if (exists $hash->{$key}) { } 78 0 78
805 0 else { return }
806 }
807 59 else { $self->SUPER::value }
808 };
809  
810 }
811  
812 18 ▶sub keys { my @k = CORE::keys %{ $_[0]->value }; wantarray ? @k : \@k; }
Subroutine keys: called CC=2 SCAR=7.1
Branch: 1 branch
BranchTrueFalseTotal
wantarray ? : 18 0 18
813 2 ▶sub values {
Subroutine values: called CC=2 SCAR=7.0
814 2 my @v = map { $_->value } CORE::values %{ $_[0]->value };
815 2 ▶ wantarray ? @v : \@v;
Branch: 1 branch
BranchTrueFalseTotal
wantarray ? : 2 0 2
816 }
817  
818 3 ▶sub as_basic_data {
Subroutine as_basic_data: called CC=2 SCAR=7.0
819 3 my $self = shift;
820  
821 my %dict = map {
822 3 my ($k, $v) = ($_, $self->{$_});
823 9 ▶ $k => eval { $v->can('as_basic_data') } ? $v->as_basic_data : $v
Branch: 1 branch
BranchTrueFalseTotal
eval { do { $v->can('as_basic_data') } } ? : 9 0 9
824 } CORE::keys %$self;
825  
826 3 return \%dict;
827 }
828  
829 12 ▶sub write_key {
Subroutine write_key: called CC=1 SCAR=0.0
830 12 '<key>' . HTML::Entities::encode_entities($_[1]) . '</key>'
831 }
832  
833 6 ▶sub write {
Subroutine write: called CC=2 SCAR=6.9
834 6 my $self = shift;
835  
836 6 my $string = $self->write_open . "\n";
837  
838 6 foreach my $key ( sort { $a cmp $b } $self->keys ) {
839 12 my $element = $self->{$key};
840  
841 12 my $bit = __PACKAGE__->write_key( $key ) . "\n";
842 12 $bit .= $element->write . "\n";
843  
844 12 $bit =~ s/^/\t/gm;
845  
846 12 $string .= $bit;
847 }
848  
849 6 $string .= $self->write_close;
850  
851 6 return $string;
852 }
853  
854 8 ▶sub as_perl {
Subroutine as_perl: called CC=2 SCAR=6.9
855 8 my $self = CORE::shift;
856  
857 my %dict = map {
858 8 my $v = $self->value($_);
859 69 ▶ $v = $v->as_perl if eval { $v->can( 'as_perl' ) };
Branch: 1 branch
BranchTrueFalseTotal
if eval { do { $v->can("as_perl") } } 20 49 69
860 69 $_, $v
861 } $self->keys;
862  
863 8 return \%dict;
864 }
865  
866  
867 # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # #
868 package Mac::PropertyList::Scalar;
869 24 ▶use base qw(Mac::PropertyList::Item);
Subroutine BEGIN: called CC=1 SCAR=0.0
870  
871 720 ▶sub new { my $copy = $_[1]; $_[0]->SUPER::new( \$copy ) }
Subroutine new: called CC=1 SCAR=0.0
872  
873 16 ▶sub as_basic_data { $_[0]->value }
Subroutine as_basic_data: called CC=1 SCAR=0.0
874  
875 23 ▶sub write { $_[0]->write_open . HTML::Entities::encode_entities($_[0]->value) . $_[0]->write_close }
Subroutine write: called CC=1 SCAR=0.0
876  
877 20 ▶sub as_perl { $_[0]->value }
Subroutine as_perl: called CC=1 SCAR=0.0
878  
879 # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # #
880 package Mac::PropertyList::date;
881 24 ▶use base qw(Mac::PropertyList::Scalar);
Subroutine BEGIN: called CC=1 SCAR=0.0
882  
883 # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # #
884 package Mac::PropertyList::real;
885 24 ▶use base qw(Mac::PropertyList::Scalar);
Subroutine BEGIN: called CC=1 SCAR=0.0
886  
887 # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # #
888 package Mac::PropertyList::integer;
889 24 ▶use base qw(Mac::PropertyList::Scalar);
Subroutine BEGIN: called CC=1 SCAR=0.0
890  
891 # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # #
892 package Mac::PropertyList::uid;
893 24 ▶use base qw(Mac::PropertyList::Scalar);
Subroutine BEGIN: called CC=1 SCAR=0.0
894  
895 # The following is conservative, since the actual largest unsigned
896 # integer is ~0, which is 0xFFFFFFFFFFFFFFFF on many (most?) modern
897 # Perls; but it is consistent with Mac::PropertyList::ReadBinary.
898 # This is really just future-proofing though, since it appears from
899 # CFBinaryPList.c that a UID is carried as a hard-coded uint32_t.
900 24 ▶use constant LONGEST_HEX_REPRESENTABLE_AS_NATIVE => 8; # 4 bytes
Subroutine BEGIN: called CC=1 SCAR=0.0
901  
902 # Instantiate with hex string. The string will be padded on the left
903 # with zero if its length is odd. It is this string which will be
904 # returned by value(). Presence of a non-hex character causes an
905 # exception. We default the argument to '00'.
906 10 ▶sub new {
Subroutine new: called CC=4 SCAR=13.9
907 10 my ( $class, $value ) = @_;
908 10 ▶ $value = '00' unless defined $value;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $value 1 9 10
909 10 ▶ Carp::croak( 'uid->new() argument must be hexadecimal' )
Branch: 1 branch
BranchTrueFalseTotal
if $value =~ / [[:^xdigit:]] /msx 0 10 10
910 if $value =~ m/ [[:^xdigit:]] /smx;
911 10 ▶ substr $value, 0, 0, '0'
Branch: 1 branch
BranchTrueFalseTotal
if length($value) % 2 1 9 10
912 if length( $value ) % 2;
913 10 return $class->SUPER::new( $value );
914 }
915  
916 # Without argument, this is an accessor returning the value as an unsigned
917 # integer, either a native Perl value or a Math::BigInt as needed.
918 # With argument, this is a mutator setting the value to the hex
919 # representation of the argument, which must be an unsigned integer,
920 # either native Perl of Math::BigInt object. If called as static method
921 # instantiates a new object.
922 2 ▶sub integer {
Subroutine integer: called CC=7 SCAR=22.6
923 2 my ( $self, $integer ) = @_;
924 2 ▶ if ( @_ < 2 ) {
Branch: 1 branch
BranchTrueFalseTotal
if (@_ < 2) { } 1 1 2
925 1 my $value = $self->value();
926 1 ▶ return length( $value ) > LONGEST_HEX_REPRESENTABLE_AS_NATIVE ?
Branch: 1 branch
BranchTrueFalseTotal
length $value > 8 ? : 0 1 1
927 Math::BigInt->from_hex( $value ) :
928 hex $value;
929 }
930 else {
931 1 ▶ Carp::croak( 'uid->integer() argument must be unsigned' )
Branch: 1 branch
BranchTrueFalseTotal
if $integer < 0 0 1 1
932 if $integer < 0;
933 1 ▶ my $value = ref $integer ?
Branch: 1 branch
BranchTrueFalseTotal
ref $integer ? : 0 1 1
934 $integer->to_hex() :
935 sprintf '%x', $integer;
936 1 ▶ if ( ref $self ) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $self) { } 0 1 1
937 0 ▶ substr $value, 0, 0, '0'
Branch: 1 branch
BranchTrueFalseTotal
if length($value) % 2 0 0 0
938 if length( $value ) % 2;
939 0 ${ $self } = $value;
940 }
941 else {
942 1 $self = $self->new( $value );
943 }
944 1 return $self;
945 }
946 }
947  
948 # This is how plutil represents a UID in XML.
949 1 ▶sub write {
Subroutine write: called CC=1 SCAR=0.0
950 1 my $self = shift;
951 1 my $dict = Mac::PropertyList::dict->new( {
952 'CF$UID' => Mac::PropertyList::integer->new(
953 $self->integer ),
954 }
955 );
956 1 return $dict->write();
957 }
958  
959 # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # #
960 package Mac::PropertyList::string;
961 24 ▶use base qw(Mac::PropertyList::Scalar);
Subroutine BEGIN: called CC=1 SCAR=0.0
962  
963 # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # #
964 package Mac::PropertyList::ustring;
965 24 ▶use base qw(Mac::PropertyList::Scalar);
Subroutine BEGIN: called CC=1 SCAR=0.0
966  
967 # XXX need to do some fancy unicode checking here
968  
969 # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # #
970 package Mac::PropertyList::data;
971 24 ▶use base qw(Mac::PropertyList::Scalar);
Subroutine BEGIN: called CC=1 SCAR=0.0
972  
973 2 ▶sub write {
Subroutine write: called CC=1 SCAR=0.0
974 2 my $self = shift;
975  
976 2 my $type = $self->type;
977 2 my $value = $self->value;
978  
979 2 require MIME::Base64;
980  
981 2 my $string = MIME::Base64::encode_base64($value);
982  
983 2 $self->write_open . $string . $self->write_close;
984 }
985  
986 # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # #
987 package Mac::PropertyList::Boolean;
988 24 ▶use base qw(Mac::PropertyList::Item);
Subroutine BEGIN: called CC=1 SCAR=0.0
989  
990 20 ▶sub new {
Subroutine new: called CC=1 SCAR=0.0
991 20 my $class = shift;
992  
993 20 my( $type ) = $class =~ m/.*::(.*)/g;
994  
995 20 $class->either( $type );
996 }
997  
998 20 ▶sub either { my $copy = $_[1]; bless \$copy, $_[0] }
Subroutine either: called CC=1 SCAR=0.0
999  
1000 3 ▶sub write { $_[0]->write_empty }
Subroutine write: called CC=1 SCAR=0.0
1001  
1002 # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # #
1003 package Mac::PropertyList::true;
1004 24 ▶use base qw(Mac::PropertyList::Boolean);
Subroutine BEGIN: called CC=1 SCAR=0.0
1005  
1006 # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # #
1007 package Mac::PropertyList::false;
1008 24 ▶use base qw(Mac::PropertyList::Boolean);
Subroutine BEGIN: called CC=1 SCAR=0.0
1009  
1010  
1011 =head1 SOURCE AVAILABILITY
1012  
1013 This project is in Github:
1014  
1015 https://github.com/briandfoy/mac-propertylist.git
1016  
1017 =head1 CREDITS
1018  
1019 Thanks to Chris Nandor for general Mac kung fu and Chad Walker for
1020 help figuring out the recursion for nested structures.
1021  
1022 Mike Ciul provided some classes for the different input modes, and
1023 these allow us to optimize the parsing code for each of those.
1024  
1025 Ricardo Signes added the C<as_basic_types()> methods so you can dump
1026 all the plist junk and just play with the data.
1027  
1028 =head1 TO DO
1029  
1030 * change the value of an object
1031  
1032 * validate the values of objects (date, integer)
1033  
1034 * methods to add to containers (dict, array)
1035  
1036 * do this from a filehandle or a scalar reference instead of a scalar
1037 + generate closures to handle the work.
1038  
1039 =head1 AUTHOR
1040  
1041 brian d foy, C<< <briandfoy@pobox.com> >>
1042  
1043 Tom Wyant added support for UID types.
1044  
1045 =head1 COPYRIGHT AND LICENSE
1046  
1047 Copyright © 2004-2026, brian d foy <briandfoy@pobox.com>. All rights reserved.
1048  
1049 This program is free software; you can redistribute it and/or modify
1050 it under the terms of the Artistic License 2.0.
1051  
1052 =head1 SEE ALSO
1053  
1054 http://www.apple.com/DTDs/PropertyList-1.0.dtd
1055  
1056 =cut
1057  
1058 24 "See why 1984 won't be like 1984";
« PropertyList/WriteBinary.pm ↑ Summary PropertyList/ReadBinary.pm »