shinjuo
shinjuo

Reputation: 21012

Filling out a form in perl using HTML::Form

I am trying to fill out a form on a separate page and return the data, but my hosting does not support the www::mechanize module. I saw that HTML::FORM would accomplish the same thing but I am getting the error

Can't call method "value" on an undefined value at G:\Programming\test.pl line 12.

Here is the code I have been testing with

use strict;
use LWP::Simple;
use LWP::UserAgent;
use HTML::Form;

use HTML::Strip;
my $base_uri = "UTF-8";
my $url = 'xxxxxxx';

 my $form = HTML::Form->parse($url, $base_uri);
 $form->value(Zip => '74014');
 my $ua = LWP::UserAgent->new;
 my $response = $ua->request($form->click);

Upvotes: 0

Views: 811

Answers (1)

gangabass
gangabass

Reputation: 10666

The first argument for parse is HTML document itself but not the URL.

The required arguments is the HTML document to parse ($html_document) and the URI used to retrieve the document ($base_uri). The base URI is needed to resolve relative action URIs. The provided HTML document should be a Unicode string (or US-ASCII).

So you need to first get this document (with LWP::UserAgent) and parse response:

 my $response = $ua->get($url);

 if ($response->is_success) {
     my $form = HTML::Form->parse($response->decoded_content, $base_uri);
     ...
 }
 else {
     die $response->status_line;
 }

Upvotes: 2

Related Questions