File Coverage

blib/lib/WWW/Mechanize/FormFiller.pm
Criterion Covered Total %
statement 144 144 100.0
branch 48 54 88.8
condition 7 9 77.7
subroutine 19 19 100.0
pod 5 8 62.5
total 223 234 95.3


line stmt bran cond sub pod time code
1             package WWW::Mechanize::FormFiller;
2 14     13   14248 use strict;
  13         14  
  13         327  
3 13     13   42 use Carp;
  13         13  
  13         804  
4              
5 13     13   46 use vars qw( $VERSION @ISA );
  14         18  
  13         1062  
6              
7             $VERSION = '0.12';
8             @ISA = ();
9              
10             sub load_value_class {
11 55     54 0 479 my ($class) = @_;
12 55 100       81 if ($class) {
13 13     13   48 no strict 'refs';
  14         20  
  14         1767  
14              
15 54         2554 my $full_class = "WWW::Mechanize::FormFiller::Value::$class";
16              
17 53 100       2571 unless (defined eval '${' . $full_class . '::VERSION}') {
18 5     4   1667 eval "use $full_class";
  5     5   17071  
  5     5   246  
  6         1296  
  6         14  
  6         71  
  6         1518  
  6         10  
  6         492  
  11         689  
  1         1  
  1         2  
  1         1  
19 11 50       65 Carp::confess $@ if $@;
20             };
21             } else {
22 3 100       12 Carp::croak "No class name given to load" unless $class;
23             };
24             };
25              
26             sub new {
27 52     51 1 26864 my ($class,%args) = @_;
28 52         266 my $self = {
29             values => {},
30             default => undef
31             };
32 51         73 bless $self, $class;
33              
34 51 100       136 if (exists $args{default}) {
35 3         2 my ($class,@args) = @{$args{default}};
  3         6  
36 3         4 load_value_class($class);
37 14     13   54 no strict 'refs';
  14         13166  
  14         2226  
38 3         10 $self->{default} = "WWW::Mechanize::FormFiller::Value::$class"->new(undef, @args);
39             };
40              
41 51 100       93 if (exists $args{values}) {
42 9 100       30 if (ref $args{values} eq 'ARRAY') {
43 8         681 for my $value (@{$args{values}}) {
  8         16  
44 11 100       408 if (ref $value eq 'ARRAY') {
45 10         67 my ($name,$class,@args) = @$value;
46 10 100       21 if ($class) {
47 8         318 $self->add_filler( $name, $class, @args );
48             } else {
49 3 100       23 Carp::croak "Each element of the values array must have at least 2 elements (name and class)" unless defined $class;
50 3 100       7 Carp::croak "Each element of the values array must have a class name" unless $class;
51             };
52             } else {
53 2         4 Carp::croak "Each element of the values array must be an array reference";
54             };
55             }
56             } else {
57 2         4 Carp::croak "values parameter must be an array reference";
58             };
59             };
60 51         104 return $self;
61             };
62              
63             sub add_filler {
64 51     51 1 30696 my ($self,$name,$class,@args) = @_;
65 51         94 load_value_class($class);
66              
67 51 100       133 if ($class) {
68 14     13   11257 no strict 'refs';
  14         98  
  14         3950  
69 50         230 $self->add_value( $name, "WWW::Mechanize::FormFiller::Value::$class"->new($name, @args));
70             } else {
71 2         383 Carp::croak "A value must have at least a class name and a field name (which may be undef though)" ;
72             };
73             };
74              
75             sub add_value {
76 61     60 1 94 my ($self, $name, $value) = @_;
77 61 100 66     178 if (ref $name and UNIVERSAL::isa($name,'Regexp')) {
78 4         9 $self->{values}->{byre}->{$name} = $value;
79             } else {
80 58         177 $self->{values}->{byname}->{$name} = $value;
81             };
82 61         124 $value;
83             };
84              
85             sub default {
86 11     10 0 332 my ($self,$newdefault) = @_;
87 11         15 my $result = $self->{default};
88 11 50       23 $self->{default} = $newdefault if (@_ > 1);
89 11         19 $result;
90             };
91              
92             sub find_filler {
93 66     65 0 64 my ($self,$input) = @_;
94 66 50       126 croak "No input given" unless defined $input;
95 66         60 my $value;
96 66 100       195 if (exists $self->{values}->{byname}->{$input->name()}) {
    100          
    50          
    50          
97 51         333 $value = $self->{values}->{byname}->{$input->name};
98 8         13 } elsif (grep { $input->name =~ /$_/ } keys %{$self->{values}->{byre}}) {
  16         103  
99 6         57 my $match = (grep { $input->name =~ /$_/ } keys %{$self->{values}->{byre}})[0];
  6         24  
  6         13  
100 6         41 $value = $self->{values}->{byre}->{$match};
101             } elsif ($input->type eq "image") {
102             # Image inputs are really buttons, and if they have no (user) specified value,
103             # we don't ask about them.
104             } elsif ($self->default) {
105 1         5 $value = $self->default();
106             };
107 66         239 $value;
108             };
109              
110             sub fill_form {
111 41     40 1 5430 my ($self,$form) = @_;
112 41         99 for my $input ($form->inputs) {
113 66         265 my $value = $self->find_filler($input);
114             # We leave all values alone whenever we don't know what to do with them
115 66 100       474 if (defined $value) {
116             # Hmm - who cares about whether a value was hidden/readonly ??
117 14     13   526 no warnings;
  14         51  
  14         2727  
118 56         185 local $^W = undef;
119 56         477 my $v = $value->value($input);
120 56 100 100     470131 undef $v if ($input->type() eq "checkbox" and $v eq "");
121 56         532 eval { $input->value( $v ) };
  56         131  
122 56 50       787 $@ and croak "Field '" .$input->name. "' had illegal value: $v";
123             };
124             };
125             };
126              
127             sub fillout {
128 8     7 1 32596 my $self_class = shift;
129 8 100       25 my $self = ref $self_class ? $self_class : $self_class->new();
130 8         9 my $form;
131              
132 8         17 while (@_) {
133 17 100 66     36 if (ref $_[0] and eval { UNIVERSAL::isa($_[0],'HTML::Form') }) {
  8         90  
134 8 100       169 croak "Two HTML::Form objects passed into fillout()" if ($form);
135 7         285 $form = shift;
136             } else {
137 10         11 my $field = shift;
138 10 100       21 if (ref $_[0] eq 'ARRAY') {
139 6         6 my $args = shift;
140 6         11 $self->add_filler($field,@$args);
141             } else {
142 5         6 my $value = shift;
143 5         67 $self->add_filler($field,'Fixed',$value);
144             };
145             };
146             };
147 7 100       27 $self->fill_form($form) if $form;
148 7         321 $self;
149             };
150              
151             1;
152             __END__