File Coverage

blib/lib/HTML/Form/ForceValue.pm
Criterion Covered Total %
statement 30 30 100.0
branch 3 4 75.0
condition 2 3 66.6
subroutine 7 7 100.0
pod n/a
total 42 44 95.4


line stmt bran cond sub pod time code
1 2     2   158650 use strict;
  2         7  
  2         201  
2 2     2   16 use warnings;
  2         5  
  2         457  
3             package HTML::Form::ForceValue;
4             # ABSTRACT: who cares what values are legal, anyway?
5             $HTML::Form::ForceValue::VERSION = '0.009';
6             # =head1 SYNOPSIS
7             #
8             # use Test::WWW::Mechanize tests => 5;
9             # use HTML::Form::ForceValue;
10             #
11             # my $mech = WWW::Mechanize->new;
12             #
13             # # We're going to test our form.
14             # $mech->get_ok("http://cgi.example.com/form");
15             #
16             # $mech->set_fields(
17             # name => 'Crazy Ivan',
18             # city => 'Vladivostok',
19             # );
20             #
21             # # What if insane bot tries to claim it's from USSR?
22             # $mech->form_name("user_info")->find_input("country")->force_value("su");
23             #
24             # $mech->submit;
25             #
26             # =head1 DEPRECATION NOTICE
27             #
28             # As of C 5.817, HTML::Form has a strict mode, which restricts form
29             # values to the options given. Without strict mode, values may be set to
30             # anything you like, making this module unnecessary. It remains on the CPAN for
31             # use by those who choose not to upgrade their LWP, but in general this code is
32             # now obsolete.
33             #
34             # =head1 DESCRIPTION
35             #
36             # L is a very useful module that provides objects to
37             # represent HTML forms. They can be filled in, and the filled-in values can be
38             # converted into an HTTP::Request for submission to a server.
39             #
40             # L makes this even easier by providing a very
41             # easy to automate user agent that provides HTML::Form objects to represent
42             # forms. L hangs some testing
43             # features on Mech, making it easy to automatically test how web applications
44             # behave.
45             #
46             # One really important thing to test is how a web application responds to invalid
47             # input. Unfortunately, HTML::Form protects you from doing this by throwing an
48             # exception when an invalid datum is assigned to an enumerated field.
49             # HTML::Form::ForceValue mixes in to HTML::Form classes to provide C
50             # methods which behave like C, but will automatically add any invalid
51             # datum to the list of valid data.
52             #
53             # =cut
54              
55             sub import {
56 2     2   25 my $class = shift;
57 2         14 HTML::Form::ForceValue::Form->import(@_);
58 2         697 HTML::Form::ForceValue::Form::Input->import(@_);
59             }
60              
61             package HTML::Form::ForceValue::Form;
62             $HTML::Form::ForceValue::Form::VERSION = '0.009';
63 2         32 use Sub::Exporter 0.960 -setup => {
64             into => 'HTML::Form',
65             exports => [ qw(force_value) ],
66             groups => [ default => [ '-all' ] ],
67 2     2   18035 };
  2         63032  
68              
69             sub force_value {
70 1     1   648 my ($self, $name, $value) = @_;
71              
72 1         5 my $input = $self->find_input($name);
73              
74 1 50       8257 unless ($input) {
75 1         25 $input = HTML::Form::ListInput->new(
76             type => 'option',
77             name => $name,
78             menu => [ { value => $value, name => $value } ],
79             current => 0,
80             multiple => 1,
81             );
82              
83 1         3712 $input->add_to_form($self);
84             }
85              
86 1         346 $input->force_value($value);
87             }
88              
89             package HTML::Form::ForceValue::Form::Input;
90             $HTML::Form::ForceValue::Form::Input::VERSION = '0.009';
91 2         17 use Sub::Exporter -setup => {
92             into => 'HTML::Form::Input',
93             exports => [ qw(force_value) ],
94             groups => [ default => [ '-all' ] ],
95 2     2   1308 };
  2         4  
96              
97             sub force_value {
98 2     2   1166 my ($self, $value) = @_;
99 2         14 my $old = $self->value;
100 2         25 eval { $self->value($value); };
  2         37  
101 2 100 66     359 if ($@ and $@ =~ /Illegal value/) {
102 1         2 push @{$self->{menu}}, { name => $value, value => $value };
  1         6  
103 1         4 return $self->value($value);
104             }
105 1         9 return $old;
106             }
107              
108             # =head1 WARNING
109             #
110             # This implementation is extremely crude. This feature should really be in
111             # HTML::Form (in my humble opinion), and this module should cease to exist once
112             # it is. In the meantime, just keep in mind that I spent a lot more time
113             # packaging this than I did writing it. I
114             #
115             # =cut
116              
117             1;
118              
119             __END__