lib/PXML/Preserialize.pm

filter: Statementstmt 100% 90 / 90 Branchbran 59.0% 13 / 22 Conditioncond 100% 2 / 2 MC/DCmcdc 100% 1 / 1 Subroutinesub 100% 22 / 22 Podpod 0.0% 0 / 3 totaltotal 91.4% 128 / 140 CC 13 SCAR 8.0
CC
13
Cov
92%
_pxmlpre
5.1
__ANON__
4.0
pxmlpre
2.0
CRAP 2.2
« testmem.pl ↑ Summary lib/FP/url_.pm »
1 #
2 # Copyright (c) 2015-2020 Christian Jaeger, copying@christianjaeger.ch
3 #
4 # This is free software, offered under either the same terms as perl 5
5 # or the terms of the Artistic License version 2 or the terms of the
6 # MIT License (Expat version). See the file COPYING.md that came
7 # bundled with this file.
8 #
9  
10 =head1 NAME
11  
12 PXML::Preserialize - faster PXML templating through preserialization
13  
14 =head1 SYNOPSIS
15  
16 1 ▶ use PXML::Preserialize qw(pxmlfunc pxmlpre);
Subroutine BEGIN: called CC=1 SCAR=0.0
17 use PXML::XHTML qw(A B);
18  
19 my $link_normal = sub {
20 my ($href,$body)=@_;
21 A {href=> $href}, $body
22 };
23  
24 my $link_fast = pxmlfunc {
25 my ($href,$body)=@_; # can take up to 10[?] arguments.
26 A {href=> $href}, $body
27 };
28  
29 # the `2` is the number of arguments
30 my $link_fast2 = pxmlpre 2, $link_normal;
31  
32 # these expressions are all returning the same result, but the second
33 # and third are (supposedly) evaluated faster than the first:
34 is $link_normal->("http://foo", [B("Foo"), "Bar"])->string,
35 '<a href="http://foo"><b>Foo</b>Bar</a>';
36 is $link_fast->("http://foo", [B("Foo"), "Bar"])->string,
37 '<a href="http://foo"><b>Foo</b>Bar</a>';
38 is $link_fast2->("http://foo", [B("Foo"), "Bar"])->string,
39 '<a href="http://foo"><b>Foo</b>Bar</a>';
40  
41  
42 =head1 DESCRIPTION
43  
44 =for test ignore
45  
46 PXML represents every XML/HTML element as an individual Perl object,
47 and both building up a PXML tree and serializing it is somewhat
48 costly.
49  
50 And even if only a few strings change in a (sub)tree, a new tree
51 instance needs to be created and serialized for every set of those
52 strings.
53  
54 This overhead can be eliminated by pre-serializing the segments of the
55 tree that don't change.
56  
57 This module offers C<pxmlpre>, a function that takes a user supplied
58 function which maps some number of arguments to a PXML tree with those
59 arguments inserted, and returns a function that maps those same
60 arguments to an array with preserialized fragments and the (escaped)
61 argument values so that the PXML serialization functions don't have
62 any work to do except for printing the fragments and values.
63  
64 With the example from the synopsis:
65  
66 &$link_normal("foo","bar")
67  
68 returns a PXML element with name "a", a hash C<{href=> "foo"}> as
69 attributes, and "bar" as the body. C<<->string>> walks over the element
70 and hash and body and turns all parts into the proper XML syntax.
71  
72 &$link_fast2("foo","bar")
73  
74 returns
75  
76 bless [ $fragment1, "foo", $fragment2, "$bar", $fragment3 ], "PXML::Body"
77  
78 where $fragment1 is the string '<a href="' blessed to
79 C<PXML::Preserialize::Serialized>, which has a
80 C<pxml_serialized_body_string> method that returns the unmodified
81 string, which the serializer finds and calls (this is the currently
82 implemented way to add fragments to a PXML data structure and have the
83 serializer output them unescaped).
84  
85 The fragments are built by running the user-supplied function with
86 trial arguments and then serializing the resulting tree, breaking up
87 the serialized string where the arguments are found, then returning a
88 closure that returns an array with both the constant pieces and new
89 arguments.
90  
91 =head1 RESTRICTIONS
92  
93 The user-supplied functions need to heed the following restrictions:
94  
95 =over 4
96  
97 =item * be pure
98  
99 =item * must not contain branching (if/else etc.)
100  
101 The arguments are not allowed to influence the structure of the
102 returned PXML tree, only the contents at the ever same spots.
103  
104 =item * must insert the arguments into the result unmodified
105  
106 The arguments, if they are used at all, must be inserted into the tree
107 unmodified. Even appending or prepending strings is not allowed (use a
108 wrapper array to put values before/after instead). To guard against
109 such errors, the argument values that are passed during the
110 preserialization step throw an error if they are stringified etc.
111  
112 =back
113  
114 Also, the values returned by C<pxmlpre>'d functions can not be
115 processed with e.g. functions in L<PXML::Util>, or at least those won't
116 find the elements in the parts that have been pre-serialized.
117  
118 =head1 SEE ALSO
119  
120 L<PXML>
121  
122 =head1 NOTE
123  
124 This is alpha software! Read the status section in the package README
125 or on the L<website|http://functional-perl.org/>.
126  
127 =cut
128  
129 package PXML::Preserialize;
130 3 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
131 3 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
132 3 ▶use warnings FATAL => 'uninitialized';
Subroutine BEGIN: called CC=1 SCAR=0.0
133 3 ▶use Exporter "import";
Subroutine BEGIN: called CC=1 SCAR=0.0
134  
135 3 our @EXPORT = qw(pxmlpre pxmlfunc);
136 3 our @EXPORT_OK = qw();
137 3 our %EXPORT_TAGS = (all => [@EXPORT, @EXPORT_OK]);
138  
139 3 ▶use FP::Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
140  
141 {
142  
143 package PXML::Preserialize::Serialized;
144  
145 24 ▶ sub new {
Subroutine new: called CC=1 SCAR=0.0
146 24 my ($class, $str) = @_;
147 24 bless \($str), $class
148 }
149  
150 19 ▶ sub pxml_serialized_body_string {
Subroutine pxml_serialized_body_string: called CC=1 SCAR=0.0
151 20 my $s = shift;
152 19 $$s
153 }
154 }
155  
156 {
157  
158 3 package PXML::Preserialize::Argument;
159 3 ▶ use FP::Struct ["effecter", "n"], 'FP::Struct::Show', 'FP::Abstract::Pure';
Subroutine BEGIN: called CC=1 SCAR=0.0
160  
161 # Prevent erroneous usage:
162 3 ▶ use overload (
Subroutine BEGIN: called CC=1 SCAR=0.0
163 3 '""' => 'err',
164 '0+' => 'err',
165  
166 #'+' => 'err',
167 fallback => 1 # necessary not to have to provide + etc.
168 3 );
169  
170 3 ▶ sub err {
Subroutine err: called CC=1 SCAR=0.0
171 3 die "tried to access a " . __PACKAGE__ . " object"
172 }
173  
174 # Called when used correctly:
175 6 ▶ sub pxml_serialized_body_string {
Subroutine pxml_serialized_body_string: called CC=2 SCAR=7.0
176 6 my $self = shift;
177 6 my ($fh) = @_;
178 6 ▶ flush $fh or die $!;
Branch: 1 branch
BranchTrueFalseTotal
$fh->flush or die $! 6 0 6
179 6 $self->effecter->(0, $self->n);
180 6 ""
181 }
182  
183 5 ▶ sub pxml_serialized_attribute_string {
Subroutine pxml_serialized_attribute_string: called CC=2 SCAR=7.0
184 5 my $self = shift;
185 5 my ($fh) = @_;
186 5 ▶ flush $fh or die $!;
Branch: 1 branch
BranchTrueFalseTotal
$fh->flush or die $! 5 0 5
187 5 $self->effecter->(1, $self->n);
188 5 ""
189 }
190 3 _END_
191 }
192  
193 3 ▶use PXML::Serialize qw(pxml_print_fragment_fast attribute_escape);
Subroutine BEGIN: called CC=1 SCAR=0.0
194 3 ▶use PXML qw(pxmlbody pxmlflush);
Subroutine BEGIN: called CC=1 SCAR=0.0
195 3 ▶use FP::Div qw(max);
Subroutine BEGIN: called CC=1 SCAR=0.0
196  
197 # passes $fn $nargs arguments that it will use during serialization to
198 # cut apart the serialized representation.
199 11 ▶sub _pxmlpre {
Subroutine _pxmlpre: called CC=5 SCAR=16.4
200 11 ▶ @_ == 2 or fp_croak_arity 2;
Branch: 1 branch
BranchTrueFalseTotal
@_ == 2 or fp_croak_arity(2) 11 0 11
201 11 my ($nargs, $fn) = @_;
202  
203 11 my @items;
204 11 my $buf = "";
205 11 my $lasti = 0;
206  
207 11 ▶ my $effecter = sub {
Subroutine __ANON__: called CC=2 SCAR=7.0
208 11 my ($is_attribute, $n) = @_;
209  
210 # let $buf grow unimpeded (setting it to "" here seems to mess
211 # up perl: string is regrown to previous size, but shows
212 # what's probably uninitialized memory, heh!)
213 11 ▶ push @items, PXML::Preserialize::Serialized->new(substr $buf, $lasti)
Branch: 1 branch
BranchTrueFalseTotal
if $lasti < length $buf 11 0 11
214 if $lasti < length $buf;
215 11 push @items, [$is_attribute, $n];
216 11 $lasti = length $buf;
217 11 };
218  
219 11 my @args = map { PXML::Preserialize::Argument->new($effecter, $_) }
220 0 .. $nargs - 1;
221  
222 11 my $res = &$fn(@args);
223  
224 8 ▶ open my $out, ">", \$buf or die $!;
Branch: 1 branch
BranchTrueFalseTotal
open my $out, ">", \$buf or die $! 8 0 8
225  
226 8 pxml_print_fragment_fast($res, $out);
227  
228 8 ▶ close $out or die $!;
Branch: 1 branch
BranchTrueFalseTotal
close $out or die $! 8 0 8
229 8 ▶ push @items, PXML::Preserialize::Serialized->new(substr $buf, $lasti)
Branch: 1 branch
BranchTrueFalseTotal
if $lasti < length $buf 8 0 8
230 if $lasti < length $buf;
231  
232 \@items
233 8 }
234  
235 8 ▶sub build {
Subroutine build: called CC=1 SCAR=0.0
Pod undocumented
236 8 my ($nargs, $items) = @_;
237  
238 # return interpreter(?), not compilate (to avoid eval (overhead?))
239 7 ▶ sub {
Subroutine __ANON__: called CC=4 SCAR=13.9
240 7 ▶ @_ == $nargs or die "expecting $nargs argument(s), got " . @_;
Branch: 1 branch
BranchTrueFalseTotal
@_ == $nargs or die "expecting $nargs argument(s), got " . @_ 7 0 7
241 pxmlbody(
242 map {
243 7 ref($_) eq "ARRAY"
244 23 ▶ ? do {
Branch: 1 branch
BranchTrueFalseTotal
ref $_ eq 'ARRAY' ? : 8 15 23
245 8 my ($is_attribute, $i) = @$_;
246 8 ▶ $is_attribute
Branch: 1 branch
BranchTrueFalseTotal
$is_attribute ? : 4 4 8
247 ? PXML::Preserialize::Serialized->new(
248 attribute_escape($_[$i]))
249  
250 # otherwise let the default escaper in the
251 # serializer do it (this *should* always be in
252 # body context, XXX danger?)
253 : $_[$i];
254 }
255 : $_;
256 } @$items
257 )
258 }
259 8 }
260  
261 3 ▶sub pxmlpre {
Subroutine pxmlpre: called CC=2 SCAR=7.1
Pod undocumented
262 3 ▶ @_ == 2 or fp_croak_arity 2;
Branch: 1 branch
BranchTrueFalseTotal
@_ == 2 or fp_croak_arity(2) 3 0 3
263 3 my ($nargs, $fn) = @_;
264 3 build($nargs, _pxmlpre($nargs, $fn))
265 }
266  
267 3 our $maxargs = 10;
268  
269 8 ▶sub pxmlfunc (&) {
Subroutine pxmlfunc: called CC=2 SCAR=6.9
Pod undocumented
270 8 my ($fn) = @_;
271 8 my $items = _pxmlpre($maxargs, $fn);
272 my $nargs
273 5 ▶ = (max(map { $$_[1] } grep { ref($_) eq "ARRAY" } @$items) // -1) + 1;
Condition100%2/2
max(map({$_->[1];} grep({ref $_ eq "ARRAY";} @$items))) // -1
l!l
23
MC/DC100%1/1
max(map({$_->[1];} grep({ref $_ eq "ARRAY";} @$items))) // -1
max(map({$_->[1];} grep({ref $_ eq "ARRAY";} @$items)))
Truth table100%2/2
A
A= max(map({$_->[1];} grep({ref $_ eq "ARRAY";} @$items)))
Aresult
11
00
274 5 build $nargs, $items
275 }
276  
277 3 1
« testmem.pl ↑ Summary lib/FP/url_.pm »