perlquestion
talexb
<p>I am using the most excellent [cpan://Test::WWW::Mechanize::CGIApp] to test my web application, and I appear to have run into a snag.</p>
<p>I want to set some values in a form, but since the form values are stored in a hash, the values that I set may be superceded by the default values. So my plan was to get all of the form's key/value pairs and copy everything over to a new list, except for the keys that I want to change.</p>
<p>I did this by getting the form from Mech, then extracting the key/value pairs from the form ..
<c>
my $form = $mech->form_number(1);
my @keyValuePairs = $form->form();
</c>
.. then I looped through the key/value pairs, transferring over everything except the fields I'm going to be monkeying with ..
<c>
my @newKeyValuePairs;
my %testFields =
('p_customer' => 1, 'p_name' => 1, 'new_users' => 1);
while (@keyValuePairs) {
my ($key, $value) =
(shift @keyValuePairs, shift @keyValuePairs);
push (@newKeyValuePairs, $key, $value)
unless (exists($testFields{$key}));
}
</c>
.. and finally I used Mech's <c>set_fields</c> method to set the form's fields.
<c>
$mech->set_fields(
@newKeyValuePairs,
p_customer => 'Internal',
p_name => $thisProject,
new_users =>
join ("\n", @{$newProjects{$thisProject}}),
);
</c>
</p>
<p>This produced the following errors:
<c>
Input 'rm' is readonly at /usr/lib/perl5/vendor_perl/5.8.8/WWW/Mechanize.pm line 1233
Input 'member' is readonly at /usr/lib/perl5/vendor_perl/5.8.8/WWW/Mechanize.pm line 1233
Input 'p_id' is readonly at /usr/lib/perl5/vendor_perl/5.8.8/WWW/Mechanize.pm line 1233
</c>
Is there a better way to solve this problem? I haven't tried just setting the field values -- I assume that because it's a hash, it will work sometimes and other times will not.</p>
<p><b>Update:</b> Aha -- of course, the readonly variables are the variables that are 'hidden' in the original page. I don't feel like I'm that much closer to a solution, however.</p>
<p><b>Update 2:</b> I have tried calling <c>set_field</c> without all of the cleverness shown above, and it seems to work properly .. so perhaps this effort was in vain. I certainly am learning a lot about using [cpan://Test::WWW::Mechanize::CGIApp], in any case, and that's definitely a Good Thing. Thanks to all the monks who read this node.</p>
<div class="pmsig"><div class="pmsig-131279">
<p>Alex / [talexb] / Toronto</p>
<p><small>"<a href="http://www.groklaw.net">Groklaw</a> is the open-source mentality applied to legal research" ~ Linus Torvalds</small></p>
</div></div>