I beseech the monks for wisdom and insight. I'm trying to learn how to use WWW::Mechanize to submit data to a form, but I'm clearly doing something wrong and try as I might I just can't seem to find the error. As a test case I'm using our beloved http://search.cpan.org and searching for 'mechanize'. Here's the code:

use strict; use warnings; use WWW::Mechanize; use HTTP::Cookies; use Data::Dumper; my $url = 'http://search.cpan.org'; my $searchstring = 'mechanize'; my $mech_obj = WWW::Mechanize->new(); $mech_obj->get( $url ); if( not $mech_obj->success() ) { print "Could not retrieve page:\n"; print $mech_obj->content(); die; } print "all forms:\n", Dumper( [ $mech_obj->forms ] ); $mech_obj->form_number( 1 ); $mech_obj->field( 'query' => $searchstring ); print "current form:\n", Dumper( $mech_obj->current_form() ); $mech_obj->submit(); print "content:\n", $mech_obj->content();

And here is the output:

all forms: $VAR1 = [ bless( { 'inputs' => [ bless( { 'value' => '', 'size' => '35', 'type' => 'text', 'name' => 'query' }, 'HTML::Form::TextInput' ), bless( { 'seen' => [ 1, 0, 0, 0 ], 'menu' => [ 'all', 'module', 'dist', 'author' ], 'current' => 0, 'type' => 'option', 'name' => 'mode' }, 'HTML::Form::ListInput' ), bless( { 'value' => 'CPAN Search', 'type' => 'submit' }, 'HTML::Form::SubmitInput' ) ], 'extra_attr' => { 'class' => 'searchbox', 'name' => 'f' }, 'enctype' => 'application/x-www-form-urlencoded', 'method' => 'GET', 'action' => bless( do{\(my $o = 'http://search.cpan +.org/search')}, 'URI::http' ) }, 'HTML::Form' ) ]; current form: $VAR1 = bless( { 'inputs' => [ bless( { 'value' => 'mechanize', 'size' => '35', 'type' => 'text', 'name' => 'query' }, 'HTML::Form::TextInput' ), bless( { 'seen' => [ 1, 0, 0, 0 ], 'menu' => [ 'all', 'module', 'dist', 'author' ], 'current' => 0, 'type' => 'option', 'name' => 'mode' }, 'HTML::Form::ListInput' ), bless( { 'value' => 'CPAN Search', 'type' => 'submit' }, 'HTML::Form::SubmitInput' ) ], 'extra_attr' => { 'class' => 'searchbox', 'name' => 'f' }, 'enctype' => 'application/x-www-form-urlencoded', 'method' => 'GET', 'action' => bless( do{\(my $o = 'http://search.cpan.o +rg/search')}, 'URI::http' ) }, 'HTML::Form' ); Unexpected field value http://search.cpan.org at (eval 5) line 1

Based on the output I know the proper form was selected and the query string was assigned to the proper field. I don't understand why I get an "Unexpected field value" error when the form is submitted, but I know there has to be a simple explanation for this.

Could someone please run my code and see if it works on a different system? That would at least tell me if the problem is in my code or if it is something else (e.g., the WWW::Mech installation, etc).

Thanks in advance!


In reply to "Unexpected field value" error with WWW::Mechanize by bobf

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.