Feral_Shade has asked for the wisdom of the Perl Monks concerning the following question:

Hi,

I'm trying to use HTML::Form->parse() to extract the
form elements from an html file, but it is not working.
I am using the following code:
use URI use HTML::Form #folder where the file resides $base = URI->new("http://web.umr.edu/~msw/CS304/"); #form file to parse $file = "personal-info.HTML"; #try to parse the file, it only has 1 form $form = HTML::Form->parse($file, $base) || die("Parse failed"); . . . .
Every time I call the parse function it fails.
Am I doing something wrong? Thanks in advance.

Replies are listed 'Best First'.
Re: HTML::Form problem
by BrowserUk (Patriarch) on Apr 15, 2003 at 20:56 UTC

    The problem is that you are expecting HTML::Form to fetch the page and then parse it but this is not what it does.

    You will need to fetch the HTML yourself using LWP or similar, get it into a string and then give that string to HTML::Form::Parse(). The confusion is caused by the presence of the BaseURI parameter, but this is only used for resolving relative url's within the form. Try something like this.

    #! perl -slw use strict; use LWP::Simple; use Data::Dumper; use URI; use HTML::Form; #folder where the file resides my $base = URI->new("http://web.umr.edu/~msw/CS304/"); my $file = get('http://web.umr.edu/~msw/CS304/personal-info.HTML'); #try to parse the file, it only has 1 form my $form = HTML::Form->parse($file, $base) || die("Parse failed"); print Dumper $form;

    Ps. Nice XHTML:)


    Examine what is said, not who speaks.
    1) When a distinguished but elderly scientist states that something is possible, he is almost certainly right. When he states that something is impossible, he is very probably wrong.
    2) The only way of discovering the limits of the possible is to venture a little way past them into the impossible
    3) Any sufficiently advanced technology is indistinguishable from magic.
    Arthur C. Clarke.
      Yep, that worked perfectly.
      Thank you so much for your help.

      P.S. Gotta love Dreamweaver :)
Re: HTML::Form problem
by jasonk (Parson) on Apr 15, 2003 at 19:58 UTC

    It is failing because it needs to study harder.

    Seriously though, letting us know HOW it fails would let people actually suggest solutions.


    We're not surrounded, we're in a target-rich environment!
      A not so long, hard look at the code posted makes me guess it's throwing a Parse failed and dying. Obviously it won't help if the OP tells us that - so I didn't post asking for a clarification, either. Instead, I went looking at the POD for HTML::Form, but it doesn't appear to have an errstr method or similar facility. Unless someone finds something I missed, I don't think anyone can do more than guess blindly, here.

      Makeshifts last the longest.

      Sorry about that,
      Whenever I call the parse() function with the || die("")
      condition, die() is called and the script terminates, so I
      assumed that I had somehow made an error in using the function.
      I apologize if this is too ambiguous, but I don't know how to explain it any better.