I have been involved in an LWP exercise recently. Rather than trivial link spidering, this involved form filling and POST method.

I was struck with the feeling that boiling down the form data is something that has probably been done many times over - but a search didn't find anything obvious.

I feel a CPAN module coming on (unless it's already been done and I've missed it), but I'm stuck on which namespace to use: HTML::Formdata, HTTP::Formdata, LWP::HTMLForm, thoughts please.

From the existing code that I have written, is a sub formdata, which takes the HTML page and form name as parameters. The form name is optional; if no form name is specified, the routine picks up the first form on the page.
sub formdata { my ($html,$formname) = @_; my $tp = HTML::TokeParser->new(\$html) or die "Bad HTML form"; while (my $form = $tp->get_tag('form')) { last if !$formname || ($form->[1]{name} eq $formname); $tp->get_text('/form'); } my @form; while (my $field = $tp->get_tag('input','select','textarea')) { my ($tag,$attr) = @$field; if ($tag eq 'textarea') { my $text = $tp->get_text('/textarea'); push @form,$attr->{name},$text; next; } if ($tag eq 'select') { my $selected; while (my $tok = $tp->get_token) { last if $tok->[-1] =~ m(/select)i; my ($typ,$tag,$att) = @$tok; next unless $typ eq 'S' && $tag eq 'option'; $selected = $att->{value} if exists $att->{selected}; } push @form,$attr->{name},$selected if defined $selected; next; } if ($attr->{type} =~ /hidden|password|text/) { push @form,$attr->{name},$attr->{value}; } if ($attr->{type} =~ /radio|checkbox/ && exists $attr->{checked}) { push @form,$attr->{name},$attr->{value}; } } @form; }
The sub returns a list of key/value pairs. Thinking about it, I realised that if the calling code turns it into a hash, this could lose any duplicate keys.

At this point, the light of recognition came on in my mind. This was a very familiar concept, that of a CGI object. I could make formdata return a CGI object or something inheriting from CGI, giving access to all the input fields via $form->param.

Besides being capable of being submitted via a normal POST of encoding type application/x-www-form-urlencoded, I would also like the code to be able to handle file uploads and encoding type multipart/form-data.

Has anything like this been done before? Please let me know if I am duplicating effort here.

rinceWind


In reply to LWP Form scraping by rinceWind

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.