script/mech-dump

filter: Statementstmt 64.3% 65 / 101 Branchbran 47.5% 19 / 40 Conditioncond 0.0% 0 / 3 MC/DCmcdc 0.0% 0 / 2 Subroutinesub 57.1% 12 / 21 Podpod n/a totaltotal 57.4% 96 / 167 CC 8 SCAR 15.1
CC
8
Cov
58%
completions
20.0
dump_headers
6.0
dump_forms
2.0
CRAP 4.5
« blib/lib/WWW/Mechanize.pm ↑ Summary blib/lib/WWW/Mechanize/Image.pm »
1 #!/usr/bin/perl
2  
3 # PODNAME: mech-dump
4 # ABSTRACT: Dumps information about a web page
5  
6 2 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 2 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 2 ▶use WWW::Mechanize ();
Subroutine BEGIN: called CC=1 SCAR=0.0
9 2 ▶use Getopt::Long;
Subroutine BEGIN: called CC=1 SCAR=0.0
10 2 ▶use Pod::Usage;
Subroutine BEGIN: called CC=1 SCAR=0.0
11  
12 2 ▶use HTTP::Cookies;
Subroutine BEGIN: called CC=1 SCAR=0.0
13 2 my @actions;
14 my $absolute;
15 2 my $all;
16  
17 2 my $user;
18 2 my $pass;
19 2 my $agent;
20 2 my $agent_alias;
21 2 my $cookie_filename;
22  
23 my %command_line_options = (
24 'user=s' => \$user,
25 'password=s' => \$pass,
26 0 ▶ headers => sub { push( @actions, \&dump_headers ) },
Subroutine __ANON__: not called CC=1 SCAR=6.9
27 1 ▶ forms => sub { push( @actions, \&dump_forms ) },
Subroutine __ANON__: called CC=1 SCAR=0.0
28 1 ▶ links => sub { push( @actions, \&dump_links ) },
Subroutine __ANON__: called CC=1 SCAR=0.0
29 1 ▶ images => sub { push( @actions, \&dump_images ) },
Subroutine __ANON__: called CC=1 SCAR=0.0
30 all => sub {
31 0 ▶ $all++;
Subroutine __ANON__: not called CC=1 SCAR=6.9
32 0 push(
33 @actions,
34 \&dump_headers, \&dump_forms, \&dump_links, \&dump_images
35 );
36 },
37 0 ▶ text => sub { push( @actions, \&dump_text ) },
Subroutine __ANON__: not called CC=1 SCAR=6.9
38 'absolute!' => \$absolute,
39 'agent=s' => \$agent,
40 'agent-alias=s' => \$agent_alias,
41 'cookie-file=s' => \$cookie_filename,
42 0 ▶ help => sub { pod2usage(1); },
Subroutine __ANON__: not called CC=1 SCAR=6.9
43 0 ▶ version => sub { print STDERR $WWW::Mechanize::VERSION, "\n"; exit 0; },
Subroutine __ANON__: not called CC=1 SCAR=6.9
44 0 ▶ 'completions' => sub { completions(@ARGV); },
Subroutine __ANON__: not called CC=1 SCAR=6.9
45 2 );
46 2 ▶GetOptions(%command_line_options) or pod2usage(2);
Branch: 1 branch
BranchTrueFalseTotal
&GetOptions(%command_line_options) or pod2usage(2) 0 2 2
47  
48 sub completions {
49 0 ▶ my (@words) = @_;
Subroutine completions: not called CC=4 SCAR=30.0
50 0 my @opts;
51 0 foreach ( sort keys %command_line_options ) {
52 0 ▶ if (m/^ (?<opt> [^!]+) ! $/msx) {
Branch: 2 branches
BranchTrueFalseTotal
if (/^ (?<opt> [^!]+) ! $/msx) { } 0 0 0
elsif (/^ (?<opt> [^=]+) = [siof]{1} $/msx) { } 0 0 0
53 0 push @opts, $+{opt}, 'no-' . $+{opt};
54 }
55 elsif (m/^ (?<opt> [^=]+) = [siof]{1} $/msx) {
56 0 push @opts, ( split qr{\|}, $+{opt} );
57 }
58 else {
59 0 push @opts, $_;
60 }
61 }
62 0 print join "\n", ( map { q{--} . $_ } @opts );
63 0 print "\n";
64 0 exit 0;
65 }
66  
67  
68 2 ▶my @uris = @ARGV
Branch: 1 branch
BranchTrueFalseTotal
my(@uris) = @ARGV or die "Must specify a URL or file to check. See --help for details.\n" 0 2 2
69 or die "Must specify a URL or file to check. See --help for details.\n";
70  
71 2 ▶@actions = ( \&dump_forms ) unless @actions;
Branch: 1 branch
BranchTrueFalseTotal
unless @actions 1 1 2
72  
73 2 binmode( STDOUT, ':utf8' );
74  
75 2 my $mech = WWW::Mechanize->new( autocheck => 0 );
76 2 ▶if ( defined $agent ) {
Branch: 2 branches
BranchTrueFalseTotal
if (defined $agent) { } 0 2 2
elsif (defined $agent_alias) { } 0 2 2
77 0 $mech->agent($agent);
78 }
79 elsif ( defined $agent_alias ) {
80 0 $mech->agent_alias($agent_alias);
81 }
82 2 ▶if ( defined $cookie_filename ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $cookie_filename) { } 0 2 2
83 0 my $cookies = HTTP::Cookies->new(
84 file => $cookie_filename,
85 autosave => 1,
86 ignore_discard => 1
87 );
88 0 $cookies->load();
89 0 $mech->cookie_jar($cookies);
90 }
91 else {
92 2 $mech->cookie_jar(undef);
93 }
94  
95 2 $mech->env_proxy();
96 2 foreach my $uri (@uris) {
97 4 my $no_ct_check;
98 4 ▶ if ( -e $uri ) {
Branch: 1 branch
BranchTrueFalseTotal
if (-e $uri) 2 2 4
99 2 require URI::file;
100 2 $uri = URI::file->new_abs($uri)->as_string;
101 2 $no_ct_check = 1; # we don't have to check the content type
102 }
103  
104 4 my $response = $mech->get($uri);
105 4 ▶ if ( !$response->is_success ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($response->is_success) 1 3 4
106 1 ▶ if ( defined( $response->www_authenticate ) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $response->www_authenticate) { } 0 1 1
107 0 ▶ if ( !defined $user or !defined $pass ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $user and defined $pass) 0 0 0
Condition0%0/3
defined $user and defined $pass
!ll&&!rl&&r
000
MC/DC0%0/2
defined $user and defined $pass
defined $user defined $pass
Truth table0%0/3
A and B
A= defined $userB= defined $pass
ABresult
0X0
100
111
108 0 die(
109 "Page requires username and password, but none specified.\n"
110 );
111 }
112 0 $mech->credentials( $user, $pass );
113 0 $response = $mech->get($uri);
114 0 ▶ $response->is_success
Branch: 1 branch
BranchTrueFalseTotal
$response->is_success or die "Can't fetch $uri with username and password\n", $response->status_line, "\n" 0 0 0
115 or die "Can't fetch $uri with username and password\n",
116 $response->status_line, "\n";
117 }
118 else {
119 1 die "$uri returns status ", $response->code, "\n";
120 }
121 }
122  
123 3 ▶ unless ($no_ct_check) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($no_ct_check) 1 2 3
124 1 ▶ $mech->is_html
Branch: 1 branch
BranchTrueFalseTotal
$mech->is_html or die qq[$uri returns type "], $mech->ct, "\", not \"text/html\"\n" 0 1 1
125 or die qq{$uri returns type "}, $mech->ct,
126 qq{", not "text/html"\n};
127 }
128  
129 3 foreach my $action (@actions) {
130 9 $action->($mech);
131 9 ▶ print "\n" if @actions;
Branch: 1 branch
BranchTrueFalseTotal
if @actions 9 0 9
132 }
133 }
134  
135 sub dump_headers {
136 0 ▶ my $mech = shift;
Subroutine dump_headers: not called CC=2 SCAR=17.9
137 0 ▶ print "--> Headers:\n" if $all;
Branch: 1 branch
BranchTrueFalseTotal
if $all 0 0 0
138 0 $mech->dump_headers(undef);
139 0 return;
140 }
141  
142 sub dump_forms {
143 3 ▶ my $mech = shift;
Subroutine dump_forms: called CC=2 SCAR=7.0
144 3 ▶ print "--> Forms:\n" if $all;
Branch: 1 branch
BranchTrueFalseTotal
if $all 0 3 3
145 3 $mech->dump_forms(undef);
146 3 return;
147 }
148  
149 sub dump_links {
150 3 ▶ my $mech = shift;
Subroutine dump_links: called CC=2 SCAR=7.0
151 3 ▶ print "--> Links:\n" if $all;
Branch: 1 branch
BranchTrueFalseTotal
if $all 0 3 3
152 3 $mech->dump_links( undef, $absolute );
153 3 return;
154 }
155  
156 sub dump_images {
157 3 ▶ my $mech = shift;
Subroutine dump_images: called CC=2 SCAR=7.0
158 3 ▶ print "--> Images:\n" if $all;
Branch: 1 branch
BranchTrueFalseTotal
if $all 0 3 3
159 3 $mech->dump_images( undef, $absolute );
160 3 return;
161 }
162  
163 sub dump_text {
164 0 ▶ my $mech = shift;
Subroutine dump_text: not called CC=1 SCAR=6.9
165 0 $mech->dump_text();
166 0 return;
167 }
168  
169 __END__
« blib/lib/WWW/Mechanize.pm ↑ Summary blib/lib/WWW/Mechanize/Image.pm »