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

Dear Monks, I am very new to Perl, and I have looked all over for solutions and reasons why my code isn't working. I wanted to write a program that would get an anagram-finding website, plug in the user input, and return the anagrams. I have written this code:

#!/usr/bin/perl use strict; use diagnostics; use WWW::Mechanize::Firefox; my $mech = WWW::Mechanize::Firefox->new(); print "What word would you like to know the anagrams of?\n"; my $word = <STDIN>; $word = chomp( $word ); $mech->get( 'http://www.wordsmith.org/anagram/index.html' ) or die "Un +able to access page.\n"; $mech->field( 'anagram', $word ); $mech->submit; wait until $mech->success(); print $mech->content;

I cannot remember all the different options that I have tried to fix the problem, but the latest one was:

 $mech->click_button(name => 'Get anagrams');

No matter what I put on line 14 (the submit line), there is a problem. There seems to be less of a problem when I just use "submit", in which case, Perl returns:

 I don't know which form to submit, sorry.

I have tried to do it by numbering the "submit" options, but to no avail. I just can't seem to submit the page. The source code for the page is:

<form action="anagram.cgi" name="inputform" onSubmit="return valid_in +put(anagram)"> <table border="0" bgcolor="#DDEEFF" bordercolor="#DDEEFF" cellpadding= +"8" cellspacing="0"> <tr> <td>Find anagrams for <input name="anagram" size="16" type="text">< +/td> <td><input value="Get anagrams" type="submit"><input name="t" value +="1000" type="hidden"><input name="a" value="n" type="hidden"></td> </tr>

Maybe I'm just in over my head, but I've been searching for days, and I would very much appreciate any help you could give. Thank you!

Joie

Replies are listed 'Best First'.
Re: WWW::Mechanize::Firefox question
by Corion (Patriarch) on Oct 15, 2012 at 07:57 UTC
    $mech->click_button(name => 'Get anagrams');

    is the thing that should work. You don't tell me how it fails for you, so I can't advise further on that front.

    If WWW::Mechanize::Firefox tells you

    I don't know which form to submit, sorry.

    ... that means that there is more than one form on the page. Use ->form_name or ->form_number to set the "current form", as documented in WWW::Mechanize::Firefox.

      Thank you for your reply. I played around with it some more and looked at the form information on the page. I realized that there were 2 forms, I amended the code to:

      #!/usr/bin/perl use strict; use diagnostics; use warnings; use WWW::Mechanize::Firefox; my $mech = WWW::Mechanize::Firefox->new(); print "What word would you like to know the anagrams of?\n"; my $word = <STDIN>; $word = chomp( $word ); $mech->get( 'http://www.wordsmith.org/anagram/index.html' ) or die "Un +able to access page.\n"; $mech->form_number(2); $mech->field( 'anagram', $word ); $mech->submit_form; wait until $mech->success(); print $mech->content;

      Now it has no error messages; however, it seems to be printing the original website, rather than the results of my query.

         wait until $mech->success(); ?? That seems like an error