l i n u x - u s e r s - g r o u p - o f - d a v i s
L U G O D
 
Next Meeting:
November 4: Social gathering
Next Installfest:
TBD
Latest News:
Oct. 10: LUGOD Installfests coming again soon
Page last updated:
2001 Dec 30 16:59

The following is an archive of a post made to our 'vox-tech mailing list' by one of its subscribers.

Report this post as spam:

(Enter your email address)
Re: [vox-tech] How to parse and submit a form in perl
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: [vox-tech] How to parse and submit a form in perl



On Thu, Dec 21, 2000 at 10:03:54PM -0600, Jay wrote:
> Sorry to post a perl question, but I know there are some of you out there who
> know perl.
> 
> I've been working on this all day and while I'm learning stuff I've got about 10
> lines of code (and only 8 work) to show for my effort.
> 
> I swear I'm RTFMs, perldoc, searching maillists... but things are not documented
> for the unfamiliar (i.e. beginner, i.e stupid).

Oreilly's Learning Perl (the Llama Book) is supposed to be good; but of course
it's not free either.  I'm currently trying to remedy the situation by adapting
the notes from my Perl series at Lugod into a Free (FDL'd) book, which will
of course be available over the internet.  It's far from complete however, as
I have not had much time to dedicate to it.

> 
> (since I'm drowning) Maybe it will be easier if explain what I want to do.
> 
> I want to go to a web page, I'll supply the URL as command line arg, it will
> look like:
> 
> https://some.securesite.com/cgi/purchasepage.asp?product_id=700314996C26550&prod
> uct_code='
> 
> This will return a page composed of 3 forms, I'm concerned with the 2nd form. On
> this form there are a bunch of hidden input fields and 3 fields the user is
> supposed to enter (1st is text field (Quantity), second is a select list
> (state), 3rd is a radio button (preferred shipper (UPS, Fedex)), and a submit
> button.
> 
> I want to set the quantity, state, and shipper, and submit the form (along with
> all the hidden values).  Then retrieve then next form that is returned (from the
> submission of the previous form) and start the process over (but with different
> inputs) working my way all the way thru the purchase process.
> 
> I've been looking at HTML::Parser, HTML::Form, HTML::Tokeparser, they all seem
> to do some of what I want.  But I can't figure out how to use them.
> 

It would be much easier to generate the HTML page from within the perl script.
HTML::Template is a great way to do this, but you can also embed it in the
code directly.

> Could someone point me in the right direction (maybe with some examples).  I've
> got this code so far (get ready to laugh):
> 
> #!/usr/bin/perl
> 
> use strict;
> use LWP::UserAgent;
> use HTML::Form;
> 
> # Extract the program name from the incarnation
> # of the program
> my @path = split(/\//,$0);
> my $programName = $path[@path -1];

A clearer equivelant to [@path -1] would be [$#path].

> 
> # Get website from command line
> my $url = $ARGV[0];
> 
> my $uri =
> URI->new('https://some.securesite.com/cgi/purchasePage.asp?product_id=700314996C
> 26550&product_code=');
> 
> my $ua = new LWP::UserAgent;
> $ua->timeout(60);
> $ua->agent("$programName/0.1 ". $ua->agent);
> 
> my $request = new HTTP::Request('GET' , $uri );
> my $response = $ua->request($request);
> 
> # it works up till here #
> # this stuff is new
> my @forms = HTML::Form->parse($uri->path,$uri->host);

I have note used HTML::Form before; however, judging from the
manpage, you seem to be supplying the wrong arguments, which are:

$html_document, $base_uri

The second argument is just for the sake of resolving relative URIs.
So the first argument should probably be $response->content,
and the second should be $uri->path.  Hope that helps.

> 
> #print $forms[1]->action;
> print $forms[1]->inputs;
> 
> exit(0);

This exit is completely unnecessary.

> 
> 
> 
> Jay Strauss
> jstrauss@bazillion.com
> (h) 773.935.5326
> (c) 312.617.0264
> 

Hope this helps!

Micah


LinkedIn
LUGOD Group on LinkedIn
Sign up for LUGOD event announcements
Your email address:
facebook
LUGOD Group on Facebook
'Like' LUGOD on Facebook:

Hosting provided by:
Sunset Systems
Sunset Systems offers preconfigured Linux systems, remote system administration and custom software development.

LUGOD: Linux Users' Group of Davis
PO Box 2082, Davis, CA 95617
Contact Us

LUGOD is a 501(c)7 non-profit organization
based in Davis, California
and serving the Sacramento area.
"Linux" is a trademark of Linus Torvalds.

Sponsored in part by:
O'Reilly and Associates
For numerous book donations.