Plist.pm

filter: Statementstmt 93.4% 86 / 92 Branchbran 68.9% 40 / 58 Conditioncond 50.0% 9 / 18 MC/DCmcdc 22.2% 2 / 9 Subroutinesub 100% 14 / 14 Podpod 100% 8 / 8 totaltotal 79.9% 159 / 199 CC 39 SCAR 23.1
CC
39
Cov
80%
reify
16.9
is_archive
14.4
collapse
10.2
CRAP 10.1
↑ Summary Plist/BinaryReader.pm »
1 =head1 NAME
2  
3 Data::Plist - object representing a property list
4  
5 =head1 SYNOPSIS
6  
7 # Create a new plist containing $data
8 my $plist = Data::Plist->new($data);
9  
10 # Get nested arrays containing the perl data structure's
11 # information
12 my $ret = $plist->raw_data;
13  
14 # Get perl data structure
15 $ret = $plist->data;
16  
17 # Get an Objective C object
18 $ret = $plist->object;
19  
20  
21 =head1 DESCRIPTION
22  
23 Plists are intermediate structures from which nested array
24 structures of the format specified in L</SERIALIZED DATA>,
25 perl data structures and Objective C data structures can be
26 obtained.
27  
28 =cut
29  
30 =head1 SERIALIZED DATA
31  
32 Perl data structures that have been serialized become
33 nested array structures containing their data and their
34 data type. Example:
35  
36 [ array => [ string => "kitten" ], [ integer => 42], [ real => 3.14159 ] ]
37  
38 Array references are passed around when dealing with
39 serialized data.
40  
41 =head1 KEYED ARCHIVES
42  
43 Apple uses binary property lists as a serialization format from
44 Objective C, in a format C<NSKeyedArchiver>. L<Data::Plist> has the
45 capability to recognize property lists which were generated using
46 C<NSKeyedArchiver>, and to construct perl objects based on the
47 information in the property list. Objects thus created are blessed
48 under the C<Data::Plist::Foundation> namespace. Thus, the root
49 ancestor of all Objective C objects thus imported is
50 L<Data::Plist::Foundation::NSObject>.
51  
52 =cut
53  
54 package Data::Plist;
55  
56 4 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
57 4 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
58  
59 4 ▶use DateTime;
Subroutine BEGIN: called CC=1 SCAR=0.0
60 4 ▶use UNIVERSAL::require;
Subroutine BEGIN: called CC=1 SCAR=0.0
61  
62 4 ▶use vars qw/$VERSION/;
Subroutine BEGIN: called CC=1 SCAR=0.0
63 4 $VERSION = "0.100_001";
64  
65 =head1 METHODS
66  
67 =head2 new
68  
69 Creates a new Data::Plist object. Generally, you will not need to
70 call this directly, as Plists are generally created by
71 L<Data::Plist::Reader> classes, and are not needed in serialization
72 when using L<Data::Plist::Writer> classes.
73  
74 =cut
75  
76 37 ▶sub new {
Subroutine new: called CC=1 SCAR=0.0
Pod documented
77 37 my $class = shift;
78 37 return bless { data => undef, @_ } => $class;
79 }
80  
81 =head2 collapse $data
82  
83 Takes an array of serialized data C<$data>. Recursively
84 returns the actual data, without the datatype labels.
85  
86 =cut
87  
88 386 ▶sub collapse {
Subroutine collapse: called CC=10 SCAR=23.2
Pod documented
89 386 my $self = shift;
90 386 my ($data) = @_;
91  
92 386 ▶ unless ( ref $data eq "ARRAY" ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (ref $data eq "ARRAY") 0 386 386
93 0 warn "Got $data?";
94 0 return "???";
95 }
96  
97 386 ▶ if ( $data->[0] eq "array" ) {
Branch: 5 branches
BranchTrueFalseTotal
if ($data->[0] eq 'array') { } 9 377 386
elsif ($data->[0] eq 'dict') { } 13 364 377
elsif ($data->[0] eq 'string') { } 32 332 364
elsif ($data->[0] eq 'date') { } 1 331 332
elsif ($data->[0] eq 'UID' and ref $data->[1]) { } 18 313 331
98 9 return [ map $self->collapse($_), @{ $data->[1] } ];
99 } elsif ( $data->[0] eq "dict" ) {
100 13 my %dict = %{ $data->[1] };
101 13 $dict{$_} = $self->collapse( $dict{$_} ) for keys %dict;
102 13 return \%dict;
103 } elsif ( $data->[0] eq "string" ) {
104 32 ▶ return $data->[1] eq '$null' ? undef : $data->[1];
Branch: 1 branch
BranchTrueFalseTotal
$data->[1] eq '$null' ? : 3 29 32
105 } elsif ( $data->[0] eq "date" ) {
106 1 return DateTime->from_epoch( epoch => $data->[1] + 978307200 );
107 ▶ } elsif ( $data->[0] eq "UID" and ref $data->[1] ) {
Condition66%2/3
$data->[0] eq "UID" and ref $data->[1]
!ll&&!rl&&r
313018
MC/DC50%1/2
$data->[0] eq "UID" and ref $data->[1]
$data->[0] eq "UID" ref $data->[1]
Truth table66%2/3
A and B
A= $data->[0] eq "UID"B= ref $data->[1]
ABresult
0X0
100
111
108 18 return $self->collapse( $data->[1] );
109 } else {
110 313 return $data->[1];
111 }
112  
113 }
114  
115 =head2 raw_data
116  
117 Returns the plist as a set of nested arrays of the format specified in
118 L</SERIALIZED DATA>.
119  
120 =cut
121  
122 57 ▶sub raw_data {
Subroutine raw_data: called CC=1 SCAR=0.0
Pod documented
123 57 my $self = shift;
124 57 return $self->{data};
125 }
126  
127 =head2 data
128  
129 Returns the plist as its corresponding perl data structure.
130  
131 =cut
132  
133 16 ▶sub data {
Subroutine data: called CC=1 SCAR=0.0
Pod documented
134 16 my $self = shift;
135 16 return $self->collapse( $self->raw_data );
136 }
137  
138 =head2 is_archive
139  
140 Checks if the plist is actually an archived Objective C generated by
141 C<NSKeyedArchiver>. Returns true if it is. See L</KEYED ARCHIVES>.
142  
143 =cut
144  
145 3 ▶sub is_archive {
Subroutine is_archive: called CC=11 SCAR=26.6
Pod documented
146 3 my $self = shift;
147 3 my $data = $self->raw_data;
148 3 ▶ return unless $data->[0] eq "dict";
Branch: 1 branch
BranchTrueFalseTotal
unless $data->[0] eq "dict" 0 3 3
149  
150 3 ▶ return unless exists $data->[1]{'$archiver'};
Branch: 1 branch
BranchTrueFalseTotal
unless exists $data->[1]{"\$archiver"} 0 3 3
151 3 ▶ return unless $data->[1]{'$archiver'}[0] eq "string";
Branch: 1 branch
BranchTrueFalseTotal
unless $data->[1]{"\$archiver"}[0] eq "string" 0 3 3
152 3 ▶ return unless $data->[1]{'$archiver'}[1] eq "NSKeyedArchiver";
Branch: 1 branch
BranchTrueFalseTotal
unless $data->[1]{"\$archiver"}[1] eq "NSKeyedArchiver" 0 3 3
153  
154 3 ▶ return unless exists $data->[1]{'$objects'};
Branch: 1 branch
BranchTrueFalseTotal
unless exists $data->[1]{"\$objects"} 0 3 3
155 3 ▶ return unless $data->[1]{'$objects'}[0] eq "array";
Branch: 1 branch
BranchTrueFalseTotal
unless $data->[1]{"\$objects"}[0] eq "array" 0 3 3
156  
157 3 ▶ return unless exists $data->[1]{'$top'};
Branch: 1 branch
BranchTrueFalseTotal
unless exists $data->[1]{"\$top"} 0 3 3
158  
159 3 ▶ return unless exists $data->[1]{'$version'};
Branch: 1 branch
BranchTrueFalseTotal
unless exists $data->[1]{"\$version"} 0 3 3
160 3 ▶ return unless $data->[1]{'$version'}[0] eq "integer";
Branch: 1 branch
BranchTrueFalseTotal
unless $data->[1]{"\$version"}[0] eq "integer" 0 3 3
161 3 ▶ return unless $data->[1]{'$version'}[1] eq "100000";
Branch: 1 branch
BranchTrueFalseTotal
unless $data->[1]{"\$version"}[1] eq 100000 0 3 3
162  
163 3 return 1;
164 }
165  
166 =head2 unref
167  
168 Recursively strips references from the plist.
169  
170 =cut
171  
172 64 ▶sub unref {
Subroutine unref: called CC=8 SCAR=20.8
Pod documented
173 64 my $self = shift;
174 64 my $p = shift;
175 64 ▶ if ( $p->[0] eq "UID" ) {
Branch: 4 branches
BranchTrueFalseTotal
if ($p->[0] eq 'UID') { } 18 46 64
elsif ($p->[0] eq 'array') { } 5 41 46
elsif ($p->[0] eq 'dict') { } 10 31 41
elsif ($p->[0] eq 'data' and ref $p->[1] and $p->[1]->isa('Data::Plist')) { } 1 30 31
176 return [
177 "UID",
178 18 $self->unref( $self->raw_data->[1]{'$objects'}[1][ $p->[1] ] )
179 ];
180 } elsif ( $p->[0] eq "array" ) {
181 5 return [ "array", [ map { $self->unref($_) } @{ $p->[1] } ] ];
182 } elsif ( $p->[0] eq "dict" ) {
183 10 my %dict = %{ $p->[1] };
184 10 $dict{$_} = $self->unref( $dict{$_} ) for keys %dict;
185 10 return [ "dict", \%dict ];
186 ▶ } elsif ( $p->[0] eq "data"
Condition66%4/6
$p->[0] eq "data" and ref $p->[1] and $p->[1]->isa("Data::Plist")
!ll&&!rl&&r
3001
$p->[0] eq "data" and ref $p->[1]
!ll&&!rl&&r
3001
MC/DC33%1/3
$p->[0] eq "data" and ref $p->[1] and $p->[1]->isa("Data::Plist")
$p->[0] eq "data" ref $p->[1] $p->[1]->isa("Data::Plist")
Truth table50%2/4
A and B and C
A= $p->[0] eq "data"B= ref $p->[1]C= $p->[1]->isa("Data::Plist")
ABCresult
0XX0
10X0
1100
1111
187 and ref $p->[1]
188 and $p->[1]->isa("Data::Plist") )
189 {
190 1 return $p->[1]->_raw_object;
191 } else {
192 30 return $p;
193 }
194 }
195  
196 =head2 reify $data
197  
198 Takes serialized data (see L</SERIALIZED DATA>) C<$data>
199 and checks if it's a keyed archive (see L</SERIALIZED
200 DATA>). If the data is a keyed archive, it blesses it into
201 the appropriate perl class.
202  
203 =cut
204  
205 18 ▶sub reify {
Subroutine reify: called CC=11 SCAR=28.3
Pod documented
206 18 my $self = shift;
207 18 my ( $data ) = @_;
208  
209 18 ▶ return $data unless ref $data;
Branch: 1 branch
BranchTrueFalseTotal
unless ref $data 13 5 18
210 5 ▶ if ( ref $data eq "HASH" ) {
Branch: 2 branches
BranchTrueFalseTotal
if (ref $data eq 'HASH') { } 5 0 5
elsif (ref $data eq 'ARRAY') { } 0 0 0
211 5 my $hash = { %{$data} };
212 5 my $class = delete $hash->{'$class'};
213 5 $hash->{$_} = $self->reify( $hash->{$_} ) for keys %{$hash};
214 5 ▶ if ( $class
Branch: 1 branch
BranchTrueFalseTotal
if ($class and ref $class and ref $class eq "HASH" and $class->{"\$classname"}) 5 0 5
Condition33%3/9
$class and ref $class and ref $class eq "HASH" and $class->{"\$classname"}
!ll&&!rl&&r
005
$class and ref $class and ref $class eq "HASH"
!ll&&!rl&&r
005
$class and ref $class
!ll&&!rl&&r
005
MC/DC0%0/4
$class and ref $class and ref $class eq "HASH" and $class->{"\$classname"}
$class ref $class ref $class eq "HASH" $class->{"\$classname"}
Truth table20%1/5
A and B and C and D
A= $classB= ref $classC= ref $class eq "HASH"D= $class->{"\$classname"}
ABCDresult
0XXX0
10XX0
110X0
11100
11111
215 and ref $class
216 and ref $class eq "HASH"
217 and $class->{'$classname'} )
218 {
219 5 my $classname = "Data::Plist::Foundation::" . $class->{'$classname'};
220 5 ▶ if ( not $classname->require ) {
Branch: 2 branches
BranchTrueFalseTotal
if (not $classname->require) { } 1 4 5
elsif (not $classname->isa('Data::Plist::Foundation::NSObject')) { } 0 4 4
221 1 warn "Can't require $classname: $@\n";
222 } elsif ( not $classname->isa( "Data::Plist::Foundation::NSObject" ) ) {
223 0 warn "$classname isn't a Data::Plist::Foundation::NSObject\n";
224 } else {
225 4 bless( $hash, $classname );
226 4 $hash = $hash->replacement;
227 }
228 }
229 5 return $hash;
230 } elsif ( ref $data eq "ARRAY" ) {
231 0 return [ map $self->reify( $_ ), @{$data} ];
232 } else {
233 0 return $data;
234 }
235 }
236  
237 2 ▶sub _raw_object {
Subroutine _raw_object: called CC=2 SCAR=7.1
238  
239 2 my $self = shift;
240 2 ▶ return unless $self->is_archive;
Branch: 1 branch
BranchTrueFalseTotal
unless $self->is_archive 0 2 2
241 2 return $self->unref( $self->raw_data->[1]{'$top'}[1]{root} );
242 }
243  
244 =head2 object
245  
246 If the plist is an Objective C object archive created with
247 C<NSKeyedArchiver> (see L</KEYED ARCHIVES>), returns the object
248 blessed into the corresponding class under
249 L<Data::Plist::Foundation::NSOjbect>. Otherwise, returns undef.
250  
251 =cut
252  
253 1 ▶sub object {
Subroutine object: called CC=2 SCAR=7.0
Pod documented
254 1 my $self = shift;
255  
256 1 require Data::Plist::Foundation::NSObject;
257  
258 1 ▶ return unless $self->is_archive;
Branch: 1 branch
BranchTrueFalseTotal
unless $self->is_archive 0 1 1
259 1 return $self->reify( $self->collapse( $self->_raw_object ) );
260 }
261  
262 =head1 DEPENDENCIES
263  
264 L<Class::ISA>, L<DateTime>, L<Digest::MD5>, L<Math::BigInt>,
265 L<MIME::Base64>, L<Scalar::Util>, L<Storable>, L<UNIVERSAL::isa>,
266 L<XML::Writer>
267  
268 =head1 BUGS AND LIMITATIONS
269  
270 UIDs don't seem to be sufficiently well supported by the XML
271 readers and writers.
272  
273 Please report any bugs or feature requests to
274 C<bug-Data-Plist@rt.cpan.org>, or through the web interface at
275 L<http://rt.cpan.org>.
276  
277 =head1 AUTHORS
278  
279 Alex Vandiver and Jacky Chang.
280  
281 XML reader support added by Lubomir Rintel <lkundrak@v3.sk>
282  
283 Based on plutil.pl, written by Pete Wilson <wilsonpm@gamewood.net>
284  
285 =head1 LICENSE
286  
287 This module is free software; you can redistribute it and/or modify it
288 under the same terms as Perl itself. See L<perlartistic>.
289  
290 =cut
291  
292 4 1;
↑ Summary Plist/BinaryReader.pm »