File Coverage

script/mech-dump
Criterion Covered Total %
statement 65 101 64.3
branch 19 40 47.5
condition 0 3 0.0
subroutine 12 21 57.1
pod n/a
total 96 165 58.1


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