blib/lib/HTML/Display.pm

filter: Statementstmt 91.0% 61 / 67 Branchbran 68.7% 11 / 16 Conditioncond 64.2% 9 / 14 MC/DCmcdc 37.5% 3 / 8 Subroutinesub 100% 14 / 14 Podpod 100% 2 / 2 totaltotal 82.6% 100 / 121 CC 15 SCAR 19.5
CC
15
Cov
84%
new
14.5
__ANON__
2.2
display
2.1
CRAP 7.0
« blib/lib/HTML/Display/Common.pm ↑ Summary blib/lib/HTML/Display/Win32/IE.pm »
1 package HTML::Display;
2 6 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
3 6 ▶use HTML::TokeParser;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 6 ▶use Carp qw( croak );
Subroutine BEGIN: called CC=1 SCAR=0.0
5 6 ▶use vars qw( $VERSION );
Subroutine BEGIN: called CC=1 SCAR=0.0
6 5 $VERSION='0.40';
7  
8 =head1 NAME
9  
10 HTML::Display - display HTML locally in a browser
11  
12 =head1 SYNOPSIS
13  
14 =for example
15 my $html = "foo\n";
16 %HTML::Display::os_default = ();
17 delete $ENV{PERL_HTML_DISPLAY_CLASS};
18  
19 =for example begin
20  
21 1 ▶ use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
22 1 ▶ use HTML::Display;
Subroutine BEGIN: called CC=1 SCAR=0.0
23  
24 # guess the best value from $ENV{PERL_HTML_DISPLAY_CLASS}
25 # or $ENV{PERL_HTML_DISPLAY_COMMAND}
26 # or the operating system, in that order
27 my $browser = HTML::Display->new();
28 warn "# Displaying HTML using " . ref $browser;
29 my $location = "http://www.google.com/";
30 $browser->display(html => $html, location => $location);
31  
32 # Or, for a one-off job :
33 display("<html><body><h1>Hello world!</h1></body></html>");
34  
35 =for example end
36  
37 =for example_testing
38 is($::_STDOUT_,"foo\n<html><body><h1>Hello world!</h1></body></html>");
39  
40 =head1 DESCRIPTION
41  
42 This module abstracts the task of displaying HTML to the user. The
43 displaying is done by launching a browser and navigating it to either
44 a temporary file with the HTML stored in it, or, if possible, by
45 pushing the HTML directly into the browser window.
46  
47 The module tries to automagically select the "correct" browser, but
48 if it dosen't find a good browser, you can modify the behaviour by
49 setting some environment variables :
50  
51 PERL_HTML_DISPLAY_CLASS
52  
53 If HTML::Display already provides a class for the browser you want to
54 use, setting C<PERL_HTML_DISPLAY_CLASS> to the name of the class will
55 make HTML::Display use that class instead of what it detects.
56  
57 PERL_HTML_DISPLAY_COMMAND
58  
59 If there is no specialized class yet, but your browser can be controlled
60 via the command line, then setting C<PERL_HTML_DISPLAY_COMMAND> to the
61 string to navigate to the URL will make HTML::Display use a C<system()>
62 call to the string. A C<%s> in the value will be replaced with the name
63 of the temporary file containing the HTML to display.
64  
65 =cut
66  
67 6 ▶use vars qw( @ISA @EXPORT %os_default );
Subroutine BEGIN: called CC=1 SCAR=0.0
68 5 require Exporter;
69 5 @ISA='Exporter';
70  
71 5 @EXPORT = qw( display );
72  
73 =head2 %HTML::Display::os_default
74  
75 The hash C<%HTML::Display::os_default> contains pairs of class names
76 for the different operating systems and routines that test whether
77 this script is currently running under it. If you you want to dynamically
78 add a new class or replace a class (or the rule), modify C<%os_default> :
79  
80 =for example begin
81  
82 # Install class for MagicOS
83 $HTML::Display::os_default{"HTML::Display::MagicOS"}
84 = sub { $^O =~ qr/magic/i };
85  
86 =for example end
87  
88 =cut
89  
90 %os_default = (
91 2 ▶ "HTML::Display::Win32::IE" => sub {
Subroutine __ANON__: called CC=2 SCAR=7.9
92 3 my $have_ole;
93 2 eval {
94 2 require Win32::OLE;
95 0 Win32::OLE->import();
96 0 $have_ole = 1;
97 };
98 1 ▶ $have_ole and $^O =~ qr/mswin32/i
Branch: 1 branch
BranchTrueFalseTotal
$have_ole and $^O =~ qr/mswin32/i 0 1 1
99 },
100 1 ▶ "HTML::Display::Debian" => sub { -x "/usr/bin/x-www-browser" },
Subroutine __ANON__: called CC=1 SCAR=0.0
101 1 ▶ "HTML::Display::OSX" => sub { $^O =~ qr/darwin/i },
Subroutine __ANON__: called CC=1 SCAR=0.0
102 5 );
103  
104 =head2 __PACKAGE__->new %ARGS
105  
106 =cut
107  
108 7 ▶sub new {
Subroutine new: called CC=13 SCAR=26.7
Pod documented
109 7 my $class = shift;
110 7 my (%args) = @_;
111  
112 # First see whether the programmer or user specified a class
113 7 ▶ my $best_class = delete $args{class} || $ENV{PERL_HTML_DISPLAY_CLASS};
Condition66%2/3
delete $args{'class'} || $ENV{'PERL_HTML_DISPLAY_CLASS'}
l!l&&r!l&&!r
403
MC/DC50%1/2
delete $args{'class'} || $ENV{'PERL_HTML_DISPLAY_CLASS'}
delete $args{'class'} $ENV{'PERL_HTML_DISPLAY_CLASS'}
Truth table66%2/3
A || B
A= delete $args{'class'}B= $ENV{'PERL_HTML_DISPLAY_CLASS'}
ABresult
1X1
011
000
114  
115 # Now, did they specify a command?
116 7 ▶ unless ($best_class) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($best_class) 3 4 7
117 3 ▶ my $command = delete $args{browsercmd} || $ENV{PERL_HTML_DISPLAY_COMMAND};
Condition33%1/3
delete $args{'browsercmd'} || $ENV{'PERL_HTML_DISPLAY_COMMAND'}
l!l&&r!l&&!r
003
MC/DC0%0/2
delete $args{'browsercmd'} || $ENV{'PERL_HTML_DISPLAY_COMMAND'}
delete $args{'browsercmd'} $ENV{'PERL_HTML_DISPLAY_COMMAND'}
Truth table33%1/3
A || B
A= delete $args{'browsercmd'}B= $ENV{'PERL_HTML_DISPLAY_COMMAND'}
ABresult
1X1
011
000
118 3 ▶ if ($command) {
Branch: 1 branch
BranchTrueFalseTotal
if ($command) 0 3 3
119 0 $best_class = "HTML::Display::TempFile";
120 0 $args{browsercmd} = $command;
121 0 @_ = %args;
122 };
123 };
124  
125 7 ▶ unless ($best_class) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($best_class) 3 4 7
126 3 for my $class (sort keys %os_default) {
127 $best_class = $class
128 3 ▶ if $os_default{$class}->();
Branch: 1 branch
BranchTrueFalseTotal
if $HTML::Display::os_default{$class}->() 0 3 3
129 };
130 };
131 7 ▶ $best_class ||= "HTML::Display::Dump";
Condition100%2/2
$best_class ||= "HTML::Display::Dump"
l!l
43
MC/DC100%1/1
$best_class ||= "HTML::Display::Dump"
$best_class
Truth table100%2/2
A
A= $best_class
Aresult
11
00
132  
133 6 ▶ { no strict 'refs';
Subroutine BEGIN: called CC=1 SCAR=0.0
134 7 undef $@;
135 eval "use $best_class;"
136 7 unless ( @{"${best_class}::ISA"}
137 3 or defined *{"${best_class}::new"}{CODE}
138 5 ▶ or defined *{"${best_class}::AUTOLOAD"}{CODE});
Subroutine BEGIN: called CC=1 SCAR=0.0
Branch: 1 branch
BranchTrueFalseTotal
unless @{"${best_class}::ISA";} or defined *{"${best_class}::new";}{"CODE"} or defined *{"${best_class}::AUTOLOAD";}{"CODE"} 3 4 7
Condition66%4/6
@{"${best_class}::ISA";} or defined *{"${best_class}::new";}{"CODE"} or defined *{"${best_class}::AUTOLOAD";}{"CODE"}
l!l&&r!l&&!r
403
@{"${best_class}::ISA";} or defined *{"${best_class}::new";}{"CODE"}
l!l&&r!l&&!r
403
MC/DC33%1/3
@{"${best_class}::ISA";} or defined *{"${best_class}::new";}{"CODE"} or defined *{"${best_class}::AUTOLOAD";}{"CODE"}
@{"${best_class}::ISA";} defined *{"${best_class}::new";}{"CODE"} defined *{"${best_class}::AUTOLOAD";}{"CODE"}
Truth table50%2/4
A or B or C
A= @{"${best_class}::ISA";}B= defined *{"${best_class}::new";}{"CODE"}C= defined *{"${best_class}::AUTOLOAD";}{"CODE"}
ABCresult
1XX1
01X1
0011
0000
139 7 ▶ croak "While trying to load $best_class: $@" if $@;
Branch: 1 branch
BranchTrueFalseTotal
if $@ 0 7 7
140 };
141 7 return $best_class->new(@_);
142 };
143  
144 =head2 $browser-E<gt>display( %ARGS )
145  
146 Will display the HTML. The following arguments are valid :
147  
148 base => Base to which all relative links will be resolved
149 html => Scalar containing the HTML to be displayed
150 file => Scalar containing the name of the file to be displayed
151 This file will possibly be copied into a temporary file!
152  
153 location (synonymous to base)
154  
155 If only one argument is passed, then it is taken as if
156  
157 html => $_[0]
158  
159 was passed.
160  
161 =cut
162  
163 1 ▶sub display {
Subroutine display: called CC=2 SCAR=7.4
Pod documented
164 1 my %args;
165 1 ▶ if (scalar @_ == 1) {
Branch: 1 branch
BranchTrueFalseTotal
if (scalar @_ == 1) { } 1 0 1
166 1 %args = ( html => @_ )
167 } else {
168 0 %args = @_
169 };
170 1 HTML::Display->new()->display( %args );
171 };
172  
173 =head1 EXPORTS
174  
175 The subroutine C<display> is exported by default
176  
177 =head1 COMMAND LINE USAGE
178  
179 Display some HTML to the user :
180  
181 perl -MHTML::Display -e "display '<html><body><h1>Hello world</body></html>'"
182  
183 Display a web page to the user :
184  
185 perl -MLWP::Simple -MHTML::Display -e "display get 'http://www.google.com'"
186  
187 Display the same page with the images also working :
188  
189 perl -MLWP::Simple -MHTML::Display -e "display html => get('http://www.google.com'),
190 location => 'http://www.google.com'"
191  
192 =head1 AUTHOR
193  
194 Copyright (c) 2004-2007 Max Maischein C<< <corion@cpan.org> >>
195  
196 =head1 LICENSE
197  
198 This module is released under the same terms as Perl itself.
199  
200 =cut
201  
202  
203 5 1;
« blib/lib/HTML/Display/Common.pm ↑ Summary blib/lib/HTML/Display/Win32/IE.pm »