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   58953 use strict;
  13         34  
  13         342  
3 13     13   56 use Carp;
  13         23  
  13         754  
4              
5 13     13   70 use vars qw( $VERSION @ISA );
  14         33  
  13         1264  
6              
7             $VERSION = '0.13';
8             @ISA = ();
9              
10             sub load_value_class {
11 55     54 0 509 my ($class) = @_;
12 55 100       111 if ($class) {
13 13     13   91 no strict 'refs';
  14         27  
  14         2260  
14              
15 54         2607 my $full_class = "WWW::Mechanize::FormFiller::Value::$class";
16              
17 53 100       2834 unless (defined eval '${' . $full_class . '::VERSION}') {
18 5     4   1759 eval "use $full_class";
  5     5   25029  
  5     5   285  
  6         1319  
  6         15  
  6         88  
  6         857  
  6         15  
  6         572  
  11         723  
  1         2  
  1         1  
  1         2  
19 11 50       70 Carp::confess $@ if $@;
20             };
21             } else {
22 3 100       25 Carp::croak "No class name given to load" unless $class;
23             };
24             };
25              
26             sub new {
27 52     51 1 42415 my ($class,%args) = @_;
28 52         261 my $self = {
29             values => {},
30             default => undef
31             };
32 51         108 bless $self, $class;
33              
34 51 100       154 if (exists $args{default}) {
35 3         5 my ($class,@args) = @{$args{default}};
  3         6  
36 3         8 load_value_class($class);
37 14     13   88 no strict 'refs';
  14         15792  
  14         2835  
38 3         11 $self->{default} = "WWW::Mechanize::FormFiller::Value::$class"->new(undef, @args);
39             };
40              
41 51 100       121 if (exists $args{values}) {
42 9 100       27 if (ref $args{values} eq 'ARRAY') {
43 8         968 for my $value (@{$args{values}}) {
  8         17  
44 11 100       659 if (ref $value eq 'ARRAY') {
45 10         92 my ($name,$class,@args) = @$value;
46 10 100       21 if ($class) {
47 8         269 $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       12 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         5 Carp::croak "values parameter must be an array reference";
58             };
59             };
60 51         157 return $self;
61             };
62              
63             sub add_filler {
64 51     51 1 43825 my ($self,$name,$class,@args) = @_;
65 51         141 load_value_class($class);
66              
67 51 100       169 if ($class) {
68 14     13   11763 no strict 'refs';
  14         109  
  14         4891  
69 50         260 $self->add_value( $name, "WWW::Mechanize::FormFiller::Value::$class"->new($name, @args));
70             } else {
71 2         403 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 122 my ($self, $name, $value) = @_;
77 61 100 66     182 if (ref $name and UNIVERSAL::isa($name,'Regexp')) {
78 4         14 $self->{values}->{byre}->{$name} = $value;
79             } else {
80 58         201 $self->{values}->{byname}->{$name} = $value;
81             };
82 61         166 $value;
83             };
84              
85             sub default {
86 11     10 0 332 my ($self,$newdefault) = @_;
87 11         20 my $result = $self->{default};
88 11 50       26 $self->{default} = $newdefault if (@_ > 1);
89 11         25 $result;
90             };
91              
92             sub find_filler {
93 66     65 0 120 my ($self,$input) = @_;
94 66 50       153 croak "No input given" unless defined $input;
95 66         89 my $value;
96 66 100       210 if (exists $self->{values}->{byname}->{$input->name()}) {
    100          
    50          
    50          
97 51         429 $value = $self->{values}->{byname}->{$input->name};
98 8         15 } elsif (grep { $input->name =~ /$_/ } keys %{$self->{values}->{byre}}) {
  16         136  
99 6         77 my $match = (grep { $input->name =~ /$_/ } keys %{$self->{values}->{byre}})[0];
  6         26  
  6         14  
100 6         57 $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         340 $value;
108             };
109              
110             sub fill_form {
111 41     40 1 8106 my ($self,$form) = @_;
112 41         133 for my $input ($form->inputs) {
113 66         336 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       1025 if (defined $value) {
116             # Hmm - who cares about whether a value was hidden/readonly ??
117 14     13   379 no warnings;
  14         56  
  14         3675  
118 56         258 local $^W = undef;
119 56         494 my $v = $value->value($input);
120 56 100 100     902553 undef $v if ($input->type() eq "checkbox" and $v eq "");
121 56         591 eval { $input->value( $v ) };
  56         162  
122 56 50       1178 $@ and croak "Field '" .$input->name. "' had illegal value: $v";
123             };
124             };
125             };
126              
127             sub fillout {
128 8     7 1 45626 my $self_class = shift;
129 8 100       28 my $self = ref $self_class ? $self_class : $self_class->new();
130 8         13 my $form;
131              
132 8         17 while (@_) {
133 17 100 66     40 if (ref $_[0] and eval { UNIVERSAL::isa($_[0],'HTML::Form') }) {
  8         109  
134 8 100       222 croak "Two HTML::Form objects passed into fillout()" if ($form);
135 7         313 $form = shift;
136             } else {
137 10         15 my $field = shift;
138 10 100       26 if (ref $_[0] eq 'ARRAY') {
139 6         7 my $args = shift;
140 6         14 $self->add_filler($field,@$args);
141             } else {
142 5         7 my $value = shift;
143 5         81 $self->add_filler($field,'Fixed',$value);
144             };
145             };
146             };
147 7 100       32 $self->fill_form($form) if $form;
148 7         307 $self;
149             };
150              
151             1;
152             __END__