blib/lib/PLP/Functions.pm

filter: Statementstmt 59.2% 74 / 125 Branchbran 37.1% 23 / 62 Conditioncond 0.0% 0 / 3 MC/DCmcdc 0.0% 0 / 2 Subroutinesub 76.1% 16 / 21 Podpod 92.3% 12 / 13 totaltotal 55.3% 125 / 226 CC 37 SCAR 26.5
CC
37
Cov
51%
AutoURL
56.0
Counter
30.0
WriteFile
20.0
CRAP 14.1
« blib/lib/PLP.pm ↑ Summary blib/lib/PLP/Backend/Apache.pm »
1 package PLP::Functions;
2  
3 5 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 5 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
5  
6 5 ▶use base 'Exporter';
Subroutine BEGIN: called CC=1 SCAR=0.0
7 5 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 5 ▶use Fcntl qw(:flock);
Subroutine BEGIN: called CC=1 SCAR=0.0
9  
10 5 our $VERSION = '1.01';
11 5 our @EXPORT = qw/Entity DecodeURI EncodeURI Include include PLP_END
12 EscapeHTML
13 AddCookie ReadFile WriteFile AutoURL Counter exit/;
14  
15 2 ▶sub Include ($) {
Subroutine Include: called CC=3 SCAR=11.0
Pod documented
16 5 ▶ no strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
17 2 $PLP::file = $_[0];
18 2 $PLP::inA = 0;
19 2 $PLP::inB = 0;
20 2 local $@;
21 2 ▶ eval 'package PLP::Script; no warnings; ' . PLP::source($PLP::file, 0, join ' ', (caller)[2,1]);
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine BEGIN: called CC=1 SCAR=0.0
22 2 ▶ if ($@) {
Branch: 1 branch
BranchTrueFalseTotal
if ($@) 1 1 2
23 1 ▶ PLP::Functions::exit() if $@ =~ /\cS\cT\cO\cP/;
Branch: 1 branch
BranchTrueFalseTotal
if $@ =~ /\cS\cT\cO\cP/ 0 1 1
24 1 PLP::error($@, 1);
25 }
26 }
27  
28 2 ▶sub include ($) {
Subroutine include: called CC=1 SCAR=0.0
Pod documented
29 2 goto &Include;
30 }
31  
32 1 ▶sub exit (;$) {
Subroutine exit: called CC=1 SCAR=0.0
Pod undocumented
33 1 die "\cS\cT\cO\cP\n";
34 }
35  
36 2 ▶sub PLP_END (&) {
Subroutine PLP_END: called CC=1 SCAR=0.0
Pod documented
37 2 push @PLP::END, shift;
38 }
39  
40 19 ▶sub EscapeHTML {
Subroutine EscapeHTML: called CC=5 SCAR=16.1
Pod documented
41 19 ▶ @_ == 1 or croak "Unsupported parameters given to EscapeHTML";
Branch: 1 branch
BranchTrueFalseTotal
@_ == 1 or croak("Unsupported parameters given to EscapeHTML") 18 1 19
42 18 ▶ unshift @_, shift if defined wantarray; # dereference if not void
Branch: 1 branch
BranchTrueFalseTotal
if defined wantarray 16 2 18
43 18 for ($_[0]) {
44 18 ▶ defined or next;
Branch: 1 branch
BranchTrueFalseTotal
defined $_ or next 17 1 18
45 17 s/&/&/g;
46 16 s/"/"/g;
47 16 s/</&lt;/g;
48 16 s/>/&gt;/g;
49 }
50 17 return $_[0];
51 }
52  
53 2 ▶sub Entity (@) {
Subroutine Entity: called CC=6 SCAR=18.4
Pod documented
54 2 ▶ my $ref = defined wantarray ? [@_] : \@_;
Branch: 1 branch
BranchTrueFalseTotal
defined wantarray ? : 2 0 2
55 2 for (@$ref) {
56 2 ▶ defined or next;
Branch: 1 branch
BranchTrueFalseTotal
defined $_ or next 2 0 2
57 2 eval {
58 2 s/&/&amp;/g;
59 2 s/"/&quot;/g;
60 2 s/</&lt;/g;
61 2 s/>/&gt;/g;
62 2 s/\n/<br>\n/g;
63 2 s/\t/&nbsp; &nbsp; &nbsp; &nbsp;&nbsp;/g;
64 2 s/ /&nbsp;&nbsp;/g;
65 };
66 }
67 2 ▶ return defined wantarray ? (wantarray ? @$ref : "@$ref") : undef;
Branch: 2 branches
BranchTrueFalseTotal
defined wantarray ? : 2 0 2
wantarray ? : 0 2 2
68 }
69  
70 74 ▶sub DecodeURI (@) {
Subroutine DecodeURI: called CC=6 SCAR=18.0
Pod documented
71 74 ▶ my $ref = defined wantarray ? [@_] : \@_;
Branch: 1 branch
BranchTrueFalseTotal
defined wantarray ? : 2 72 74
72 74 for (@$ref) {
73 122 ▶ defined or next;
Branch: 1 branch
BranchTrueFalseTotal
defined $_ or next 122 0 122
74 122 eval {
75 122 tr/+/ /; # Browsers do tr/ /+/ - I don't care about RFCs, but
76 # I do care about real-life situations.
77 122 s/%([0-9A-Fa-f][0-9A-Fa-f])/chr hex $1/ge;
78 };
79 }
80 74 ▶ return defined wantarray ? (wantarray ? @$ref : "@$ref") : undef;
Branch: 2 branches
BranchTrueFalseTotal
defined wantarray ? : 2 72 74
wantarray ? : 0 2 2
81 }
82  
83 1 ▶sub EncodeURI (@) {
Subroutine EncodeURI: called CC=6 SCAR=19.0
Pod documented
84 1 ▶ my $ref = defined wantarray ? [@_] : \@_;
Branch: 1 branch
BranchTrueFalseTotal
defined wantarray ? : 1 0 1
85 1 for (@$ref) {
86 1 ▶ defined or next;
Branch: 1 branch
BranchTrueFalseTotal
defined $_ or next 1 0 1
87 1 eval {
88 1 s{([^A-Za-z0-9\-_.!~*'()/?:@\$,])}{sprintf("%%%02x", ord $1)}ge;
89 };
90 }
91 1 ▶ return defined wantarray ? (wantarray ? @$ref : "@$ref") : undef;
Branch: 2 branches
BranchTrueFalseTotal
defined wantarray ? : 1 0 1
wantarray ? : 0 1 1
92 }
93  
94 0 ▶sub AddCookie ($) {
Subroutine AddCookie: not called CC=2 SCAR=17.9
Pod documented
95 0 ▶ if ($PLP::Script::header{'Set-Cookie'}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($PLP::Script::header{'Set-Cookie'}) { } 0 0 0
96 0 $PLP::Script::header{'Set-Cookie'} .= "\n" . $_[0];
97 } else {
98 0 $PLP::Script::header{'Set-Cookie'} = $_[0];
99 }
100 }
101  
102 0 ▶sub ReadFile ($) {
Subroutine ReadFile: not called CC=2 SCAR=17.9
Pod documented
103 0 local $/ = undef;
104 0 ▶ open (my $fh, '<', $_[0]) or do {
Branch: 1 branch
BranchTrueFalseTotal
open my $fh, "<", $_[0] or do { PLP::error("Cannot open $_[0] for reading ($!)", 1); return undef } 0 0 0
105 0 PLP::error("Cannot open $_[0] for reading ($!)", 1);
106 0 return undef;
107 };
108 0 my $r = readline $fh;
109 0 close $fh;
110 0 return $r;
111 }
112  
113 0 ▶sub WriteFile ($$) {
Subroutine WriteFile: not called CC=4 SCAR=30.0
Pod documented
114 0 ▶ open (my $fh, '>', $_[0]) or do {
Branch: 1 branch
BranchTrueFalseTotal
open my $fh, ">", $_[0] or do { PLP::error("Cannot open $_[0] for writing ($!)", 1); return undef } 0 0 0
115 0 PLP::error("Cannot open $_[0] for writing ($!)", 1);
116 0 return undef;
117 };
118 0 flock $fh, LOCK_EX;
119 0 ▶ print $fh $_[1] or do {
Branch: 1 branch
BranchTrueFalseTotal
print $fh $_[1] or do { PLP::error("Cannot write to $_[0] ($!)"); return undef } 0 0 0
120 0 PLP::error("Cannot write to $_[0] ($!)");
121 0 return undef;
122 };
123 0 ▶ close $fh or do {
Branch: 1 branch
BranchTrueFalseTotal
close $fh or do { PLP::error("Cannot close $_[0] ($!)"); return undef } 0 0 0
124 0 PLP::error("Cannot close $_[0] ($!)");
125 0 return undef;
126 };
127 0 return 1;
128 }
129  
130 0 ▶sub Counter ($) {
Subroutine Counter: not called CC=5 SCAR=34.0
Pod documented
131 0 local $/ = undef;
132 0 my $fh;
133 0 ▶ open $fh, '+<', $_[0] or
Branch: 1 branch
BranchTrueFalseTotal
open $fh, "+<", $_[0] or open $fh, ">", $_[0] or return undef 0 0 0
Condition0%0/3
open $fh, "+<", $_[0] or open $fh, ">", $_[0]
l!l&&r!l&&!r
000
MC/DC0%0/2
open $fh, "+<", $_[0] or open $fh, ">", $_[0]
open $fh, "+<", $_[0] open $fh, ">", $_[0]
Truth table0%0/3
A or B
A= open $fh, "+<", $_[0]B= open $fh, ">", $_[0]
ABresult
1X1
011
000
134 open $fh, '>', $_[0] or return undef;
135 0 flock $fh, 2;
136 0 seek $fh, 0, 0;
137 0 my $counter = <$fh>;
138 0 seek $fh, 0, 0;
139 0 truncate $fh, 0;
140 0 ▶ print $fh ++$counter or return undef;
Branch: 1 branch
BranchTrueFalseTotal
print $fh ++$counter or return undef 0 0 0
141 0 ▶ close $fh or return undef;
Branch: 1 branch
BranchTrueFalseTotal
close $fh or return undef 0 0 0
142 0 return $counter;
143 }
144  
145 0 ▶sub AutoURL ($) {
Subroutine AutoURL: not called CC=7 SCAR=40.3
Pod documented
146 # This sub assumes your string does not match /(["<>])\cC\1/
147 0 ▶ my $ref = defined wantarray ? \(my $copy = $_[0]) : \$_[0];
Branch: 1 branch
BranchTrueFalseTotal
defined wantarray ? : 0 0 0
148 0 eval {
149 0 $$ref =~ s/&quot;/"\cC"/g; # Single characters are easier to match :)
150 0 $$ref =~ s/&gt;/>\cC>/g; # so we can just use a character class []
151 0 $$ref =~ s/&lt;/<\cC</g;
152
153 # Now this is a big, ugly regex! But hey - it works :)
154 0 $$ref =~ s{((\w+://|www\.|WWW\.)[a-zA-Z0-9\.\@:-]+[^\"\'>< \r\t\n]*)}{
155 0 local $_ = $1;
156 0 my $scheme = $2;
157 0 ▶ s/// if (my $trailing) = /([\.,!\?\(\)\[\]]+$)/;
Branch: 1 branch
BranchTrueFalseTotal
if my($trailing) = /([\.,!\?\(\)\[\]]+$)/ 0 0 0
158 0 s/&(?!\x23?\w+;)/&amp;/g;
159 0 s/\"/&quot;/g;
160 0 ▶ my $href = ($scheme =~ /www\./i ? "http://$_" : $_);
Branch: 1 branch
BranchTrueFalseTotal
$scheme =~ /www\./i ? : 0 0 0
161 0 qq{<a href="$href" target="_blank">$_</a>$trailing};
162 }eg;
163  
164 0 $$ref =~ s/"\cC"/&quot;/g;
165 0 $$ref =~ s/>\cC>/&gt;/g;
166 0 $$ref =~ s/<\cC</&lt;/g;
167 };
168 0 ▶ if ($@){ return defined wantarray ? @_ : undef } # return original on error
Branch: 2 branches
BranchTrueFalseTotal
if ($@) 0 0 0
defined wantarray ? : 0 0 0
169 0 ▶ return defined wantarray ? $$ref : undef;
Branch: 1 branch
BranchTrueFalseTotal
defined wantarray ? : 0 0 0
170 }
171  
172 5 1;
173  
174 =head1 NAME
175  
176 PLP::Functions - Functions that are available in PLP documents
177  
178 =head1 DESCRIPTION
179  
180 The functions are exported into the PLP::Script package that is used by PLP documents. Although uppercased letters are unusual in Perl, they were chosen to stand out.
181  
182 Most of these functions are context-hybird. Before using them, one should know about contexts in Perl. The three major contexts are: B<void>, B<scalar> and B<list> context. You'll find more about context in L<perlfunc>.
183  
184 Some context examples:
185  
186 print foo(); # foo is in list context (print LIST)
187 foo(); # foo is in void context
188 $bar = foo(); # foo is in scalar context
189 @bar = foo(); # foo is in list context
190 length foo(); # foo is in scalar context (length EXPR)
191  
192 =head2 The functions
193  
194 =over 10
195  
196 =item Include FILENAME
197  
198 Executes another PLP file, that will be parsed (i.e. code must be in C<< <: :> >>). As with Perl's C<do>, the file is evaluated in its own lexical file scope, so lexical variables (C<my> variables) are not shared. PLP's C<< <(filename)> >> includes at compile-time, is faster and is doesn't create a lexical scope (it shares lexical variables).
199  
200 Include can be used recursively, and there is no depth limit:
201  
202 <!-- This is crash.plp -->
203 <:
204 include 'crash.plp';
205 # This example will loop forever,
206 # and dies with an out of memory error.
207 # Do not try this at home.
208 :>
209  
210 =item include FILENAME
211  
212 An alias for C<Include>.
213  
214 =item PLP_END BLOCK
215  
216 Adds a piece of code that is executed when at the end of the PLP document. This is useful when creating a template file:
217  
218 <html><body> <!-- this is template.plp -->
219 <: PLP_END { :>
220 </body></html>
221 <: } :>
222  
223 <(template.plp)> <!-- this is index.plp -->
224 Hello, world!
225  
226 You should use this function instead of Perl's built-in C<END> blocks, because those do not work properly with mod_perl.
227  
228 =item EscapeHTML STRING
229  
230 Replaces HTML syntax characters by HTML entities, so the text can be output safely.
231 You should always use this when displaying user input (or database output),
232 to avoid cross-site-scripting vurnerabilities.
233  
234 In void context, B<changes> the value of the given variable.
235  
236 <: EscapeHTML($user_input); print "<pre>$user_input</pre>"; :>
237  
238 In other contexts, returns the changed version.
239  
240 <a href="<:= EscapeHTML($ENV{REQUEST_URI}) :>">
241  
242 Be warned that single quotes are not substituted, so always use double quotes for attributes.
243 Also does not convert whitespace for formatted output; use Entity() for that.
244  
245 To escape high-bit characters as well, refer to L<HTML::Entities|HTML::Entities>.
246  
247 =item Entity LIST
248  
249 Formats given arguments for literal display in HTML documents.
250 Similar to EscapeHTML(), but also preserves newlines and consecutive spaces
251 using corresponding C<< <br> >> and C<&nbsp;> respectively.
252  
253 In void context, B<changes> the values of the given variables. In other contexts, returns the changed versions.
254  
255 <: print '<p>' . Entity($user_input) . '</p>'; :>
256  
257 Inside attributes, always use EscapeHTML() instead.
258  
259 =item EncodeURI LIST
260  
261 Encodes URI strings according to RFC 3986. All disallowed characters are replaced by their %-encoded values.
262  
263 In void context, B<changes> the values of the given variables. In other contexts, returns the changed versions.
264  
265 <a href="/foo.plp?name=<:= EncodeURI($name) :>">Link</a>
266  
267 Note that the following reserved characters are I<not> percent-encoded, even though they may have a special meaning in URIs:
268  
269 / ? : @ $
270  
271 This should be safe for escaping query values (as in the example above),
272 but otherwise it may be a better idea to use L<URI::Escape|URI::Escape> instead.
273  
274 =item DecodeURI LIST
275  
276 Decodes %-encoded strings. Unlike L<URI::Escape|URI::Escape>,
277 it also translates + characters to spaces (as browsers use those).
278  
279 In void context, B<changes> the values of the given variables. In other contexts, returns the changed versions.
280  
281 =item ReadFile FILENAME
282  
283 Returns the contents of FILENAME in one large string. Returns undef on failure.
284  
285 =item WriteFile FILENAME, STRING
286  
287 Writes STRING to FILENAME (overwrites FILENAME if it already exists). Returns true on success, false on failure.
288  
289 =item Counter FILENAME
290  
291 Increases the contents of FILENAME by one and returns the new value. Returns undef on failure. Fails silently.
292  
293 You are visitor number <:= Counter('counter.txt') :>.
294  
295 =item AutoURL STRING
296  
297 Replaces URLs (actually, replace things that look like URLs) by links.
298  
299 In void context, B<changes> the value of the given variable. In other contexts, returns the changed version.
300  
301 <: print AutoURL(Entity($user_input)); :>
302  
303 =item AddCookie STRING
304  
305 Adds a Set-Cookie header. STRING must be a valid Set-Cookie header value.
306  
307 =back
308  
309 =head1 AUTHOR
310  
311 Juerd Waalboer <juerd@cpan.org>
312  
313 Current maintainer: Mischa POSLAWSKY <shiar@cpan.org>
314  
315 =cut
316  
« blib/lib/PLP.pm ↑ Summary blib/lib/PLP/Backend/Apache.pm »