lib/Pod/Usage/CGI.pm

filter: Statementstmt 86.9% 40 / 46 Branchbran 50.0% 7 / 14 Conditioncond 33.3% 2 / 6 MC/DCmcdc 0.0% 0 / 4 Subroutinesub 100% 5 / 5 Podpod 100% 1 / 1 totaltotal 72.3% 55 / 76 CC 11 SCAR 23.6
CC
11
Cov
74%
pod2usage
15.2
_html_escape
2.0
BEGIN
1.0
CRAP 10.6
↑ Summary t/test_pod_usage.cgi »
1 package Pod::Usage::CGI;
2  
3 3 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 3 ▶use Exporter;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 3 ▶use vars qw($VERSION @ISA @EXPORT);
Subroutine BEGIN: called CC=1 SCAR=0.0
6 3 $VERSION = sprintf'%d.%03d', q$Revision: 1.10 $ =~ /: (\d+)\.(\d+)/;
7 3 @ISA=qw(Exporter);
8 3 @EXPORT=qw(pod2usage);
9  
10 sub pod2usage
11 1 ▶{
Subroutine pod2usage: called CC=10 SCAR=27.2
Pod documented
12 1 my %options = @_;
13 1 ▶ my $message = '<div class="message">'._html_escape($options{message})."</div>\n" || $options{raw_message};
Condition33%1/3
'<div class="message">' . _html_escape($options{'message'}) . "</div>\n" || $options{'raw_message'}
l!l&&r!l&&!r
100
MC/DC0%0/2
'<div class="message">' . _html_escape($options{'message'}) . "</div>\n" || $options{'raw_message'}
'<div class="message">' . _html_escape($options{'message'}) . "</div>\n" $options{'raw_message'}
Truth table33%1/3
A || B
A= '<div class="message">' . _html_escape($options{'message'}) . "</div>\n"B= $options{'raw_message'}
ABresult
1X1
011
000
14 1 my $css = delete $options{css};
15 1 ▶ $css = [$css] if($css && ref $css ne 'array');
Branch: 1 branch
BranchTrueFalseTotal
if $css and ref $css ne "array" 1 0 1
Condition33%1/3
$css and ref $css ne "array"
!ll&&!rl&&r
001
MC/DC0%0/2
$css and ref $css ne "array"
$css ref $css ne "array"
Truth table33%1/3
A and B
A= $cssB= ref $css ne "array"
ABresult
0X0
100
111
16 1 ▶ my $file = ($0 eq '-e')? undef : $0;
Branch: 1 branch
BranchTrueFalseTotal
$0 eq '-e' ? : 0 1 1
17  
18 1 require Pod::Xhtml;
19 1 my $parser = new Pod::Xhtml(%options, StringMode => 1);
20 1 ▶ if($css) {
Branch: 1 branch
BranchTrueFalseTotal
if ($css) 1 0 1
21 1 $parser->addHeadText(qq[<link rel="stylesheet" href="$_"/>\n]) for @$css;
22 }
23 1 ▶ $parser->addBodyOpenText($message) if($message);
Branch: 1 branch
BranchTrueFalseTotal
if $message 1 0 1
24 1 my $usage = "";
25 1 ▶ if($file) {
Branch: 1 branch
BranchTrueFalseTotal
if ($file) 1 0 1
26 1 $parser->parse_from_file($file);
27 1 $usage = $parser->asString;
28 }
29  
30 1 ▶ if($ENV{MOD_PERL}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($ENV{'MOD_PERL'}) { } 0 1 1
31 # Although Apache::Registry would do this for us
32 # we do this to support any variants that may not
33 0 require Apache;
34 0 my $r = Apache->request;
35 0 $r->content_type("text/html");
36 0 $r->send_http_header;
37 0 $r->print($usage);
38 0 Apache::exit();
39 } else {
40 1 require CGI;
41 1 print CGI::header();
42 1 print $usage;
43 1 exit;
44 }
45  
46 }
47  
48 sub _html_escape
49 1 ▶{
Subroutine _html_escape: called CC=2 SCAR=7.0
50 1 my $str = shift;
51 1 ▶ return '' unless length $str;
Branch: 1 branch
BranchTrueFalseTotal
unless length $str 0 1 1
52 1 $str =~ s/&/&amp;/g;
53 1 $str =~ s/</&lt;/g;
54 1 $str =~ s/>/&gt;/g;
55 1 $str =~ s/'/&apos;/g;
56 1 $str =~ s/\"/&quot;/g;
57 1 return $str;
58 }
59  
60 3 1;
61  
62 =head1 NAME
63  
64 Pod::Usage::CGI - generate usage message for CGI scripts
65  
66 =head1 SYNOPSIS
67  
68 use CGI;
69 use Pod::Usage::CGI;
70  
71 #Message is HTML-escaped
72 my $necessary = CGI::param(foo) || pod2usage(message => "you forgot >>foo<<");
73  
74 #Raw message is not escaped
75 my $another = CGI::param(bar) || pod2usage(raw_message => "you forgot <b>bar</b>");
76  
77 =head1 DESCRIPTION
78  
79 Provides pod2usage exit from CGI scripts. You may optionally supply a message.
80 By default the message text is escaped to prevent cross-site scripting injection attacks and placed in a div container of class "message" that you can optionally format with a CSS.
81 You can use the C<raw_message> directive if you want to write HTML out into the page and manage your own escaping.
82  
83 The module works fine under Apache::Registry but will not work in any environments where $0 is not defined.
84  
85 =head1 FUNCTIONS
86  
87 =over 4
88  
89 =item pod2usage(%options)
90  
91 Displays usage and exits. Valid options are:
92  
93 message - message (will be automatically escaped)
94 raw_message - message (not escaped)
95 css - one or more CSS URLs to be applied to the page (either a scalar or an arrayref)
96  
97 =back
98  
99 =head1 DEPENDENCIES
100  
101 L<Pod::XHtml> and either L<Apache> or L<CGI> are loaded on demand if required
102  
103 =head1 SEE ALSO
104  
105 =over 4
106  
107 =item L<Pod::Usage>
108  
109 Generates usage messages for command line scripts
110  
111 =back
112  
113 =head1 VERSION
114  
115 $Revision: 1.10 $ on $Date: 2005/07/15 11:25:22 $ by $Author: simonf $
116  
117 =head1 AUTHOR
118  
119 John Alden E<lt>cpan _at_ bbc _dot_ co _dot_ ukE<gt>
120  
121 =head1 COPYRIGHT
122  
123 (c) BBC 2005. This program is free software; you can redistribute it and/or modify it under the GNU GPL.
124 See the file COPYING in this distribution, or http://www.gnu.org/licenses/gpl.txt
125  
126 =cut
↑ Summary t/test_pod_usage.cgi »