| line | stmt | bran | cond | sub | pod | time | code | 
| 1 |  |  |  |  |  |  | package WWW::Mechanize::FormFiller; | 
| 2 | 14 |  |  | 13 |  | 31447 | use strict; | 
|  | 13 |  |  |  |  | 32 |  | 
|  | 13 |  |  |  |  | 722 |  | 
| 3 | 13 |  |  | 13 |  | 124 | use Carp; | 
|  | 13 |  |  |  |  | 27 |  | 
|  | 13 |  |  |  |  | 1326 |  | 
| 4 |  |  |  |  |  |  |  | 
| 5 | 13 |  |  | 13 |  | 70 | use vars qw( $VERSION @ISA ); | 
|  | 14 |  |  |  |  | 37 |  | 
|  | 13 |  |  |  |  | 1523 |  | 
| 6 |  |  |  |  |  |  |  | 
| 7 |  |  |  |  |  |  | $VERSION = '0.11'; | 
| 8 |  |  |  |  |  |  | @ISA = (); | 
| 9 |  |  |  |  |  |  |  | 
| 10 |  |  |  |  |  |  | sub load_value_class { | 
| 11 | 55 |  |  | 54 | 0 | 1012 | my ($class) = @_; | 
| 12 | 55 | 100 |  |  |  | 137 | if ($class) { | 
| 13 | 13 |  |  | 13 |  | 73 | no strict 'refs'; | 
|  | 14 |  |  |  |  | 28 |  | 
|  | 14 |  |  |  |  | 2716 |  | 
| 14 |  |  |  |  |  |  |  | 
| 15 | 54 |  |  |  |  | 5063 | my $full_class = "WWW::Mechanize::FormFiller::Value::$class"; | 
| 16 |  |  |  |  |  |  |  | 
| 17 | 53 | 100 |  |  |  | 5321 | unless (defined eval '${' . $full_class . '::VERSION}') { | 
| 18 | 5 |  |  | 4 |  | 3687 | eval "use $full_class"; | 
|  | 5 |  |  | 5 |  | 40983 |  | 
|  | 5 |  |  | 5 |  | 430 |  | 
|  | 6 |  |  |  |  | 2397 |  | 
|  | 6 |  |  |  |  | 70 |  | 
|  | 6 |  |  |  |  | 122 |  | 
|  | 6 |  |  |  |  | 2182 |  | 
|  | 6 |  |  |  |  | 25 |  | 
|  | 6 |  |  |  |  | 738 |  | 
|  | 11 |  |  |  |  | 967 |  | 
|  | 1 |  |  |  |  | 2 |  | 
|  | 1 |  |  |  |  | 2 |  | 
|  | 1 |  |  |  |  | 1 |  | 
| 19 | 11 | 50 |  |  |  | 110 | Carp::confess $@ if $@; | 
| 20 |  |  |  |  |  |  | }; | 
| 21 |  |  |  |  |  |  | } else { | 
| 22 | 3 | 100 |  |  |  | 17 | Carp::croak "No class name given to load" unless $class; | 
| 23 |  |  |  |  |  |  | }; | 
| 24 |  |  |  |  |  |  | }; | 
| 25 |  |  |  |  |  |  |  | 
| 26 |  |  |  |  |  |  | sub new { | 
| 27 | 52 |  |  | 51 | 1 | 53131 | my ($class,%args) = @_; | 
| 28 | 52 |  |  |  |  | 322 | my $self = { | 
| 29 |  |  |  |  |  |  | values => {}, | 
| 30 |  |  |  |  |  |  | default => undef | 
| 31 |  |  |  |  |  |  | }; | 
| 32 | 51 |  |  |  |  | 268 | bless $self, $class; | 
| 33 |  |  |  |  |  |  |  | 
| 34 | 51 | 100 |  |  |  | 200 | if (exists $args{default}) { | 
| 35 | 3 |  |  |  |  | 5 | my ($class,@args) = @{$args{default}}; | 
|  | 3 |  |  |  |  | 8 |  | 
| 36 | 3 |  |  |  |  | 7 | load_value_class($class); | 
| 37 | 14 |  |  | 13 |  | 90 | no strict 'refs'; | 
|  | 14 |  |  |  |  | 23101 |  | 
|  | 14 |  |  |  |  | 3556 |  | 
| 38 | 3 |  |  |  |  | 16 | $self->{default} = "WWW::Mechanize::FormFiller::Value::$class"->new(undef, @args); | 
| 39 |  |  |  |  |  |  | }; | 
| 40 |  |  |  |  |  |  |  | 
| 41 | 51 | 100 |  |  |  | 184 | if (exists $args{values}) { | 
| 42 | 9 | 100 |  |  |  | 52 | if (ref $args{values} eq 'ARRAY') { | 
| 43 | 8 |  |  |  |  | 1062 | for my $value (@{$args{values}}) { | 
|  | 8 |  |  |  |  | 26 |  | 
| 44 | 11 | 100 |  |  |  | 584 | if (ref $value eq 'ARRAY') { | 
| 45 | 10 |  |  |  |  | 124 | my ($name,$class,@args) = @$value; | 
| 46 | 10 | 100 |  |  |  | 30 | if ($class) { | 
| 47 | 8 |  |  |  |  | 628 | $self->add_filler( $name, $class, @args ); | 
| 48 |  |  |  |  |  |  | } else { | 
| 49 | 3 | 100 |  |  |  | 34 | Carp::croak "Each element of the values array must have at least 2 elements (name and class)" unless defined $class; | 
| 50 | 3 | 100 |  |  |  | 14 | Carp::croak "Each element of the values array must have a class name" unless $class; | 
| 51 |  |  |  |  |  |  | }; | 
| 52 |  |  |  |  |  |  | } else { | 
| 53 | 2 |  |  |  |  | 6 | Carp::croak "Each element of the values array must be an array reference"; | 
| 54 |  |  |  |  |  |  | }; | 
| 55 |  |  |  |  |  |  | } | 
| 56 |  |  |  |  |  |  | } else { | 
| 57 | 2 |  |  |  |  | 6 | Carp::croak "values parameter must be an array reference"; | 
| 58 |  |  |  |  |  |  | }; | 
| 59 |  |  |  |  |  |  | }; | 
| 60 | 51 |  |  |  |  | 188 | return $self; | 
| 61 |  |  |  |  |  |  | }; | 
| 62 |  |  |  |  |  |  |  | 
| 63 |  |  |  |  |  |  | sub add_filler { | 
| 64 | 51 |  |  | 51 | 1 | 64169 | my ($self,$name,$class,@args) = @_; | 
| 65 | 51 |  |  |  |  | 161 | load_value_class($class); | 
| 66 |  |  |  |  |  |  |  | 
| 67 | 51 | 100 |  |  |  | 425 | if ($class) { | 
| 68 | 14 |  |  | 13 |  | 22829 | no strict 'refs'; | 
|  | 14 |  |  |  |  | 149 |  | 
|  | 14 |  |  |  |  | 6124 |  | 
| 69 | 50 |  |  |  |  | 550 | $self->add_value( $name, "WWW::Mechanize::FormFiller::Value::$class"->new($name, @args)); | 
| 70 |  |  |  |  |  |  | } else { | 
| 71 | 2 |  |  |  |  | 732 | 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 | 127 | my ($self, $name, $value) = @_; | 
| 77 | 61 | 100 | 66 |  |  | 276 | if (ref $name and UNIVERSAL::isa($name,'Regexp')) { | 
| 78 | 4 |  |  |  |  | 23 | $self->{values}->{byre}->{$name} = $value; | 
| 79 |  |  |  |  |  |  | } else { | 
| 80 | 58 |  |  |  |  | 311 | $self->{values}->{byname}->{$name} = $value; | 
| 81 |  |  |  |  |  |  | }; | 
| 82 | 61 |  |  |  |  | 246 | $value; | 
| 83 |  |  |  |  |  |  | }; | 
| 84 |  |  |  |  |  |  |  | 
| 85 |  |  |  |  |  |  | sub default { | 
| 86 | 11 |  |  | 10 | 0 | 634 | my ($self,$newdefault) = @_; | 
| 87 | 11 |  |  |  |  | 21 | my $result = $self->{default}; | 
| 88 | 11 | 50 |  |  |  | 42 | $self->{default} = $newdefault if (@_ > 1); | 
| 89 | 11 |  |  |  |  | 36 | $result; | 
| 90 |  |  |  |  |  |  | }; | 
| 91 |  |  |  |  |  |  |  | 
| 92 |  |  |  |  |  |  | sub find_filler { | 
| 93 | 66 |  |  | 65 | 0 | 120 | my ($self,$input) = @_; | 
| 94 | 66 | 50 |  |  |  | 201 | croak "No input given" unless defined $input; | 
| 95 | 66 |  |  |  |  | 85 | my $value; | 
| 96 | 66 | 100 |  |  |  | 374 | if (exists $self->{values}->{byname}->{$input->name()}) { | 
|  | 8 | 100 |  |  |  | 18 |  | 
|  |  | 50 |  |  |  |  |  | 
|  |  | 50 |  |  |  |  |  | 
| 97 | 51 |  |  |  |  | 603 | $value = $self->{values}->{byname}->{$input->name}; | 
| 98 | 16 |  |  |  |  | 190 | } elsif (grep { $input->name =~ /$_/ } keys %{$self->{values}->{byre}}) { | 
| 99 | 6 |  |  |  |  | 82 | my $match = (grep { $input->name =~ /$_/ } keys %{$self->{values}->{byre}})[0]; | 
|  | 6 |  |  |  |  | 35 |  | 
|  | 6 |  |  |  |  | 21 |  | 
| 100 | 6 |  |  |  |  | 61 | $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 |  |  |  |  | 9 | $value = $self->default(); | 
| 106 |  |  |  |  |  |  | }; | 
| 107 | 66 |  |  |  |  | 406 | $value; | 
| 108 |  |  |  |  |  |  | }; | 
| 109 |  |  |  |  |  |  |  | 
| 110 |  |  |  |  |  |  | sub fill_form { | 
| 111 | 41 |  |  | 40 | 1 | 11073 | my ($self,$form) = @_; | 
| 112 | 41 |  |  |  |  | 181 | for my $input ($form->inputs) { | 
| 113 | 66 |  |  |  |  | 411 | 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 |  |  |  | 1355 | if (defined $value) { | 
| 116 |  |  |  |  |  |  | # Hmm - who cares about whether a value was hidden/readonly ?? | 
| 117 | 14 |  |  | 13 |  | 693 | no warnings; | 
|  | 14 |  |  |  |  | 84 |  | 
|  | 14 |  |  |  |  | 4311 |  | 
| 118 | 56 |  |  |  |  | 364 | local $^W = undef; | 
| 119 | 56 |  |  |  |  | 1241 | my $v = $value->value($input); | 
| 120 | 56 | 100 | 100 |  |  | 697237 | undef $v if ($input->type() eq "checkbox" and $v eq ""); | 
| 121 | 56 |  |  |  |  | 1027 | eval { $input->value( $v ) }; | 
|  | 56 |  |  |  |  | 268 |  | 
| 122 | 56 | 50 |  |  |  | 1230 | $@ and croak "Field '" .$input->name. "' had illegal value: $v"; | 
| 123 |  |  |  |  |  |  | }; | 
| 124 |  |  |  |  |  |  | }; | 
| 125 |  |  |  |  |  |  | }; | 
| 126 |  |  |  |  |  |  |  | 
| 127 |  |  |  |  |  |  | sub fillout { | 
| 128 | 8 |  |  | 7 | 1 | 587338 | my $self_class = shift; | 
| 129 | 8 | 100 |  |  |  | 39 | my $self = ref $self_class ? $self_class : $self_class->new(); | 
| 130 | 8 |  |  |  |  | 14 | my $form; | 
| 131 |  |  |  |  |  |  |  | 
| 132 | 8 |  |  |  |  | 29 | while (@_) { | 
| 133 | 17 | 100 | 66 |  |  | 55 | if (ref $_[0] and eval { UNIVERSAL::isa($_[0],'HTML::Form') }) { | 
|  | 8 |  |  |  |  | 180 |  | 
| 134 | 8 | 100 |  |  |  | 230 | croak "Two HTML::Form objects passed into fillout()" if ($form); | 
| 135 | 7 |  |  |  |  | 687 | $form = shift; | 
| 136 |  |  |  |  |  |  | } else { | 
| 137 | 10 |  |  |  |  | 17 | my $field = shift; | 
| 138 | 10 | 100 |  |  |  | 31 | if (ref $_[0] eq 'ARRAY') { | 
| 139 | 6 |  |  |  |  | 14 | my $args = shift; | 
| 140 | 6 |  |  |  |  | 20 | $self->add_filler($field,@$args); | 
| 141 |  |  |  |  |  |  | } else { | 
| 142 | 5 |  |  |  |  | 12 | my $value = shift; | 
| 143 | 5 |  |  |  |  | 140 | $self->add_filler($field,'Fixed',$value); | 
| 144 |  |  |  |  |  |  | }; | 
| 145 |  |  |  |  |  |  | }; | 
| 146 |  |  |  |  |  |  | }; | 
| 147 | 7 | 100 |  |  |  | 49 | $self->fill_form($form) if $form; | 
| 148 | 7 |  |  |  |  | 762 | $self; | 
| 149 |  |  |  |  |  |  | }; | 
| 150 |  |  |  |  |  |  |  | 
| 151 |  |  |  |  |  |  | 1; | 
| 152 |  |  |  |  |  |  | __END__ |