blib/lib/Mac/PropertyList/XS.pm

filter: Statementstmt 91.3% 21 / 23 Branchbran n/a Conditioncond n/a MC/DCmcdc n/a Subroutinesub 100% 8 / 8 Podpod n/a totaltotal 93.5% 29 / 31 CC 1 SCAR 0.4
CC
1
Cov
91%
BEGIN
1.3
BEGIN
1.0
BEGIN
1.0
CRAP 1.0
↑ Summary XS.xs »
1 =head1 NAME
2  
3 Mac::PropertyList::XS - work with Mac plists at a low level, really fast
4  
5 =cut
6  
7 package Mac::PropertyList::XS;
8  
9 =head1 SYNOPSIS
10  
11 See L<Mac::PropertyList> and L<Mac::PropertyList::SAX>
12  
13 =head1 DESCRIPTION
14  
15 L<Mac::PropertyList::SAX> was my first attempt to speed up property-list
16 parsing. It achieves about a 30x speed boost, but large files still take
17 too long for my taste. This module addresses some remaining speed gains
18 by implementing some expensive operations in C.
19  
20 This module is intended to be a drop-in replacement for
21 L<Mac::PropertyList::SAX>, which is itself a drop-in replacement for
22 L<Mac::PropertyList>.
23  
24 =cut
25  
26 18 ▶use 5.008008;
Subroutine BEGIN: called CC=1 SCAR=0.0
27 18 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
28 18 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
29 18 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
30  
31 require Exporter;
32 18 ▶use AutoLoader;
Subroutine BEGIN: called CC=1 SCAR=0.0
33  
34 # Passthrough function
35 18 ▶use Mac::PropertyList qw(plist_as_string);
Subroutine BEGIN: called CC=1 SCAR=0.0
36 18 ▶use Mac::PropertyList::SAX qw(create_from_ref create_from_hash create_from_array);
Subroutine BEGIN: called CC=1 SCAR=0.0
37 0 ▶use XML::Parser;
Subroutine BEGIN: called CC=1 SCAR=2.6
38  
39 use base qw(Exporter);
40  
41 our @EXPORT_OK = qw(
42 parse_plist
43 parse_plist_fh
44 parse_plist_file
45 parse_plist_string
46 plist_as_string
47 create_from_ref
48 create_from_hash
49 create_from_array
50 create_from_string
51 );
52  
53 our %EXPORT_TAGS = (
54 all => \@EXPORT_OK,
55 create => [ qw(create_from_ref create_from_hash create_from_array create_from_string plist_as_string) ],
56 parse => [ qw(parse_plist parse_plist_fh parse_plist_file parse_plist_string) ],
57 );
58  
59 our $VERSION = '0.06';
60 our $XS_VERSION = $VERSION;
61 $VERSION = eval $VERSION; # see L<perlmodstyle>
62  
63 sub AUTOLOAD {
64 # This AUTOLOAD is used to 'autoload' constants from the constant()
65 # XS function.
66  
67 my $constname;
68 our $AUTOLOAD;
69 ($constname = $AUTOLOAD) =~ s/.*:://;
70 croak "&Mac::PropertyList::XS::constant not defined" if $constname eq 'constant';
71 my ($error, $val) = constant($constname);
72 if ($error) { croak $error; }
73 {
74 no strict 'refs';
75 # Fixed between 5.005_53 and 5.005_61
76 #XXX if ($] >= 5.00561) {
77 #XXX *$AUTOLOAD = sub () { $val };
78 #XXX }
79 #XXX else {
80 *$AUTOLOAD = sub { $val };
81 #XXX }
82 }
83 goto &$AUTOLOAD;
84 }
85  
86 require XSLoader;
87 XSLoader::load('Mac::PropertyList::XS', $XS_VERSION);
88  
89 =head1 EXPORTS
90  
91 By default, no functions are exported. Specify individual functions to export
92 as usual, or use the tags ':all', ':create', and ':parse' for the appropriate
93 sets of functions (':create' includes the create* functions as well as
94 plist_as_string; ':parse' includes the parse* functions).
95  
96 =head1 FUNCTIONS
97  
98 =over 4
99  
100 =item parse_plist_file
101  
102 See L<Mac::PropertyList/parse_plist_file>
103  
104 =cut
105  
106 sub parse_plist_file
107 {
108 my $file = shift;
109  
110 if (ref $file) {
111 _parse(parse_file => $file);
112 } else {
113 carp("parse_plist_file: file [$file] does not exist!"), return unless -e $file;
114 open my $fh, "<", $file;
115 _parse(parse_file => $fh);
116 }
117 }
118  
119 sub _parse {
120 # shift off first param in case we use `goto` later (leaving @_ with $data)
121 my $sub = shift;
122 my ($data) = @_;
123  
124 my $first;
125 my $fh;
126 my $delegate;
127  
128 # read initial bytes of file
129 # if we have a binary plist, delegate to Mac::PropertyList
130 if ($sub eq "parse_uri") {
131 open $fh, "<", $_[0];
132 $sub = "parse_file";
133 $_[0] = $fh;
134 # delegate will be set below
135 }
136  
137 if ($sub eq "parse_file") {
138 read $_[0], $first, length "bplist";
139 seek $_[0], 0, 0 or die "Can't seek given filehandle"; # seek back to beginning
140 $delegate = \&Mac::PropertyList::parse_plist_fh;
141 } elsif ($sub eq "parse_string") {
142 $first = $_[0];
143 $delegate = \&Mac::PropertyList::parse_plist;
144 }
145  
146 if ($first =~ /^bplist/) {
147 # binary plist -- delegate to non-SAX module
148 goto $delegate;
149 } else {
150 my $p = new XML::Parser(Handlers => { Init => \&handle_init,
151 Start => \&handle_start,
152 End => \&handle_end,
153 Char => \&handle_char,
154 Final => \&handle_final });
155 return $p->parse($data);
156 }
157 }
158  
159 =item parse_plist_fh
160  
161 See L<Mac::PropertyList/parse_plist_fh>
162  
163 =cut
164  
165 sub parse_plist_fh { _parse(parse_file => @_) }
166  
167 =item parse_plist
168  
169 See L<Mac::PropertyList/parse_plist>
170  
171 =cut
172  
173 sub parse_plist { _parse(parse_string => @_) }
174  
175 =item parse_plist_string
176  
177 An alias to parse_plist, provided for better regularity compared to Perl SAX.
178  
179 =cut
180  
181 *parse_plist_string = \&parse_plist;
182  
183 =item create_from_ref( HASH_REF | ARRAY_REF )
184  
185 Create a plist from an array or hash reference.
186  
187 The values of the hash can be simple scalars or references. Hash and array
188 references are handled recursively, and L<Mac::PropertyList> objects are output
189 correctly. All other scalars are treated as strings (use L<Mac::PropertyList>
190 objects to represent other types of scalars).
191  
192 Returns a string representing the reference in serialized plist format.
193  
194 =item create_from_hash( HASH_REF )
195  
196 Provided for backward compatibility with L<Mac::PropertyList>: aliases
197 create_from_ref.
198  
199 =item create_from_array( ARRAY_REF )
200  
201 Provided for backward compatibility with L<Mac::PropertyList>: aliases
202 create_from_ref.
203  
204 =item create_from_string( STRING )
205  
206 Provided for backward compatibility with L<Mac::PropertyList>: aliases C<Mac::PropertyList::create_from_string()>.
207  
208 =cut
209  
210 *create_from_string = \&Mac::PropertyList::create_from_string;
211  
212 =back
213  
214 =head1 BUGS / CAVEATS
215  
216 Certainly !
217  
218 =head1 SUPPORT
219  
220 Please contact the author with bug reports or feature requests.
221  
222 =head1 AUTHOR
223  
224 Darren M. Kulp, C<< <darren@kulp.ch> >>
225  
226 =head1 THANKS
227  
228 brian d foy, who created the L<Mac::PropertyList> module whose tests were
229 appropriated for this module.
230  
231 =head1 SEE ALSO
232  
233 L<Mac::PropertyList>, the inspiration for this module.
234  
235 =head1 COPYRIGHT AND LICENSE
236  
237 Copyright (C) 2009-2017 by Darren Kulp
238  
239 This library is free software; you can redistribute it and/or modify
240 it under the same terms as Perl itself, either Perl version 5.8.4 or,
241 at your option, any later version of Perl 5 you may have available.
242  
243 =cut
244  
245 1;
246 __END__
↑ Summary XS.xs »