blib/lib/WWW/Mechanize/Shell.pm

filter: Statementstmt 75.8% 545 / 719 Branchbran 59.2% 179 / 302 Conditioncond 46.6% 42 / 90 MC/DCmcdc 30.0% 15 / 50 Subroutinesub 89.1% 90 / 101 Podpod 17.8% 13 / 73 totaltotal 66.2% 884 / 1335 CC 369 SCAR 25.3
CC
369
Cov
69%
init
72.7
init
72.7
print_paged
61.2
CRAP 12.6
↑ Summary
1 package WWW::Mechanize::Shell;
2  
3 33 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 32 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 32 ▶use WWW::Mechanize;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 32 ▶use WWW::Mechanize::FormFiller;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 32 ▶use HTTP::Cookies;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 33 ▶use parent qw( Term::Shell );
Subroutine BEGIN: called CC=1 SCAR=0.0
9 33 ▶use Exporter 'import';
Subroutine BEGIN: called CC=1 SCAR=0.0
10 32 ▶use FindBin;
Subroutine BEGIN: called CC=1 SCAR=0.0
11 32 ▶use File::Temp qw(tempfile);
Subroutine BEGIN: called CC=1 SCAR=0.0
12 32 ▶use URI::URL;
Subroutine BEGIN: called CC=1 SCAR=0.0
13 32 ▶use Hook::LexWrap;
Subroutine BEGIN: called CC=1 SCAR=0.0
14 32 ▶use HTML::Display qw();
Subroutine BEGIN: called CC=1 SCAR=0.0
15 32 ▶use HTML::TokeParser::Simple;
Subroutine BEGIN: called CC=1 SCAR=0.0
16 32 ▶use B::Deparse;
Subroutine BEGIN: called CC=1 SCAR=0.0
17  
18 32 our $VERSION = '0.63';
19 32 our @EXPORT = qw( &shell );
20  
21 =head1 NAME
22  
23 WWW::Mechanize::Shell - An interactive shell for WWW::Mechanize
24  
25 =head1 SYNOPSIS
26  
27 From the command line as
28  
29 perl -MWWW::Mechanize::Shell -eshell
30  
31 or alternatively as a custom shell program via :
32  
33 =for example begin
34  
35 #!/usr/bin/perl -w
36 use strict;
37 use WWW::Mechanize::Shell;
38  
39 my $shell = WWW::Mechanize::Shell->new("shell");
40  
41 if (@ARGV) {
42 $shell->source_file( @ARGV );
43 } else {
44 $shell->cmdloop;
45 };
46  
47 =for example end
48  
49 =for example_testing
50 BEGIN {
51 require WWW::Mechanize::Shell;
52 $ENV{PERL_RL} = 0;
53 $ENV{COLUMNS} = '80';
54 $ENV{LINES} = '24';
55 };
56 BEGIN {
57 no warnings 'once';
58 no warnings 'redefine';
59 *WWW::Mechanize::Shell::cmdloop = sub {};
60 *WWW::Mechanize::Shell::display_user_warning = sub {};
61 *WWW::Mechanize::Shell::source_file = sub {};
62 };
63 isa_ok( $shell, "WWW::Mechanize::Shell" );
64  
65 =head1 DESCRIPTION
66  
67 This module implements a www-like shell above WWW::Mechanize
68 and also has the capability to output crude Perl code that recreates
69 the recorded session. Its main use is as an interactive starting point
70 for automating a session through WWW::Mechanize.
71  
72 The cookie support is there, but no cookies are read from your existing
73 browser sessions. See L<HTTP::Cookies> on how to implement reading/writing
74 your current browsers cookies.
75  
76 =head2 C<WWW::Mechanize::Shell-E<gt>new %ARGS>
77  
78 This is the constructor for a new shell instance. Some of the options
79 can be passed to the constructor as parameters.
80  
81 By default, a file C<.mechanizerc> (respectively C<mechanizerc> under Windows)
82 in the users home directory is executed before the interactive shell loop is
83 entered. This can be used to set some defaults. If you want to supply a different
84 filename for the rcfile, the C<rcfile> parameter can be passed to the constructor :
85  
86 rcfile => '.myapprc',
87  
88 =over 4
89  
90 =item B<agent>
91  
92 my $shell = WWW::Mechanize::Shell->new(
93 agent => WWW::Mechanize::Chrome->new(),
94 );
95  
96 Pass in a premade custom user agent. This object must be compatible to
97 L<WWW::Mechanize>. Use this feature from the command line as
98  
99 perl -Ilib -MWWW::Mechanize::Chrome \
100 -MWWW::Mechanize::Shell \
101 -e"shell(agent => WWW::Mechanize::Chrome->new())"
102  
103 =back
104  
105 =cut
106  
107 85 ▶sub init {
Subroutine init: called CC=17 SCAR=42.9
Pod undocumented
108 0 ▶ my ($self) = @_;
Pod undocumented
109 0 my ($name,%args) = @{$self->{API}{args}};
110  
111 0 $self->{agent} = $args{ agent };
112 0 ▶ if( ! $self->agent ) {
Branch: 2 branches
BranchTrueFalseTotal
unless ($self->agent) 84 0 84
unless ($self->agent) 0 0 0
113 0 ▶ my $class = $args{ agent_class } || 'WWW::Mechanize';
Condition25%1/4
$args{'agent_class'} || 'WWW::Mechanize'
l!l
084
$args{'agent_class'} || 'WWW::Mechanize'
l!l
00
MC/DC0%0/1
$args{'agent_class'} || 'WWW::Mechanize'
$args{'agent_class'}
MC/DC0%0/1
$args{'agent_class'} || 'WWW::Mechanize'
$args{'agent_class'}
Truth table50%1/2
A
A= $args{'agent_class'}
Aresult
11
00
Truth table0%0/2
A
A= $args{'agent_class'}
Aresult
11
00
114 0 ▶ my $args = $args{ agent_args } || [];
Condition25%1/4
$args{'agent_args'} || []
l!l
084
$args{'agent_args'} || []
l!l
00
MC/DC0%0/1
$args{'agent_args'} || []
$args{'agent_args'}
MC/DC0%0/1
$args{'agent_args'} || []
$args{'agent_args'}
Truth table50%1/2
A
A= $args{'agent_args'}
Aresult
11
00
Truth table0%0/2
A
A= $args{'agent_args'}
Aresult
11
00
115 0 $self->{agent} = $class->new( @$args );
116 };
117  
118 0 $self->{formfiller} = WWW::Mechanize::FormFiller->new(default => [ Ask => $self ]);
119  
120 0 $self->{history} = [];
121  
122 $self->{options} = {
123 autosync => 0,
124 warnings => (exists $args{warnings} ? $args{warnings} : 1),
125 autorestart => 0,
126 0 ▶ watchfiles => (exists $args{watchfiles} ? $args{watchfiles} : 1),
Branch: 4 branches
BranchTrueFalseTotal
exists $args{'warnings'} ? : 83 1 84
exists $args{'watchfiles'} ? : 0 85 85
exists $args{'warnings'} ? : 0 0 0
exists $args{'watchfiles'} ? : 0 0 0
127 cookiefile => 'cookies.txt',
128 dumprequests => 0,
129 dumpresponses => 0,
130 verbose => 0,
131 };
132 # Install the request dumper :
133 $self->{request_wrapper} = wrap 'LWP::UserAgent::request',
134 #pre => sub { printf STDERR "Dumping? %s\n",$self->option("dumprequests"); $self->request_dumper($_[1]) if $self->option("dumprequests"); },
135 145 ▶ pre => sub { $self->request_dumper($_[1]) if $self->option("dumprequests"); },
Subroutine __ANON__: called CC=2 SCAR=6.9
Branch: 1 branch
BranchTrueFalseTotal
if $self->option("dumprequests") 82 63 145
136 145 ▶ post => sub {
Subroutine __ANON__: called CC=2 SCAR=6.9
137 144 ▶ $self->response_dumper($_[-1]) if $self->option("dumpresponses");
Branch: 1 branch
BranchTrueFalseTotal
if $self->option("dumpresponses") 1 143 144
138 0 };
139  
140 $self->{redirect_ok_wrapper} = wrap 'WWW::Mechanize::redirect_ok',
141 2 ▶ post => sub {
Subroutine __ANON__: called CC=2 SCAR=10.5
142 1 ▶ return unless $_[1];
Branch: 1 branch
BranchTrueFalseTotal
unless $_[1] 1 0 1
143 0 $self->status( "\nRedirecting to ".$_[1]->uri."\n" );
144 0 $_[-1]
145 0 };
146  
147 # Load the proxy settings from the environment
148 0 ▶ $self->agent->env_proxy()
Branch: 2 branches
BranchTrueFalseTotal
if $self->agent->can("env_proxy") 84 0 84
if $self->agent->can("env_proxy") 0 0 0
149 if $self->agent->can('env_proxy');
150  
151 # Read our .rc file :
152 # I could use File::Homedir, but the docs claim it dosen't work on Win32. Maybe
153 # I should just release a patch for File::Homedir then... Not now.
154 0 my $sourcefile;
155 0 ▶ if (exists $args{rcfile}) {
Branch: 2 branches
BranchTrueFalseTotal
if (exists $args{'rcfile'}) { } 81 3 84
if (exists $args{'rcfile'}) { } 0 0 0
156 0 $sourcefile = delete $args{rcfile};
157 } else {
158 0 ▶ my $userhome = $^O =~ /win32/i ? $ENV{'USERPROFILE'} || $ENV{'HOME'} : ((getpwuid($<))[7]);
Branch: 2 branches
BranchTrueFalseTotal
$^O =~ /win32/i ? : 0 3 3
$^O =~ /win32/i ? : 0 0 0
Condition0%0/6
$ENV{"USERPROFILE"} or $ENV{"HOME"}
l!l&&r!l&&!r
000
$ENV{"USERPROFILE"} or $ENV{"HOME"}
l!l&&r!l&&!r
000
MC/DC0%0/2
$ENV{"USERPROFILE"} or $ENV{"HOME"}
$ENV{"USERPROFILE"} $ENV{"HOME"}
MC/DC0%0/2
$ENV{"USERPROFILE"} or $ENV{"HOME"}
$ENV{"USERPROFILE"} $ENV{"HOME"}
Truth table0%0/3
A or B
A= $ENV{"USERPROFILE"}B= $ENV{"HOME"}
ABresult
1X1
011
000
Truth table0%0/3
A or B
A= $ENV{"USERPROFILE"}B= $ENV{"HOME"}
ABresult
1X1
011
000
159 0 ▶ $sourcefile = "$userhome/.mechanizerc"
Branch: 2 branches
BranchTrueFalseTotal
if -f "$userhome/.mechanizerc" 0 3 3
if -f "$userhome/.mechanizerc" 0 0 0
160 if -f "$userhome/.mechanizerc";
161 };
162 0 ▶ $self->option('cookiefile', $args{cookiefile}) if (exists $args{cookiefile});
Branch: 2 branches
BranchTrueFalseTotal
if exists $args{"cookiefile"} 1 83 84
if exists $args{"cookiefile"} 0 0 0
163 0 ▶ $self->source_file($sourcefile) if defined $sourcefile;
Branch: 2 branches
BranchTrueFalseTotal
if defined $sourcefile 1 83 84
if defined $sourcefile 0 0 0
164 0 $self->{browser} = undef;
165  
166 # Keep track of the files we consist of, to enable automatic reloading
167 0 $self->{files} = undef;
168 84 ▶ if ($self->option('watchfiles')) {
Branch: 2 branches
BranchTrueFalseTotal
if ($self->option("watchfiles")) 84 0 84
if ($self->option("watchfiles")) 0 0 0
169 84 eval {
170 84 ▶ my @files = grep { -f && -r && $_ ne '-e' } values %INC;
Condition16%2/12
-f $_ && -r $_ && $_ ne '-e'
!ll&&!rl&&r
0020862
-f $_ and -r $_
!ll&&!rl&&r
0020862
-f $_ && -r $_ && $_ ne '-e'
!ll&&!rl&&r
000
-f $_ and -r $_
!ll&&!rl&&r
000
MC/DC0%0/3
-f $_ && -r $_ && $_ ne '-e'
-f $_ -r $_ $_ ne '-e'
MC/DC0%0/3
-f $_ && -r $_ && $_ ne '-e'
-f $_ -r $_ $_ ne '-e'
Truth table25%1/4
A and B && C
A= -f $_B= -r $_C= $_ ne '-e'
ABCresult
0XX0
10X0
1100
1111
Truth table0%0/4
A and B && C
A= -f $_B= -r $_C= $_ ne '-e'
ABCresult
0XX0
10X0
1100
1111
171 84 local $, = ",";
172 84 require File::Modified;
173 0 $self->{files} = File::Modified->new(files=>[@files]);
174 };
175 84 ▶ $self->display_user_warning( "Module File::Modified not found. Automatic reloading disabled.\n" )
Branch: 2 branches
BranchTrueFalseTotal
if $@ 84 0 84
if $@ 0 0 0
176 if ($@);
177 };
178 };
179  
180 =head2 C<$shell-E<gt>release_agent>
181  
182 Since the shell stores a reference back to itself within the
183 WWW::Mechanize instance, it is necessary to break this
184 circular reference. This method does this.
185  
186 =cut
187  
188 46 ▶sub release_agent {
Subroutine release_agent: called CC=1 SCAR=0.0
Pod documented
189 45 my ($self) = @_;
190 32 ▶ use Data::Dumper;
Subroutine BEGIN: called CC=1 SCAR=0.0
191 45 warn Dumper $self;
192 45 undef $self->{request_wrapper};
193 45 undef $self->{redirect_ok_wrapper};
194 45 $self->{agent} = undef;
195 };
196  
197 =head2 C<$shell-E<gt>source_file FILENAME>
198  
199 The C<source_file> method executes the lines of FILENAME
200 as if they were typed in.
201  
202 $shell->source_file( $filename );
203  
204 =cut
205  
206 4 ▶sub source_file {
Subroutine source_file: called CC=4 SCAR=13.9
Pod documented
207 3 my ($self,$filename) = @_;
208 3 local $_; # just to be on the safe side that we don't clobber outside users of $_
209 3 local *F;
210 3 ▶ open F, "< $filename"
Branch: 1 branch
BranchTrueFalseTotal
open F, "< $filename" or die "Couldn't open '${filename}' : $!\n" 1 2 3
211 or die "Couldn't open '$filename' : $!\n";
212 1 ▶ while (<F>) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined($_ = readline F)) 2 1 3
213 2 $self->cmd($_);
214 warn "cmd: $_"
215 2 ▶ if $self->{options}->{verbose};
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"options"}{"verbose"} 0 2 2
216 };
217 1 close F;
218 };
219  
220 281 ▶sub add_history {
Subroutine add_history: called CC=1 SCAR=0.0
Pod undocumented
221 280 my ($self,@code) = @_;
222 280 push @{$self->{history}},[$self->line,join "",@code];
223 };
224  
225 =head2 C<$shell-E<gt>display_user_warning>
226  
227 All user warnings are routed through this routine
228 so they can be rerouted / disabled easily.
229  
230 =cut
231  
232 88 ▶sub display_user_warning {
Subroutine display_user_warning: called CC=2 SCAR=6.9
Pod documented
233 87 my ($self,@message) = @_;
234  
235 87 ▶ warn @message
Branch: 1 branch
BranchTrueFalseTotal
if $self->option("warnings") 1 86 87
236 if $self->option('warnings');
237 };
238  
239 =head2 C<$shell-E<gt>print_paged LIST>
240  
241 Prints the text in LIST using C<$ENV{PAGER}>. If C<$ENV{PAGER}>
242 is empty, prints directly to C<STDOUT>. Most of this routine
243 comes from the C<perldoc> utility.
244  
245 =cut
246  
247 32 ▶sub print_paged {
Subroutine print_paged: called CC=10 SCAR=41.1
Pod documented
248 32 my $self = shift;
249  
250 32 ▶ if ($ENV{PAGER} and -t STDOUT) {
Branch: 1 branch
BranchTrueFalseTotal
if ($ENV{'PAGER'} and -t STDOUT) { } 0 32 32
Condition33%1/3
$ENV{"PAGER"} and -t STDOUT
!ll&&!rl&&r
3200
MC/DC0%0/2
$ENV{"PAGER"} and -t STDOUT
$ENV{"PAGER"} -t STDOUT
Truth table33%1/3
A and B
A= $ENV{"PAGER"}B= -t STDOUT
ABresult
0X0
100
111
251 0 my ($fh,$filename) = tempfile();
252 0 print $fh $_ for @_;
253 0 close $fh;
254  
255 0 my @pagers = ($ENV{PAGER},qq{"$^X" -p});
256 0 foreach my $pager (@pagers) {
257 0 ▶ if ($^O eq 'VMS') {
Branch: 1 branch
BranchTrueFalseTotal
if ($^O eq 'VMS') { } 0 0 0
258 0 ▶ last if system("$pager $filename") == 0; # quoting prevents logical expansion
Branch: 1 branch
BranchTrueFalseTotal
if system("$pager $filename") == 0 0 0 0
259 } else {
260 0 ▶ last if system(qq{$pager "$filename"}) == 0;
Branch: 1 branch
BranchTrueFalseTotal
if system(qq[$pager "$filename"]) == 0 0 0 0
261 }
262 };
263 0 ▶ unlink $filename
Branch: 1 branch
BranchTrueFalseTotal
unlink $filename or $self->display_user_warning("Couldn't unlink tempfile $filename : $!\n") 0 0 0
264 or $self->display_user_warning("Couldn't unlink tempfile $filename : $!\n");
265 } else {
266 32 print $_ for @_;
267 };
268 };
269  
270 997 ▶sub agent { $_[0]->{agent}; };
Subroutine agent: called CC=1 SCAR=0.0
Pod documented
271  
272 674 ▶sub option {
Subroutine option: called CC=3 SCAR=11.0
Pod undocumented
273 674 my ($self,$option,$value) = @_;
274 674 ▶ if (exists $self->{options}->{$option}) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $self->{'options'}{$option}) { } 671 3 674
275 671 my $result = $self->{options}->{$option};
276 671 ▶ if (scalar @_ == 3) {
Branch: 1 branch
BranchTrueFalseTotal
if (scalar @_ == 3) 74 597 671
277 74 $self->{options}->{$option} = $value;
278 };
279 671 $result;
280 } else {
281 3 Carp::carp "Unknown option '$option'";
282 3 undef;
283 };
284 };
285  
286 0 ▶sub restart_shell {
Subroutine restart_shell: not called CC=2 SCAR=17.9
Pod undocumented
287 0 ▶ if ($0 ne '-e') {
Branch: 1 branch
BranchTrueFalseTotal
if ($0 ne "-e") 0 0 0
288 0 print "Restarting $0\n";
289 0 exec $^X, $0, @ARGV;
290 };
291 };
292  
293 341 ▶sub precmd {
Subroutine precmd: called CC=4 SCAR=19.7
Pod undocumented
294 341 my $self = shift @_;
295 # We want to restart when any module was changed
296 341 ▶ if ($self->{files} and $self->{files}->changed()) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"files"} and $self->{"files"}->changed) 0 341 341
Condition33%1/3
$self->{"files"} and $self->{"files"}->changed
!ll&&!rl&&r
34100
MC/DC0%0/2
$self->{"files"} and $self->{"files"}->changed
$self->{"files"} $self->{"files"}->changed
Truth table33%1/3
A and B
A= $self->{"files"}B= $self->{"files"}->changed
ABresult
0X0
100
111
297 0 print "One or more of the base files were changed\n";
298 0 ▶ $self->restart_shell if ($self->option('autorestart'));
Branch: 1 branch
BranchTrueFalseTotal
if $self->option("autorestart") 0 0 0
299 };
300  
301 341 $self->SUPER::precmd(@_);
302 };
303  
304 1 ▶sub browser {
Subroutine browser: called CC=2 SCAR=7.6
Pod undocumented
305 1 my ($self) = @_;
306 1 ▶ $self->{browser} ||= HTML::Display->new();
Condition33%1/3
$self->{"browser"} ||= "HTML::Display"->new
l!l&&r!l&&!r
010
MC/DC0%0/2
$self->{"browser"} ||= "HTML::Display"->new
$self->{"browser"} "HTML::Display"->new
Truth table33%1/3
A ||= B
A= $self->{"browser"}B= "HTML::Display"->new
ABresult
1X1
011
000
307 1 $self->{browser};
308 };
309  
310 2 ▶sub sync_browser {
Subroutine sync_browser: called CC=4 SCAR=25.6
Pod undocumented
311 2 my ($self) = @_;
312  
313 # We only can display html if we have any :
314 2 ▶ return unless $self->agent->res;
Branch: 1 branch
BranchTrueFalseTotal
unless $self->agent->res 2 0 2
315  
316 # Prepare the HTML for local display :
317 0 my $unclean = $self->agent->content;
318 0 my $html = '';
319  
320 # ugly fix:
321 # strip all target='_blank' attributes from the HTML:
322 0 my $p = HTML::TokeParser::Simple->new(\$unclean);
323 0 ▶ while (my $token = $p->get_token) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $token = $p->get_token) 0 0 0
324 0 ▶ $token->delete_attr('target')
Branch: 1 branch
BranchTrueFalseTotal
if $token->is_start_tag 0 0 0
325 if $token->is_start_tag;
326 0 $html .= $token->as_is;
327 };
328  
329 0 my $location = $self->agent->{uri};
330 0 my $browser = $self->browser;
331 0 $browser->display( html => $html, location => $location );
332 };
333  
334 1 ▶sub prompt_str {
Subroutine prompt_str: called CC=3 SCAR=14.2
Pod undocumented
335 1 my $self = shift;
336 1 ▶ if ($self->agent->response) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->agent->response) { } 0 1 1
337 0 ▶ return ($self->agent->uri || "") . ">"
Condition0%0/2
$self->agent->uri || ''
l!l
00
MC/DC0%0/1
$self->agent->uri || ''
$self->agent->uri
Truth table0%0/2
A
A= $self->agent->uri
Aresult
11
00
338 } else {
339 1 return "(no url)>"
340 };
341 };
342  
343 0 ▶sub request_dumper { print $_[1]->as_string };
Subroutine request_dumper: not called CC=1 SCAR=6.9
Pod undocumented
344 1 ▶sub response_dumper {
Subroutine response_dumper: called CC=2 SCAR=8.1
Pod undocumented
345 1 ▶ if (ref $_[1] eq 'ARRAY') {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $_[1] eq 'ARRAY') { } 1 0 1
346 1 print $_[1]->[0]->as_string;
347 } else {
348 0 print $_[1]->as_string;
349 }
350 };
351  
352 62 ▶sub re_or_string {
Subroutine re_or_string: called CC=2 SCAR=6.9
Pod undocumented
353 62 my ($self,$arg) = @_;
354 62 ▶ if ($arg =~ m!^/(.*)/([imsx]*)$!) {
Branch: 1 branch
BranchTrueFalseTotal
if ($arg =~ m[^/(.*)/([imsx]*)$]) 11 51 62
355 11 my ($re,$mode) = ($1,$2);
356 11 $re =~ s!([^\\])/!$1\\/!g;
357 11 $arg = eval "qr/$re/$mode";
358 };
359 62 $arg;
360 };
361  
362 =head2 C<< $shell->link_text LINK >>
363  
364 Returns a meaningful text from a WWW::Mechanize::Link object. This is (in order of
365 precedence) :
366  
367 $link->text
368 $link->name
369 $link->url
370  
371 =cut
372  
373 2 ▶sub link_text {
Subroutine link_text: called CC=3 SCAR=11.1
Pod documented
374 2 my ($self,$link) = @_;
375 2 my $result;
376 2 for (qw( text name url )) {
377 2 ▶ $result = $link->$_ and last;
Branch: 1 branch
BranchTrueFalseTotal
$result = $link->$_ and last 2 0 2
378 };
379 2 $result;
380 };
381  
382 =head2 C<$shell-E<gt>history>
383  
384 Returns the (relevant) shell history, that is, all commands
385 that were not solely for the information of the user. The
386 lines are returned as a list.
387  
388 print join "\n", $shell->history;
389  
390 =cut
391  
392 3 ▶sub history {
Subroutine history: called CC=1 SCAR=0.0
Pod documented
393 3 my ($self) = @_;
394 3 map { $_->[0] } @{$self->{history}}
395 };
396  
397 =head2 C<$shell-E<gt>script>
398  
399 Returns the shell history as a Perl program. The
400 lines are returned as a list. The lines do not have
401 a one-by-one correspondence to the lines in the history.
402  
403 print join "\n", $shell->script;
404  
405 =cut
406  
407 49 ▶sub script {
Subroutine script: called CC=2 SCAR=6.9
Pod documented
408 49 my ($self,$prefix) = @_;
409 49 ▶ $prefix ||= "";
Condition100%2/2
$prefix ||= ""
l!l
346
MC/DC100%1/1
$prefix ||= ""
$prefix
Truth table100%2/2
A
A= $prefix
Aresult
11
00
410  
411 49 my @result = sprintf <<'HEADER', $^X;
412 #!%s -w
413 use strict;
414 use WWW::Mechanize;
415 use WWW::Mechanize::FormFiller;
416 use URI::URL;
417  
418 my $agent = WWW::Mechanize->new( autocheck => 1 );
419 my $formfiller = WWW::Mechanize::FormFiller->new();
420 $agent->env_proxy();
421 HEADER
422  
423 49 push @result, map { my $x = $_->[1]; $x =~ s/^/$prefix/mg; $x } @{$self->{history}};
424 49 @result;
425 };
426  
427 =head2 C<$shell-E<gt>status>
428  
429 C<status> is called for status updates.
430  
431 =cut
432  
433 22 ▶sub status {
Subroutine status: called CC=1 SCAR=0.0
Pod documented
434 22 my $self = shift;
435 22 print join "", @_;
436 };
437  
438 =head2 C<$shell-E<gt>display FILENAME LINES>
439  
440 C<display> is called to output listings, currently from the
441 C<history> and C<script> commands. If the second parameter
442 is defined, it is the name of the file to be written,
443 otherwise the lines are displayed to the user.
444  
445 =cut
446  
447 36 ▶sub display {
Subroutine display: called CC=4 SCAR=13.9
Pod documented
448 36 my ($self,$filename,@lines) = @_;
449 36 ▶ if (defined $filename) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $filename) { } 4 32 36
450 4 eval {
451 4 ▶ open my $f, ">", $filename
Branch: 1 branch
BranchTrueFalseTotal
open my $f, ">", $filename or die "Couldn't create $filename : $!" 4 0 4
452 or die "Couldn't create $filename : $!";
453 4 binmode $f;
454 4 print $f join( "", map { "$_\n" } (@lines) );
455 4 close $f;
456 };
457 4 ▶ warn $@ if $@;
Branch: 1 branch
BranchTrueFalseTotal
if $@ 0 4 4
458 } else {
459 32 $self->print_paged( join( "", map { "$_\n" } (@lines) ));
460 };
461 };
462  
463 # sub-classed from Term::Shell to handle all run_ requests that have no corresponding sub
464 # This is used for comments
465 14 ▶sub catch_run {
Subroutine catch_run: called CC=2 SCAR=6.9
Pod undocumented
466 14 my ($self) = shift;
467 14 my ($command) = @_;
468 14 ▶ if ($command =~ /^\s*#/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($command =~ /^\s*#/) { } 13 1 14
469 # Hey, it's a comment.
470 } else {
471 1 print $self->msg_unknown_cmd($command);
472 };
473 };
474  
475 # sub-classed from Term::Shell to handle all smry requests
476 1 ▶sub catch_smry {
Subroutine catch_smry: called CC=2 SCAR=10.1
Pod undocumented
477 1 my ($self,$command) = @_;
478  
479 1 my $result = eval {
480 1 require Pod::Constants;
481  
482 0 my @summary;
483 0 my $module = (ref $self ).".pm";
484 0 $module =~ s!::!/!g;
485 0 $module = $INC{$module};
486  
487 0 Pod::Constants::import_from_file( $module, $command => \@summary );
488  
489 0 $summary[0];
490 };
491 1 ▶ if ($@) {
Branch: 1 branch
BranchTrueFalseTotal
if ($@) 1 0 1
492 1 return undef;
493 };
494 0 return $result;
495 };
496  
497 # sub-classed from Term::Shell to handle all help requests
498 0 ▶sub catch_help {
Subroutine catch_help: not called CC=2 SCAR=17.9
Pod undocumented
499 0 my ($self,$command) = @_;
500  
501 0 my @result = eval {
502 0 require Pod::Constants;
503  
504 0 my @summary;
505 0 my $module = (ref $self ).".pm";
506 0 $module =~ s!::!/!g;
507 0 $module = $INC{$module};
508  
509 0 Pod::Constants::import_from_file( $module, $command => \@summary );
510  
511 0 @summary;
512 };
513 0 ▶ if ($@) {
Branch: 1 branch
BranchTrueFalseTotal
if ($@) 0 0 0
514 0 my $module = ref $self;
515 0 $self->display_user_warning( "Pod::Constants not available. Use perldoc $module for help.\n" );
516 0 return undef;
517 };
518 0 return join( "\n", @result) . "\n";
519 };
520  
521 =head1 COMMANDS
522  
523 The shell implements various commands :
524  
525 =head2 exit
526  
527 Leaves the shell.
528  
529 =cut
530  
531 672 ▶sub alias_exit { qw(quit) };
Subroutine alias_exit: called CC=1 SCAR=0.0
Pod undocumented
532  
533 =head2 restart
534  
535 Restart the shell.
536  
537 This is mostly useful when you are modifying the shell itself. It dosen't
538 work if you use the shell in oneliner mode with C<-e>.
539  
540 =cut
541  
542 1 ▶sub run_restart {
Subroutine run_restart: called CC=1 SCAR=0.0
Pod undocumented
543 1 my ($self) = @_;
544 1 $self->restart_shell;
545 };
546  
547 73 ▶sub activate_first_form {
Subroutine activate_first_form: called CC=3 SCAR=11.1
Pod undocumented
548 $_[0]->agent->form_number(1)
549 73 ▶ if $_[0]->agent->forms and scalar @{$_[0]->agent->forms};
Branch: 1 branch
BranchTrueFalseTotal
if $_[0]->agent->forms and scalar @{$_[0]->agent->forms;} 72 1 73
Condition50%1/2
$_[0]->agent->forms and scalar @{$_[0]->agent->forms;}
!ll
073
MC/DC0%0/1
$_[0]->agent->forms and scalar @{$_[0]->agent->forms;}
$_[0]->agent->forms
Truth table50%1/2
A
A= $_[0]->agent->forms
Aresult
00
11
550 };
551  
552 =head2 get
553  
554 Download a specific URL.
555  
556 This is used as the entry point in all sessions
557  
558 Syntax:
559  
560 get URL
561  
562 =cut
563  
564 60 ▶sub run_get {
Subroutine run_get: called CC=4 SCAR=13.9
Pod undocumented
565 60 my ($self,$url) = @_;
566 60 $self->status( "Retrieving $url" );
567 60 my $code;
568 60 eval { $self->agent->get($url) };
569 60 ▶ if ($@) {
Branch: 1 branch
BranchTrueFalseTotal
if ($@) { } 2 58 60
570 2 ▶ print "\n$@\n" if $@;
Branch: 1 branch
BranchTrueFalseTotal
if $@ 2 0 2
571 2 $self->agent->back;
572 } else {
573 58 $code = $self->agent->res->code;
574 58 $self->status( "($code)\n" );
575 };
576  
577 59 $self->activate_first_form;
578 59 ▶ $self->sync_browser if $self->option('autosync');
Branch: 1 branch
BranchTrueFalseTotal
if $self->option("autosync") 7 52 59
579 59 $self->add_history( sprintf q{$agent->get('%s');}."\n".q{ $agent->form_number(1) if $agent->forms and scalar @{$agent->forms};}, $url);
580 };
581  
582 =head2 save
583  
584 Download a link into a file.
585  
586 If more than one link matches the RE, all matching links are
587 saved. The filename is taken from the last part of the
588 URL. Alternatively, the number of a link may also be given.
589  
590 Syntax:
591  
592 save RE
593  
594 =cut
595  
596 3 ▶sub run_save {
Subroutine run_save: called CC=11 SCAR=24.2
Pod undocumented
597 3 my ($self,$user_link) = @_;
598  
599 3 ▶ unless (defined $user_link) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $user_link) 1 2 3
600 1 print "No link given to save\n";
601 return
602 1 };
603 2 my @history;
604  
605 2 my @links = ();
606 2 my @all_links = $self->agent->links;
607 2 push @history, q{my @links;} . "\n";
608 2 push @history, q{my @all_links = $agent->links();} . "\n";
609  
610 2 $user_link = $self->re_or_string($user_link);
611  
612 2 ▶ if (ref $user_link) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $user_link) { } 2 0 2
613 2 my $count = -1;
614 2 my $re = $user_link;
615 2 ▶ @links = map { $count++; ((defined $_->text && $_->text =~ /$re/)||(defined $_->url && $_->url =~ /$re/)) ? $count : () } @all_links;
Branch: 1 branch
BranchTrueFalseTotal
defined $_->text && $_->text =~ /$re/ || defined $_->url && $_->url =~ /$re/ ? : 3 17 20
Condition66%6/9
defined $_->text and $_->text =~ /$re/ or defined $_->url and $_->url =~ /$re/
l!l&&r!l&&!r
3017
defined $_->text and $_->text =~ /$re/
!ll&&!rl&&r
2153
defined $_->url and $_->url =~ /$re/
!ll&&!rl&&r
0170
MC/DC50%2/4
defined $_->text and $_->text =~ /$re/ or defined $_->url and $_->url =~ /$re/
defined $_->text $_->text =~ /$re/ defined $_->url $_->url =~ /$re/
Truth table42%3/7
A and B or C and D
A= defined $_->textB= $_->text =~ /$re/C= defined $_->urlD= $_->url =~ /$re/
ABCDresult
11XX1
0X111
10111
0X0X0
0X100
100X0
10100
616 2 ▶ if (@links == 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (@links == 0) 1 1 2
617 1 print "No match for /$re/.\n";
618 };
619 2 push @history, q{my $count = -1;} . "\n";
620 2 push @history, sprintf q{@links = map { $count++; ((defined $_->text && $_->text =~ qr(%s))||(defined $_->url && $_->url =~ qr(%s))) ? $count : () } @all_links;} . "\n", $re, $re;
621 } else {
622 0 @links = $user_link;
623 0 push @history, sprintf q{@links = '%s';} . "\n", $user_link;
624 };
625  
626 2 ▶ if (@links) {
Branch: 1 branch
BranchTrueFalseTotal
if (@links) 1 1 2
627 1 $self->add_history( @history,<<'CODE' );
628 my $base = $agent->uri;
629 for my $link (@links) {
630 my $target = $all_links[$link]->url;
631 my $url = URI::URL->new($target,$base);
632 $target = $url->path;
633 $target =~ s!^(.*/)?([^/]+)$!$2!;
634 $url = $url->abs;
635  
636 # use this line instead of the next in case you want to use smart mirroring
637 #$agent->mirror($url,$target);
638 $agent->get( $url, ':content_file' => $target );
639 };
640 CODE
641 1 my $base = $self->agent->uri;
642 1 for my $link (@links) {
643 3 my $target = $all_links[$link]->url;
644 3 my $url = URI::URL->new($target,$base);
645 3 $target = $url->path;
646 3 $target =~ s!^(.*/)?([^/]+)$!$2!;
647 3 $url = $url->abs;
648 3 eval {
649 3 $self->status( "$url => $target" );
650 3 $self->agent->get( $url, ':content_file' => $target );
651 };
652  
653 3 ▶ warn $@ if $@;
Branch: 1 branch
BranchTrueFalseTotal
if $@ 0 3 3
654 };
655 }
656 };
657  
658 =head2 content
659  
660 Display the content for the current page.
661  
662 Syntax: content [FILENAME]
663  
664 If the FILENAME argument is provided, save the content to the file.
665  
666 A trailing "\n" is added to the end of the content when using the
667 shell, so this might not be ideally suited to save binary files without
668 manual editing of the produced script.
669  
670 =cut
671  
672 32 ▶sub run_content {
Subroutine run_content: called CC=2 SCAR=6.9
Pod undocumented
673 32 my ($self, $filename) = @_;
674 32 $self->display($filename, $self->agent->content);
675 32 ▶ if ($filename) {
Branch: 1 branch
BranchTrueFalseTotal
if ($filename) { } 2 30 32
676 2 $self->add_history( sprintf '{ my $filename = q{%s};
677 local *F;
678 open F, "> $filename" or die "$filename: $!";
679 binmode F;
680 print F $agent->content,"\n";
681 close F
682 };', $filename );
683 } else {
684 30 $self->add_history('print $agent->content,"\n";');
685 };
686 };
687  
688 =head2 title
689  
690 Display the current page title as found
691 in the C<< <TITLE> >> tag.
692  
693 =cut
694  
695 5 ▶sub run_title {
Subroutine run_title: called CC=3 SCAR=11.0
Pod undocumented
696 5 my ($self) = @_;
697 5 my $title = $self->agent->title;
698 5 ▶ if (! defined $title) {
Branch: 2 branches
BranchTrueFalseTotal
if (not defined $title) { } 2 3 5
elsif ($title eq '') { } 1 2 3
699 2 $title = "<missing title>"
700 } elsif ($title eq '') {
701 1 $title = "<empty title>"
702 };
703 5 print "$title\n";
704 };
705  
706 =head2 headers
707  
708 Prints all C<< <H1> >> through C<< <H5> >> strings found in the content,
709 indented accordingly. With an argument, prints only those
710 levels; e.g., C<headers 145> prints H1,H4,H5 strings only.
711  
712 =cut
713  
714 11 ▶sub run_headers {
Subroutine run_headers: called CC=12 SCAR=24.9
Pod undocumented
715 11 my ($self,$headers) = @_;
716 11 ▶ $headers ||= "12345";
Condition100%2/2
$headers ||= 12345
l!l
92
MC/DC100%1/1
$headers ||= 12345
$headers
Truth table100%2/2
A
A= $headers
Aresult
11
00
717  
718 11 my $content = $self->agent->content;
719  
720 # Convert the $headers argument to a RE matching
721 # the header tags:
722 11 my $wanted = join "|", map { "H$_" } split //, $headers;
723 11 $wanted = qr/^$wanted$/i;
724 #warn $wanted;
725  
726 11 my $p = HTML::TokeParser::Simple->new( \$content );
727 11 ▶ while ( my $token = $p->get_token ) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $token = $p->get_token) 326 11 337
728 # This prints all text in an HTML doc (i.e., it strips the HTML)
729 326 ▶ if ($token->is_start_tag($wanted)) {
Branch: 1 branch
BranchTrueFalseTotal
if ($token->is_start_tag($wanted)) 40 286 326
730 40 my $tag = $token->get_tag;
731  
732 # Indent with two spaces per level
733 40 my $indent;
734 40 ▶ $indent = $1
Branch: 1 branch
BranchTrueFalseTotal
if $tag =~ /(\d)/ 40 0 40
735 if ($tag =~ /(\d)/);
736 40 ▶ $indent ||= 1;
Condition50%1/2
$indent ||= 1
l!l
400
MC/DC0%0/1
$indent ||= 1
$indent
Truth table50%1/2
A
A= $indent
Aresult
11
00
737 40 $indent--;
738 40 $indent *= 2;
739  
740 # advance and print the first text tag we encounter
741 40 ▶ while ($token and not $token->is_text and not $token->is_end_tag($wanted)) {
Branch: 1 branch
BranchTrueFalseTotal
if ($token and not $token->is_text and not $token->is_end_tag($wanted)) 40 40 80
Condition83%5/6
$token and not $token->is_text and not $token->is_end_tag($wanted)
!ll&&!rl&&r
37340
$token and not $token->is_text
!ll&&!rl&&r
03743
MC/DC66%2/3
$token and not $token->is_text and not $token->is_end_tag($wanted)
$token not $token->is_text not $token->is_end_tag($wanted)
Truth table75%3/4
A and not(B) and not(C)
A= $tokenB= not $token->is_textC= not $token->is_end_tag($wanted)
ABCresult
0XX0
10X0
1100
1111
742 40 $token = $p->get_token
743 };
744 40 my $text = "<no text>";
745 40 ▶ if ($token and $token->is_text) {
Branch: 1 branch
BranchTrueFalseTotal
if ($token and $token->is_text) 37 3 40
Condition66%2/3
$token and $token->is_text
!ll&&!rl&&r
0337
MC/DC50%1/2
$token and $token->is_text
$token $token->is_text
Truth table66%2/3
A and B
A= $tokenB= $token->is_text
ABresult
0X0
100
111
746 37 $text = $token->as_is;
747 37 ▶ if ($text !~ /\S/) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($text =~ /\S/) 8 29 37
748 8 $text = "<empty tag>";
749 };
750 };
751  
752 # Clean up whitespace
753 40 $text =~ s/^\s+//g;
754 40 $text =~ s/\s+$//g;
755 40 $text =~ s/\s+/ /g;
756  
757 40 printf "%s:%${indent}s%s\n", $tag, "", $text;
758 };
759 }
760 };
761  
762 =head2 ua
763  
764 Get/set the current user agent
765  
766 Syntax:
767  
768 # fake Internet Explorer
769 ua "Mozilla/4.0 (compatible; MSIE 4.01; Windows 98)"
770  
771 # fake QuickTime v5
772 ua "QuickTime (qtver=5.0.2;os=Windows NT 5.0Service Pack 2)"
773  
774 # fake Mozilla/Gecko based
775 ua "Mozilla/5.001 (windows; U; NT4.0; en-us) Gecko/25250101"
776  
777 # set empty user agent :
778 ua ""
779  
780 =cut
781  
782 2 ▶sub run_ua {
Subroutine run_ua: called CC=2 SCAR=7.2
Pod undocumented
783 2 my ($self,$ua) = @_;
784 2 my ($result) = $self->agent->agent;
785 2 ▶ if (scalar @_ == 2) {
Branch: 1 branch
BranchTrueFalseTotal
if (scalar @_ == 2) { } 2 0 2
786 2 $self->agent->agent($ua);
787 2 $self->add_history( sprintf q{$agent->agent('%s');}, $ua);
788 } else {
789 0 print "Current user agent: $result\n";
790 };
791 };
792  
793 =head2 links
794  
795 Display all links on a page
796  
797 The links numbers displayed can used by C<open> to directly
798 select a link to follow.
799  
800 =cut
801  
802 1 ▶sub run_links {
Subroutine run_links: called CC=2 SCAR=7.1
Pod undocumented
803 1 my ($self) = @_;
804 1 my @links = $self->agent->links;
805 1 my $count = 0;
806 1 for my $link (@links) {
807 # print "[", $count++, "] ", $link->[1],"\n";
808 0 print sprintf "[%s] %s\n", $count++, $self->link_text($link);
809 };
810 };
811  
812 =head2 images
813  
814 Display images on a page
815  
816 =cut
817  
818 0 ▶sub run_images {
Subroutine run_images: not called CC=2 SCAR=17.9
Pod undocumented
819 0 my ($self) = @_;
820  
821 0 my @images = $self->agent->images;
822 0 my $count = 0;
823  
824 0 for my $image ( @images ) {
825 0 print sprintf("[%d] \"%s\" %s\n", $count++, $image->alt, $image->url);
826 }
827 }
828  
829 =head2 parse
830  
831 Dump the output of HTML::TokeParser of the current content
832  
833 =cut
834  
835 1 ▶sub run_parse {
Subroutine run_parse: called CC=2 SCAR=7.4
Pod undocumented
836 1 my ($self) = @_;
837 1 my $content = $self->agent->content;
838 1 my $p = HTML::TokeParser->new(\$content);
839  
840 #$p->report_tags(qw(form input textarea select optgroup option));
841  
842 1 ▶ while (my $token = $p->get_token()) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $token = $p->get_token) 0 1 1
843 #while (my $token = $p->get_tag("frame")) {
844 # print "<",$token->[0],":",ref $token->[1] ? $token->[1]->{src} : "",">";
845 0 print "<",$token->[0],":",$token->[1],">";
846 }
847 };
848  
849 =head2 forms
850  
851 Display all forms on the current page.
852  
853 =cut
854  
855 1 ▶sub run_forms {
Subroutine run_forms: called CC=3 SCAR=12.3
Pod undocumented
856 1 my ($self,$number) = @_;
857  
858 1 my $count = 1;
859 1 my $agent = $self->agent;
860 1 my @forms = $agent->forms;
861 1 ▶ if (@forms) {
Branch: 1 branch
BranchTrueFalseTotal
if (@forms) { } 0 1 1
862 0 for (@forms) {
863 0 print "Form [",$count++,"]\n";
864 0 $_->dump;
865 };
866 } else {
867 1 print "No forms on current page.\n";
868 };
869 };
870  
871 =head2 form
872  
873 Select the form named NAME
874  
875 If NAME matches C</^\d+$/>, it is assumed to be the (1-based) index
876 of the form to select. There is no way of selecting a numerically
877 named form by its name.
878  
879 =cut
880  
881 4 ▶sub run_form {
Subroutine run_form: called CC=8 SCAR=24.0
Pod undocumented
882 4 my ($self,$name) = @_;
883 4 my $number;
884  
885 4 ▶ unless ($self->agent->current_form) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->agent->current_form) 1 3 4
886 1 print "There is no form on this page.\n";
887 1 return;
888 };
889  
890 3 ▶ if ($name) {
Branch: 1 branch
BranchTrueFalseTotal
if ($name) { } 3 0 3
891 3 my ($method,$val);
892 3 $val = $name;
893 3 ▶ if ($name =~ /^\d+$/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($name =~ /^\d+$/) { } 2 1 3
894 2 $method = 'form_number';
895 } else {
896 1 $method = 'form_name';
897 1 $val = qq{'$name'};
898 };
899 3 eval {
900 3 $self->agent->$method($name);
901 3 $self->add_history(sprintf q{$agent->%s(%s);}, $method, $val);
902 3 $self->status($self->agent->current_form->dump);
903 };
904 3 ▶ $self->display_user_warning( $@ )
Branch: 1 branch
BranchTrueFalseTotal
if $@ 0 3 3
905 if ($@);
906 } else {
907 0 my $count = 1;
908 0 my @forms = $self->agent->forms;
909 0 ▶ if (@forms) {
Branch: 1 branch
BranchTrueFalseTotal
if (@forms) { } 0 0 0
910 0 for my $form (@forms) {
911 0 ▶ print sprintf "Form [%s] (%s)\n", $count++, ($form->attr('name') || "<no name>");
Condition0%0/2
$form->attr('name') || '<no name>'
l!l
00
MC/DC0%0/1
$form->attr('name') || '<no name>'
$form->attr('name')
Truth table0%0/2
A
A= $form->attr('name')
Aresult
11
00
912 0 $form->dump;
913 };
914 } else {
915 0 print "No forms found on the current page.\n";
916 };
917 };
918 };
919  
920 =head2 dump
921  
922 Dump the values of the current form
923  
924 =cut
925  
926 1 ▶sub run_dump {
Subroutine run_dump: called CC=3 SCAR=12.0
Pod undocumented
927 1 my ($self) = @_;
928 1 my $form = $self->agent->current_form;
929 1 ▶ if ($form) {
Branch: 1 branch
BranchTrueFalseTotal
if ($form) { } 0 1 1
930 0 $form->dump
931 } else {
932 1 ▶ warn "There is no form on the current page\n"
Branch: 1 branch
BranchTrueFalseTotal
if $self->option("warnings") 0 1 1
933 if $self->option('warnings');
934 };
935 };
936  
937 =head2 value
938  
939 Set a form value
940  
941 Syntax:
942  
943 value NAME [VALUE]
944  
945 =cut
946  
947 7 ▶sub run_value {
Subroutine run_value: called CC=2 SCAR=7.0
Pod undocumented
948 7 my ($self,$key,@values) = @_;
949  
950 # dwim on @values
951 7 my $value = join " ", @values;
952  
953 # Look if we are filling a checkbox set:
954 #my $field = $self->agent->current_form->find_input($key);
955 #if ($field and ($field->type eq 'checkbox')) {
956 # # We want to explicitly multiple checkboxes. This means we
957 # # have to clear all checkboxes and then set them explicitly.
958 #
959 # for my $value (@values) {
960 # # Blatantly stolen from WWW::Mechanize::Ticky by
961 # # Mark Fowler E<lt>mark@twoshortplanks.comE<gt>
962 # my $input;
963 # my $index = 0;
964 # INPUT: while($input = $self->agent->current_form->find_input($name,"checkbox",$index)) {
965 # # can't guarentee that the first element will be undef and the second
966 # # element will be the right name
967 # foreach my $val ($input->possible_values()) {
968 # next unless defined $val;
969 # if ($val eq $value) {
970 # $input->value($set ? $value : undef);
971 # last INPUT;
972 # }
973 # }
974 #
975 # # move onto the next input
976 # $index++;
977 # }
978 # };
979 #};
980  
981 7 eval {
982 7 local $^W;
983 7 $self->agent->current_form->value($key,$value);
984 # Hmm - neither $key nor $value may contain backslashes nor single quotes ...
985 7 $self->add_history( sprintf q{{ local $^W; $agent->current_form->value('%s', '%s'); };}, $key, $value);
986 };
987 7 ▶ warn $@ if $@;
Branch: 1 branch
BranchTrueFalseTotal
if $@ 0 7 7
988 };
989  
990 =head2 tick
991  
992 Set checkbox marks
993  
994 Syntax:
995  
996 tick NAME VALUE(s)
997  
998 If no value is given, all boxes are checked.
999  
1000 =cut
1001  
1002 2 ▶sub tick {
Subroutine tick: called CC=3 SCAR=11.0
Pod documented
1003 2 my ($self,$tick,$key,@values) = @_;
1004 2 eval {
1005 2 local $^W;
1006 2 for my $value (@values) {
1007 2 $self->agent->$tick($key,$value);
1008 };
1009 # Hmm - neither $key nor $value may contain backslashes nor single quotes ...
1010 2 my $value_str = join ", ", map {qq{'$_'}} @values;
1011 2 $self->add_history( sprintf q{{ local $^W; for (%s) { $agent->%s('%s', $_); };}}, $value_str, $tick, $key);
1012 };
1013 2 ▶ warn $@ if $@;
Branch: 1 branch
BranchTrueFalseTotal
if $@ 0 2 2
1014 };
1015  
1016 2 ▶sub tick_all {
Subroutine tick_all: called CC=3 SCAR=11.0
Pod undocumented
1017 2 my ($self,$tick,$name) = @_;
1018 2 eval {
1019 2 local $^W;
1020 2 my $index = 1;
1021 2 ▶ while(my $input = $self->agent->current_form->find_input($name,'checkbox',$index)) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $input = $self->agent->current_form->find_input($name, "checkbox", $index)) 6 2 8
1022 6 my $value = (grep { defined $_ } ($input->possible_values()))[0];
1023 6 $self->agent->$tick($name,$value);
1024 6 $index++;
1025 };
1026 2 $self->add_history( sprintf q{
1027 { local $^W; my $index = 1;
1028 while(my $input = $agent->current_form->find_input('%s','checkbox',$index)) {
1029 my $value = (grep { defined $_ } ($input->possible_values()))[0];
1030 $agent->%s('%s',$value);
1031 $index++;
1032 };
1033 }}, $name, $tick, $name);
1034 };
1035 2 ▶ warn $@ if $@;
Branch: 1 branch
BranchTrueFalseTotal
if $@ 0 2 2
1036 };
1037  
1038 2 ▶sub run_tick {
Subroutine run_tick: called CC=2 SCAR=6.9
Pod undocumented
1039 2 my ($self,$key,@values) = @_;
1040 2 ▶ if (scalar @values) {
Branch: 1 branch
BranchTrueFalseTotal
if (scalar @values) { } 1 1 2
1041 1 $self->tick( "tick", $key, @values )
1042 } else {
1043 1 $self->tick_all( "tick", $key )
1044 };
1045 };
1046  
1047 =head2 untick
1048  
1049 Remove checkbox marks
1050  
1051 Syntax:
1052  
1053 untick NAME VALUE(s)
1054  
1055 If no value is given, all marks are removed.
1056  
1057 =cut
1058  
1059 2 ▶sub run_untick {
Subroutine run_untick: called CC=2 SCAR=6.9
Pod undocumented
1060 2 my ($self,$key,@values) = @_;
1061 2 ▶ if (scalar @values) {
Branch: 1 branch
BranchTrueFalseTotal
if (scalar @values) { } 1 1 2
1062 1 $self->tick( "untick", $key, @values )
1063 } else {
1064 1 $self->tick_all( "untick", $key )
1065 };
1066 };
1067  
1068 =head2 submit
1069  
1070 submits the form without clicking on any button
1071  
1072 =cut
1073  
1074 22 ▶sub run_submit {
Subroutine run_submit: called CC=3 SCAR=11.0
Pod undocumented
1075 22 my ($self) = @_;
1076 22 eval {
1077 22 my $res = $self->agent->submit;
1078 22 $self->status( $res->code."\n" );
1079 22 $self->add_history('$agent->submit();');
1080 22 ▶ $self->sync_browser if $self->option('autosync');
Branch: 1 branch
BranchTrueFalseTotal
if $self->option("autosync") 1 21 22
1081 };
1082 22 ▶ warn $@ if $@;
Branch: 1 branch
BranchTrueFalseTotal
if $@ 0 22 22
1083 };
1084  
1085 =head2 click
1086  
1087 Clicks on the button named NAME.
1088  
1089 No regular expression expansion is done on NAME.
1090  
1091 Syntax:
1092  
1093 click NAME
1094  
1095 If you have a button that has no name (displayed as NONAME),
1096 use
1097  
1098 click ""
1099  
1100 to click on it.
1101  
1102 =cut
1103  
1104 3 ▶sub run_click {
Subroutine run_click: called CC=4 SCAR=14.0
Pod undocumented
1105 3 my ($self,$button) = @_;
1106 3 ▶ $button ||= "";
Condition50%1/2
$button ||= ""
l!l
30
MC/DC0%0/1
$button ||= ""
$button
Truth table50%1/2
A
A= $button
Aresult
11
00
1107 3 eval {
1108 3 my $res = $self->agent->click($button);
1109 3 $self->status( "(".$res->code.")\n");
1110 3 $self->activate_first_form;
1111 3 ▶ $self->sync_browser if ($self->option('autosync'));
Branch: 1 branch
BranchTrueFalseTotal
if $self->option("autosync") 2 1 3
1112 3 $self->add_history( sprintf qq{\$agent->click('%s');}, $button );
1113 };
1114 3 ▶ warn $@ if $@;
Branch: 1 branch
BranchTrueFalseTotal
if $@ 0 3 3
1115 };
1116  
1117 =head2 open
1118  
1119 <open> accepts one argument, which can be a regular expression or the number
1120 of a link on the page, starting at zero. These numbers are displayed by the
1121 C<links> function. It goes directly to the page if a number is used
1122 or if the RE has one match. Otherwise, a list of links matching
1123 the regular expression is displayed.
1124  
1125 The regular expression should start and end with "/".
1126  
1127 Syntax:
1128  
1129 open [ RE | # ]
1130  
1131 =cut
1132  
1133 11 ▶sub run_open {
Subroutine run_open: called CC=18 SCAR=30.2
Pod undocumented
1134 11 my ($self,$user_link) = @_;
1135 11 $user_link = $self->re_or_string($user_link);
1136 11 my $link = $user_link;
1137 11 ▶ my $user_link_expr = ref $link ? qq{qr($link)} : qq{'$link'};
Branch: 1 branch
BranchTrueFalseTotal
ref $link ? : 7 4 11
1138 11 ▶ unless (defined $link) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $link) 0 11 11
1139 0 print "No link given\n";
1140 return
1141 0 };
1142  
1143 11 ▶ if ($link =~ /\D/) { # looks like a name/re
Branch: 1 branch
BranchTrueFalseTotal
if ($link =~ /\D/) 8 3 11
1144 8 ▶ my $re = $link if ref $link;
Branch: 1 branch
BranchTrueFalseTotal
if ref $link 7 1 8
1145 8 my $count = -1;
1146 8 my @possible_links = $self->agent->links();
1147 my @links = defined $re
1148 70 ▶ ? map { $count++; my $t = $_->text; defined $t && $t =~ /$re/ ? $count : () } @possible_links
Branch: 1 branch
BranchTrueFalseTotal
defined $t && $t =~ /$re/ ? : 7 63 70
Condition100%3/3
defined $t and $t =~ /$re/
!ll&&!rl&&r
7567
MC/DC100%2/2
defined $t and $t =~ /$re/
defined $t $t =~ /$re/
Truth table100%3/3
A and B
A= defined $tB= $t =~ /$re/
ABresult
0X0
100
111
1149 8 ▶ : map { $count++; my $t = $_->text; defined $t && $t eq $link ? $count : () } @possible_links;
Branch: 2 branches
BranchTrueFalseTotal
defined $re ? : 7 1 8
defined $t && $t eq $link ? : 1 9 10
Condition100%3/3
defined $t and $t eq $link
!ll&&!rl&&r
181
MC/DC100%2/2
defined $t and $t eq $link
defined $t $t eq $link
Truth table100%3/3
A and B
A= defined $tB= $t eq $link
ABresult
0X0
100
111
1150 8 ▶ if (@links > 1) {
Branch: 2 branches
BranchTrueFalseTotal
if (@links > 1) { } 0 8 8
elsif (@links == 0) { } 0 8 8
1151 0 $self->print_pairs([ @links ],[ map {$possible_links[$_]->[1]} @links ]);
1152 0 undef $link;
1153 } elsif (@links == 0) {
1154 0 print "No match.\n";
1155 0 undef $link;
1156 } else {
1157 8 $self->status( "Found $links[0]\n" );
1158 8 $link = $links[0];
1159 8 ▶ if ($possible_links[$count]->url =~ /^javascript:(.*)/i) {
Branch: 1 branch
BranchTrueFalseTotal
if ($possible_links[$count]->url =~ /^javascript:(.*)/i) 0 8 8
1160 0 print "Can't follow javascript link $1\n";
1161 0 undef $link;
1162 };
1163 };
1164 };
1165  
1166 11 ▶ if (defined $link) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $link) 11 0 11
1167 11 eval {
1168 11 $self->agent->follow_link('n' => $link +1);
1169 11 ▶ my ( $hist_option, $hist_value ) =
Branch: 2 branches
BranchTrueFalseTotal
$user_link =~ /^\d+$/ ? : 3 8 11
ref $user_link ? : 7 1 8
1170 $user_link =~ /^\d+$/
1171 ? ('n', $user_link + 1 )
1172 : ref $user_link
1173 ? ( 'text_regex', $user_link_expr )
1174 : ( 'text' , $user_link_expr );
1175 11 $self->add_history(
1176 sprintf qq{\$agent->follow_link('%s' => %s);},
1177 $hist_option,
1178 $hist_value
1179 );
1180 11 $self->activate_first_form;
1181 11 ▶ if ($self->option('autosync')) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->option("autosync")) 1 10 11
1182 1 $self->sync_browser;
1183 };
1184 11 $self->status( "(".$self->agent->res->code.")\n" );
1185 };
1186 11 ▶ warn $@ if $@;
Branch: 1 branch
BranchTrueFalseTotal
if $@ 0 11 11
1187 };
1188 };
1189  
1190 # Complete partially typed links :
1191 1 ▶sub comp_open {
Subroutine comp_open: called CC=2 SCAR=7.0
Pod undocumented
1192 1 my ($self,$word,$line,$start) = @_;
1193 1 my @completions = eval { grep {/^$word/} map { $self->link_text( $_ )} ($self->agent->find_all_links()) };
1194 1 ▶ $self->display_user_warning($@) if $@;
Branch: 1 branch
BranchTrueFalseTotal
if $@ 0 1 1
1195 1 return @completions;
1196 };
1197  
1198 =head2 back
1199  
1200 Go back one page in the browser page history.
1201  
1202 =cut
1203  
1204 2 ▶sub run_back {
Subroutine run_back: called CC=3 SCAR=11.0
Pod undocumented
1205 2 my ($self) = @_;
1206 2 eval {
1207 2 $self->agent->back();
1208 2 $self->add_history('$agent->back();');
1209 2 ▶ $self->sync_browser
Branch: 1 branch
BranchTrueFalseTotal
if $self->option("autosync") 1 1 2
1210 if ($self->option('autosync'));
1211 };
1212 2 ▶ warn $@ if $@;
Branch: 1 branch
BranchTrueFalseTotal
if $@ 0 2 2
1213 };
1214  
1215 =head2 reload
1216  
1217 Repeat the last request, thus reloading the current page.
1218  
1219 Note that also POST requests are blindly repeated, as this command
1220 is mostly intended to be used when testing server side code.
1221  
1222 =cut
1223  
1224 3 ▶sub run_reload {
Subroutine run_reload: called CC=3 SCAR=11.0
Pod undocumented
1225 3 my ($self) = @_;
1226 3 eval {
1227 3 $self->agent->reload();
1228 3 $self->add_history('$agent->reload;');
1229 3 ▶ $self->sync_browser
Branch: 1 branch
BranchTrueFalseTotal
if $self->option("autosync") 1 2 3
1230 if ($self->option('autosync'));
1231 };
1232 3 ▶ $self->display_user_warning($@)
Branch: 1 branch
BranchTrueFalseTotal
if $@ 0 3 3
1233 if $@;
1234 };
1235  
1236 =head2 browse
1237  
1238 Open the web browser with the current page
1239  
1240 Displays the current page in the browser.
1241  
1242 =cut
1243  
1244 3 ▶sub run_browse {
Subroutine run_browse: called CC=1 SCAR=0.0
Pod undocumented
1245 3 my ($self) = @_;
1246 3 $self->sync_browser;
1247 };
1248  
1249 =head2 set
1250  
1251 Set a shell option
1252  
1253 Syntax:
1254  
1255 set OPTION [value]
1256  
1257 The command lists all valid options. Here is a short overview over
1258 the different options available :
1259  
1260 autosync - automatically synchronize the browser window
1261 autorestart - restart the shell when any required module changes
1262 This does not work with C<-e> oneliners.
1263 watchfiles - watch all required modules for changes
1264 cookiefile - the file where to store all cookies
1265 dumprequests - dump all requests to STDOUT
1266 dumpresponses - dump the headers of the responses to STDOUT
1267 verbose - print commands to STDERR as they are run,
1268 when sourcing from a file
1269  
1270 =cut
1271  
1272 2 ▶sub run_set {
Subroutine run_set: called CC=7 SCAR=20.3
Pod undocumented
1273 2 my ($self,$option,$value) = @_;
1274 2 ▶ $option ||= "";
Condition100%2/2
$option ||= ""
l!l
11
MC/DC100%1/1
$option ||= ""
$option
Truth table100%2/2
A
A= $option
Aresult
11
00
1275 2 ▶ if ($option && exists $self->{options}->{$option}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($option and exists $self->{'options'}{$option}) { } 1 1 2
Condition66%2/3
$option and exists $self->{"options"}{$option}
!ll&&!rl&&r
101
MC/DC50%1/2
$option and exists $self->{"options"}{$option}
$option exists $self->{"options"}{$option}
Truth table66%2/3
A and B
A= $optionB= exists $self->{"options"}{$option}
ABresult
0X0
100
111
1276 1 ▶ if ($option and defined $value) {
Branch: 1 branch
BranchTrueFalseTotal
if ($option and defined $value) { } 1 0 1
Condition33%1/3
$option and defined $value
!ll&&!rl&&r
001
MC/DC0%0/2
$option and defined $value
$option defined $value
Truth table33%1/3
A and B
A= $optionB= defined $value
ABresult
0X0
100
111
1277 1 $self->option($option,$value);
1278 } else {
1279 0 $self->print_pairs( [$option], [$self->option($option)] );
1280 };
1281 } else {
1282 1 ▶ print "Unknown option '$option'\n" if $option;
Branch: 1 branch
BranchTrueFalseTotal
if $option 0 1 1
1283 1 print "Valid options are :\n";
1284 1 $self->print_pairs( [keys %{$self->{options}}], [ map {$self->option($_)} (keys %{$self->{options}}) ] );
1285 };
1286 };
1287  
1288 =head2 history
1289  
1290 Display your current session history as the relevant commands.
1291  
1292 Syntax:
1293  
1294 history [FILENAME]
1295  
1296 Commands that have no influence on the browser state are not added
1297 to the history. If a parameter is given to the C<history> command,
1298 the history is saved to that file instead of displayed onscreen.
1299  
1300 =cut
1301  
1302 2 ▶sub run_history {
Subroutine run_history: called CC=1 SCAR=0.0
Pod undocumented
1303 2 my ($self,$filename) = @_;
1304 2 $self->display($filename,$self->history);
1305 };
1306  
1307 =head2 script
1308  
1309 Display your current session history as a Perl script using WWW::Mechanize.
1310  
1311 Syntax:
1312  
1313 script [FILENAME]
1314  
1315 If a parameter is given to the C<script> command, the script is saved to
1316 that file instead of displayed on the console.
1317  
1318 This command was formerly known as C<history>.
1319  
1320 =cut
1321  
1322 2 ▶sub run_script {
Subroutine run_script: called CC=1 SCAR=0.0
Pod undocumented
1323 2 my ($self,$filename) = @_;
1324 2 $self->display($filename,$self->script(" "));
1325 };
1326  
1327 =head2 comment
1328  
1329 Adds a comment to the script and the history. The comment
1330 is prepended with a \n to increase readability.
1331  
1332 =cut
1333  
1334 0 ▶sub run_comment {
Subroutine run_comment: not called CC=2 SCAR=17.9
Pod undocumented
1335 0 my $self = shift;
1336 0 ▶ if (@_)
Branch: 1 branch
BranchTrueFalseTotal
if (@_) 0 0 0
1337 {
1338 0 $self->add_history("\n# @_ ");
1339 }
1340 }
1341  
1342 =head2 fillout
1343  
1344 Fill out the current form
1345  
1346 Interactively asks the values hat have no preset
1347 value via the autofill command.
1348  
1349 =cut
1350  
1351 14 ▶sub run_fillout {
Subroutine run_fillout: called CC=4 SCAR=14.0
Pod undocumented
1352 14 my ($self) = @_;
1353 14 my @interactive_values;
1354 14 eval {
1355 14 $self->{answers} = [];
1356 14 my $form = $self->agent->current_form;
1357 14 ▶ if ($form) {
Branch: 1 branch
BranchTrueFalseTotal
if ($form) { } 14 0 14
1358 14 $self->{formfiller}->fill_form($self->agent->current_form);
1359 14 @interactive_values = @{$self->{answers}};
1360 } else {
1361 0 $self->display_user_warning( "No form found on the current page." )
1362 };
1363 };
1364 14 ▶ warn $@ if $@;
Branch: 1 branch
BranchTrueFalseTotal
if $@ 0 14 14
1365 $self->add_history( join( "\n",
1366 14 ▶ map { sprintf( q[$formfiller->add_filler( '%s' => Fixed => '%s' );], $_->[0], defined $_->[1] ? $_->[1] : '' ) } @interactive_values) . '$formfiller->fill_form($agent->current_form);');
Branch: 1 branch
BranchTrueFalseTotal
defined $_->[1] ? : 23 1 24
1367 };
1368  
1369 =head2 auth
1370  
1371 Set basic authentication credentials.
1372  
1373 Syntax:
1374  
1375 auth [authority realm] user password
1376  
1377 If you get back a 401, you can simply supply the matching
1378 user and password, as the authority and the realm are taken
1379 from the last response:
1380  
1381 >get http://www.example.com
1382 Retrieving http://www.example.com(401)
1383 http://www.example.com>auth corion secret
1384 http://www.example.com>get http://www.example.com
1385 Retrieving http://www.example.com(200)
1386 http://www.example.com>
1387  
1388 If you know the authority and the realm in advance, you can
1389 presupply the credentials, for example at the start of the script :
1390  
1391 >auth www.example.com:80 secure_realm corion secret
1392 >get http://www.example.com
1393 Retrieving http://www.example.com(200)
1394 http://www.example.com>
1395  
1396 =cut
1397  
1398 4 ▶sub run_auth {
Subroutine run_auth: called CC=5 SCAR=16.8
Pod undocumented
1399 4 my ($self) = shift;
1400 4 my ($authority,$realm,$user,$password,$code);
1401 4 ▶ if (scalar @_ == 4) {
Branch: 2 branches
BranchTrueFalseTotal
if (scalar @_ == 4) { } 1 3 4
elsif (scalar @_ == 2) { } 3 0 3
1402 1 ($authority,$realm,$user,$password) = @_;
1403  
1404 1 ▶ $code = sub {
Subroutine __ANON__: called CC=1 SCAR=0.0
1405 1 $self->agent->credentials($authority,$realm,$user,$password);
1406 1 };
1407 } elsif (scalar @_ == 2) {
1408 3 ($user,$password) = @_;
1409 3 ▶ $password = "" if not defined $password;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $password 0 3 3
1410  
1411 3 ▶ if ($self->agent->res) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->agent->res) { } 3 0 3
1412 3 ▶ $code = sub {
Subroutine __ANON__: called CC=3 SCAR=11.0
1413 3 $realm = '';
1414 3 ▶ if (($self->agent->res->www_authenticate || '') =~ /\brealm=(['"]?)(.*?)\1/) {
Branch: 1 branch
BranchTrueFalseTotal
if (($self->agent->res->www_authenticate || "") =~ /\brealm=(['"]?)(.*?)\1/) 2 1 3
Condition100%2/2
$self->agent->res->www_authenticate || ''
l!l
21
MC/DC100%1/1
$self->agent->res->www_authenticate || ''
$self->agent->res->www_authenticate
Truth table100%2/2
A
A= $self->agent->res->www_authenticate
Aresult
11
00
1415 2 $realm = $2;
1416 };
1417 3 $authority = $self->agent->res->request->uri->host_port;
1418 3 $self->agent->credentials($authority,$realm,$user,$password);
1419 3 };
1420 } else {
1421 0 $self->display_user_warning("Cannot guess the authority and realm without a previous request. Use the four-argument form of auth instead.");
1422 0 $authority = $realm = '';
1423 0 ▶ $code = sub {
Subroutine __ANON__: not called CC=1 SCAR=6.9
1424 0 $self->agent->credentials($user => $password);
1425 0 };
1426 }
1427 } else {
1428 0 $self->display_user_warning("Authentication needs two or four arguments");
1429 0 return;
1430 };
1431 4 $code->();
1432 4 my $body = $self->munge_code($code);
1433  
1434 4 $self->add_history(
1435 sprintf( q{my ($authority,$realm,$user,$password) = ('%s','%s','%s','%s');},
1436 $authority, $realm, $user, $password),
1437 $body,
1438 );
1439 };
1440  
1441 =head2 table
1442  
1443 Display a table described by the columns COLUMNS.
1444  
1445 Syntax:
1446  
1447 table COLUMNS
1448  
1449 Example:
1450  
1451 table Product Price Description
1452  
1453 If there is a table on the current page that has in its first row the three
1454 columns C<Product>, C<Price> and C<Description> (not necessarily in that order),
1455 the script will display these columns of the whole table.
1456  
1457 The C<HTML::TableExtract> module is needed for this feature.
1458  
1459 =cut
1460  
1461 0 ▶sub run_table {
Subroutine run_table: not called CC=3 SCAR=24.8
Pod undocumented
1462 0 my ($self,@columns) = @_;
1463  
1464 0 eval {
1465 0 require HTML::TableExtract;
1466 0 ▶ die "I need a HTML::TableExtract version of 2 or greater. I found '$HTML::TableExtract::VERSION'"
Branch: 1 branch
BranchTrueFalseTotal
if $HTML::TableExtract::VERSION < 2 0 0 0
1467 if $HTML::TableExtract::VERSION < 2;
1468  
1469 0 ▶ my $code = sub {
Subroutine __ANON__: not called CC=3 SCAR=24.8
1470 0 my $table = HTML::TableExtract->new( headers => [ @columns ] );
1471 0 (my $content = $self->agent->content) =~ s/\&nbsp;?//g;
1472 0 $table->parse($content);
1473 0 my @lines;
1474 0 push @lines, join(", ", @columns),"\n";
1475 0 for my $ts ($table->table_states) {
1476 0 for my $row ($ts->rows) {
1477 0 push @lines, ">".join(", ", @$row)."<\n";
1478 };
1479 };
1480 0 $self->print_paged(@lines);
1481 0 };
1482 0 $code->();
1483 0 my $body = $self->munge_code($code);
1484  
1485 $self->add_history(
1486 "require HTML::TableExtract;\n",
1487 0 sprintf( 'my @columns = ( %s );'."\n", join( ",", map( { s/(['\\])/\\$1/g; qq('$_') } @columns ))),
1488 $body
1489 );
1490 };
1491 0 ▶ $self->display_user_warning( "Couldn't load HTML::TableExtract: $@" )
Branch: 1 branch
BranchTrueFalseTotal
if $@ 0 0 0
1492 if ($@);
1493 };
1494  
1495 =head2 tables
1496  
1497 Display a list of tables.
1498  
1499 Syntax:
1500  
1501 tables
1502  
1503 This command will display the top row for every
1504 table on the current page. This is convenient if you want
1505 to find out what the exact spellings for each column are.
1506  
1507 The command does not always work nice, for example if a
1508 site uses tables for layout, it will be harder to guess
1509 what tables are irrelevant and what tables are relevant.
1510  
1511 L<HTML::TableExtract> is needed for this feature.
1512  
1513 =cut
1514  
1515 2 ▶sub run_tables {
Subroutine run_tables: called CC=5 SCAR=27.8
Pod undocumented
1516 2 my ($self,@columns) = @_;
1517  
1518 2 eval {
1519 2 require HTML::TableExtract;
1520 0 ▶ die "I need a HTML::TableExtract version of 2 or greater. I found '$HTML::TableExtract::VERSION'"
Branch: 1 branch
BranchTrueFalseTotal
if $HTML::TableExtract::VERSION < 2 0 0 0
1521 if $HTML::TableExtract::VERSION < 2;
1522  
1523 0 my $table = HTML::TableExtract->new( subtables => 1 );
1524 0 (my $content = $self->agent->content) =~ s/\&nbsp;?//g;
1525 0 $table->parse($content);
1526 0 my @lines;
1527 0 for my $ts ($table->table_states) {
1528 0 my ($row) = $ts->rows;
1529 0 ▶ if (grep { /\S/ } (@$row)) {
Branch: 1 branch
BranchTrueFalseTotal
if (grep {/\S/;} @$row) 0 0 0
1530 0 push @lines, join( "", "Table ", join( ",",$ts->coords ), " : ", join(",", @$row),"\n" );
1531 };
1532 };
1533 0 $self->print_paged(@lines);
1534 };
1535 2 ▶ $self->display_user_warning( $@ )
Branch: 1 branch
BranchTrueFalseTotal
if $@ 2 0 2
1536 if $@;
1537 };
1538  
1539 =head2 cookies
1540  
1541 Set the cookie file name
1542  
1543 Syntax:
1544  
1545 cookies FILENAME
1546  
1547 =cut
1548  
1549 1 ▶sub run_cookies {
Subroutine run_cookies: called CC=1 SCAR=0.0
Pod undocumented
1550 1 my ($self,$filename) = @_;
1551 1 $self->agent->cookie_jar(HTTP::Cookies->new(
1552 file => $filename,
1553 autosave => 1,
1554 ignore_discard => 1,
1555 ));
1556 };
1557  
1558 0 ▶sub run_ {
Subroutine run_: not called CC=1 SCAR=0.0
Pod undocumented
1559 # ignore empty lines
1560 };
1561  
1562 =head2 autofill
1563  
1564 Define an automatic value
1565  
1566 Sets a form value to be filled automatically. The NAME parameter is
1567 the WWW::Mechanize::FormFiller::Value subclass you want to use. For
1568 session fields, C<Keep> is a good candidate, for interactive stuff,
1569 C<Ask> is a value implemented by the shell.
1570  
1571 A field name starting and ending with a slash (C</>) is taken to be
1572 a regular expression and will be applied to all fields with their
1573 name matching the expression. A field with a matching name still
1574 takes precedence over the regular expression.
1575  
1576 Syntax:
1577  
1578 autofill NAME [PARAMETERS]
1579  
1580 Examples:
1581  
1582 autofill login Fixed corion
1583 autofill password Ask
1584 autofill selection Random red green orange
1585 autofill session Keep
1586 autofill "/date$/" Random::Date string "%m/%d/%Y"
1587  
1588 =cut
1589  
1590 49 ▶sub run_autofill {
Subroutine run_autofill: called CC=5 SCAR=16.4
Pod undocumented
1591 49 my ($self,$name,$class,@args) = @_;
1592 49 ▶ @args = ($self)
Branch: 1 branch
BranchTrueFalseTotal
if $class eq "Ask" 0 49 49
1593 if ($class eq 'Ask');
1594 49 ▶ if ($class) {
Branch: 1 branch
BranchTrueFalseTotal
if ($class) { } 49 0 49
1595 49 my $name_vis;
1596 49 $name = $self->re_or_string($name);
1597 49 ▶ if (ref $name) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $name) { } 2 47 49
1598 2 $name_vis = qq{qr($name)};
1599 #warn "autofill RE detected $name";
1600 } else {
1601 47 $name_vis = qq{"$name"};
1602 };
1603 49 eval {
1604 49 $self->{formfiller}->add_filler($name,$class,@args);
1605 49 $self->add_history( sprintf qq{\$formfiller->add_filler( %s => "%s" => %s ); }, $name_vis, $class, join( ",", map {qq{'$_'}} @args));
1606 };
1607 49 ▶ warn $@
Branch: 1 branch
BranchTrueFalseTotal
if $@ 0 49 49
1608 if $@;
1609 } else {
1610 0 warn "No class for the autofiller given\n";
1611 };
1612 };
1613  
1614 =head2 eval
1615  
1616 Evaluate Perl code and print the result
1617  
1618 Syntax:
1619  
1620 eval CODE
1621  
1622 For the generated scripts, anything matching the regular expression
1623 C</\$self-E<gt>agent\b/> is automatically
1624 replaced by C<$agent> in your eval code, to do the Right Thing.
1625  
1626 Examples:
1627  
1628 # Say hello
1629 eval "Hello World"
1630  
1631 # And take a look at the current content type
1632 eval $self->agent->ct
1633  
1634 =cut
1635  
1636 64 ▶sub run_eval {
Subroutine run_eval: called CC=3 SCAR=11.2
Pod undocumented
1637 64 my ($self,@rest) = @_;
1638 64 my $code = $self->line;
1639 64 ▶ if ($code !~ /^eval\s+(.*)$/sm) {
Branch: 1 branch
BranchTrueFalseTotal
if (not $code =~ /^eval\s+(.*)$/ms) { } 1 63 64
1640 #warn "Don't know what to do with '$code'";
1641 1 $self->display_user_warning("Don't know what to make of '$code'");
1642 } else {
1643 63 my $str = $1;
1644 63 my $code = qq{ do { $str } };
1645 0 ▶ my @res = eval $code;
Subroutine BEGIN: called CC=1 SCAR=0.1
Subroutine __ANON__: called CC=2 SCAR=7.1
1646 63 ▶ if (my $err = $@) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $err = $@) 0 63 63
1647 #warn "Don't know what to do with '$str' ($err)";
1648 0 $self->display_user_warning($err);
1649 return
1650 0 };
1651 63 print join "", @res,"\n";
1652  
1653  
1654 63 my $script_code = $self->munge_code(qq{print $code, "\\n";});
1655 #warn "Script: $script_code<<";
1656 63 $self->add_history( $script_code );
1657 };
1658 };
1659  
1660 =head2 source
1661  
1662 Execute a batch of commands from a file
1663  
1664 Syntax:
1665  
1666 source FILENAME
1667  
1668 =cut
1669  
1670 28 ▶sub run_source {
Subroutine run_source: called CC=3 SCAR=11.0
Pod undocumented
1671 28 my ($self,$file) = @_;
1672 28 ▶ if ($file) {
Branch: 1 branch
BranchTrueFalseTotal
if ($file) { } 3 1 4
1673 27 eval { $self->source_file($file); };
1674 4 ▶ if ($@) {
Branch: 1 branch
BranchTrueFalseTotal
if ($@) 2 1 3
1675 2 $self->display_user_warning( "Could not source file '$file' : $@" );
1676 };
1677 } else {
1678 1 print "Syntax: source FILENAME\n";
1679 };
1680 };
1681  
1682 =head2 versions
1683  
1684 Print the version numbers of important modules
1685  
1686 Syntax:
1687  
1688 versions
1689  
1690 =cut
1691  
1692 2 ▶sub run_versions {
Subroutine run_versions: called CC=3 SCAR=11.0
Pod undocumented
1693 1 my ($self) = @_;
1694 32 ▶ no strict 'refs';
Subroutine BEGIN: called CC=1 SCAR=0.0
1695 1 my @modules = qw( WWW::Mechanize::Shell WWW::Mechanize::FormFiller WWW::Mechanize
1696 Term::Shell
1697 HTML::Parser HTML::TableExtract HTML::Parser HTML::Display
1698 Pod::Constants
1699 File::Modified );
1700 1 eval "require $_" foreach @modules;
1701 1 ▶ $self->print_pairs( [@modules], [map { defined ${"${_}::VERSION"} ? ${"${_}::VERSION"} : "<undef>" } @modules]);
Branch: 1 branch
BranchTrueFalseTotal
defined ${"${_}::VERSION";} ? : 7 3 10
1702 };
1703  
1704 =head2 timeout
1705  
1706 Set new timeout value for the agent. Effects all subsequent
1707 requests. VALUE is in seconds.
1708  
1709 Syntax:
1710  
1711 timeout VALUE
1712  
1713 =cut
1714  
1715 1 ▶sub run_timeout {
Subroutine run_timeout: called CC=3 SCAR=12.0
Pod undocumented
1716 1 my ($self, $timeout) = @_;
1717 1 ▶ if ($timeout) {
Branch: 1 branch
BranchTrueFalseTotal
if ($timeout) { } 1 0 1
1718 1 eval { $self->agent->timeout($timeout); };
1719 1 ▶ if ($@) {
Branch: 1 branch
BranchTrueFalseTotal
if ($@) 0 1 1
1720 0 print "Could not set new timeout value : $@";
1721 };
1722 1 $self->add_history( sprintf q{$agent->timeout(%s);}, $timeout);
1723 } else {
1724 0 print "Syntax: timeout VALUE\n";
1725 };
1726 };
1727  
1728 =head2 ct
1729  
1730 prints the content type of the most current response.
1731  
1732 Syntax:
1733  
1734 ct
1735  
1736 =cut
1737  
1738 1 ▶sub run_ct {
Subroutine run_ct: called CC=3 SCAR=17.3
Pod undocumented
1739 1 my ($self) = @_;
1740 1 ▶ if ($self->agent->content) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->agent->content) { } 0 1 1
1741 0 eval { print $self->agent->ct, "\n"; };
1742 0 ▶ if ($@) {
Branch: 1 branch
BranchTrueFalseTotal
if ($@) 0 0 0
1743 0 print "Could not get content-type : $@";
1744 };
1745 0 $self->add_history('print $agent->ct, "\n";');
1746 } else {
1747 1 print "No content available yet!\n";
1748 }
1749 };
1750  
1751 =head2 referrer
1752  
1753 set the value of the Referer: header
1754  
1755 Syntax:
1756  
1757 referer URL
1758 referrer URL
1759  
1760 =cut
1761  
1762 0 ▶sub run_referrer {
Subroutine run_referrer: not called CC=3 SCAR=24.8
Pod undocumented
1763 0 my ($self, $referrer) = @_;
1764 0 ▶ if (defined $referrer) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $referrer) { } 0 0 0
1765 0 eval { $self->agent->add_header(Referer => $referrer); };
1766 0 ▶ if ($@) {
Branch: 1 branch
BranchTrueFalseTotal
if ($@) 0 0 0
1767 0 print "Could not set referrer : $@";
1768 };
1769 # warn "Added $referrer";
1770 0 $self->add_history( sprintf q{$agent->add_header('Referer', '%s');}, $referrer);
1771 } else {
1772 # print "syntax: referer|referrer URL\n";
1773 0 eval {
1774 0 print "Referer: ", $self->agent->{req}->header('Referer'),"\n";
1775 };
1776 }
1777 };
1778  
1779 =head2 referer
1780  
1781 Alias for referrer
1782  
1783 =cut
1784  
1785 0 ▶sub run_referer {
Subroutine run_referer: not called CC=1 SCAR=6.9
Pod undocumented
1786 0 goto &WWW::Mechanize::Shell::run_referrer
1787 };
1788 # sub alias_referrer { qw(referer) };
1789  
1790 =head2 response
1791  
1792 display the last server response
1793  
1794 =cut
1795  
1796 1 ▶sub run_response {
Subroutine run_response: called CC=1 SCAR=0.0
Pod undocumented
1797 1 my ($self) = @_;
1798 1 eval { $self->print_paged( $self->agent->res->as_string )};
1799 };
1800  
1801 =head2 C<< $shell->munge_code( CODE ) >>
1802  
1803 Munges a coderef to become code fit for
1804 output independent of WWW::Mechanize::Shell.
1805  
1806 =cut
1807  
1808 32 our %munge_map = (
1809 '^{' => '',
1810 '}$' => '',
1811 '\$self->print_paged' => 'print ',
1812 '\$self->agent' => '$agent',
1813 '\s*package ' . __PACKAGE__ . ';' => '',
1814 );
1815  
1816 67 ▶sub munge_code {
Subroutine munge_code: called CC=4 SCAR=13.9
Pod documented
1817 67 my ($self, $code) = @_;
1818 67 my $body;
1819  
1820 67 ▶ if (ref $code) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $code) { } 4 63 67
1821 # Munge code
1822 4 my $d = B::Deparse->new('-sC');
1823 4 ▶ if ($d->can('ambient_pragmas')) {
Branch: 1 branch
BranchTrueFalseTotal
if ($d->can("ambient_pragmas")) 4 0 4
1824 4 $d->ambient_pragmas(strict => 'all', warnings => 'all');
1825 };
1826 4 $body = $d->coderef2text($code);
1827 } else {
1828 63 $body = $code
1829 }
1830  
1831 67 ▶ while (my ($key,$val) = each %munge_map) {
Branch: 1 branch
BranchTrueFalseTotal
if (() = each %WWW::Mechanize::Shell::munge_map) 335 67 402
1832 335 $body =~ s/$key/$val/gs;
1833 };
1834  
1835 67 $body
1836 };
1837  
1838 =head2 C<< shell >>
1839  
1840 This subroutine is exported by default as a convenience method
1841 so that the following oneliner invocation works:
1842  
1843 perl -MWWW::Mechanize::Shell -eshell
1844  
1845 You can pass constructor arguments to this
1846 routine as well. Any scripts given in C<< @ARGV >>
1847 will be run. If C<< @ARGV >> is empty,
1848 an interactive loop will be started.
1849  
1850 =cut
1851  
1852 1 ▶sub shell {
Subroutine shell: called CC=2 SCAR=7.4
Pod documented
1853 1 my @args = ("shell",@_);
1854 1 my $shell = WWW::Mechanize::Shell->new(@args);
1855  
1856 1 ▶ if (@ARGV) {
Branch: 1 branch
BranchTrueFalseTotal
if (@ARGV) { } 0 1 1
1857 0 $shell->source_file( @ARGV );
1858 } else {
1859 1 $shell->cmdloop;
1860 };
1861 };
1862  
1863 {
1864 package # hide from CPAN
1865 WWW::Mechanize::FormFiller::Value::Ask;
1866 32 ▶ use WWW::Mechanize::FormFiller;
Subroutine BEGIN: called CC=1 SCAR=0.0
1867 32 ▶ use base 'WWW::Mechanize::FormFiller::Value::Callback';
Subroutine BEGIN: called CC=1 SCAR=0.0
1868  
1869 32 our $VERSION = '0.63';
1870  
1871 84 ▶ sub new {
Subroutine new: called CC=2 SCAR=7.0
1872 84 my ($class,$name,$shell) = @_;
1873 # Using the name here to allow for late binding and overriding via eval()
1874 # from the shell command line
1875 #warn __PACKAGE__ . "::ask_value";
1876 84 my $self = $class->SUPER::new($name, __PACKAGE__ . '::ask_value');
1877 84 $self->{shell} = $shell;
1878 84 ▶ Carp::carp "WWW::Mechanize::FormFiller::Value::Ask->new called without a value for the shell" unless $self->{shell};
Branch: 1 branch
BranchTrueFalseTotal
unless $self->{"shell"} 0 84 84
1879  
1880 84 $self;
1881 };
1882  
1883 32 ▶ sub ask_value {
Subroutine ask_value: called CC=7 SCAR=28.0
1884 32 my ($self,$input) = @_;
1885 32 my @values;
1886 32 ▶ if ($input->possible_values) {
Branch: 1 branch
BranchTrueFalseTotal
if ($input->possible_values) 0 32 32
1887 0 @values = $input->possible_values;
1888 0 print join( "|", @values ), "\n";
1889 };
1890 32 my $value;
1891 32 $value = $input->value;
1892 #warn $value if $value;
1893 32 ▶ if ($input->type !~ /^(submit|hidden)$/) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($input->type =~ /^(submit|hidden)$/) 0 32 32
1894 0 ▶ $value = $self->{shell}->prompt("(" . $input->type . ")" . $input->name . "> [" . ($input->value || "") . "] ",
Condition50%2/4
$input->value || ''
l!l
191
$input->value || ''
l!l
00
MC/DC100%1/1
$input->value || ''
$input->value
MC/DC0%0/1
$input->value || ''
$input->value
Truth table100%2/2
A
A= $input->value
Aresult
11
00
Truth table0%0/2
A
A= $input->value
Aresult
11
00
1895 ($input->value||''), @values );
1896 0 ▶ undef $value if ($value eq "" and $input->type eq "checkbox");
Branch: 1 branch
BranchTrueFalseTotal
if $value eq "" and $input->type eq "checkbox" 0 0 0
Condition0%0/3
$value eq "" and $input->type eq "checkbox"
!ll&&!rl&&r
000
MC/DC0%0/2
$value eq "" and $input->type eq "checkbox"
$value eq "" $input->type eq "checkbox"
Truth table0%0/3
A and B
A= $value eq ""B= $input->type eq "checkbox"
ABresult
0X0
100
111
1897 0 push @{$self->{shell}->{answers}}, [ $input->name, $value ];
1898 };
1899 32 $value;
1900 };
1901 32 };
1902  
1903 __END__
↑ Summary