Reputation: 3752
I am following guidelines
If you want to submit a file and get its content from a scalar rather than a file in the filesystem, you can use:
$mech->submit_form(with_fields => { logfile => [ [ undef, 'whatever', Content => $content ], 1 ] } );
from WWW::Mechanize documentation
My code to submit a file
$mech->submit_form(with_fields =>
{ logfile => [ [ undef, "import_codes.xlsx", Content => $file_dir ], 1 ] });
Its failing with exception;
Can't call method "value" on an undefined value at /usr/local/share/perl/5.18.2/WWW/Mechanize.pm line 1568.
at /usr/local/share/perl/5.18.2/WWW/Mechanize.pm line 1568.
WWW::Mechanize::set_fields('WWW::Mechanize=HASH(0xf51b040)', 'logfile', 'ARRAY(0xf71e6dc)') called at /usr/local/share/perl/5.18.2/WWW/Mechanize.pm line 1948
WWW::Mechanize::submit_form('WWW::Mechanize=HASH(0xf51b040)', 'form_name', 'inputform', 'fields', 'HASH(0xf71e920)')
Code snippet of Mechanize.pm from line 1560 to 1575.
sub set_fields {
my $self = shift;
my %fields = @_;
my $form = $self->current_form or $self->die( 'No form defined' );
while ( my ( $field, $value ) = each %fields ) {
if ( ref $value eq 'ARRAY' ) {
$form->find_input( $field, undef,
$value->[1])->value($value->[0] );
}
else {
$form->value($field => $value);
}
} # while
}
Upvotes: 1
Views: 265
Reputation: 54323
It looks like you are using the wrong field name for the file input field in the form. (Emphasis mine).
If you want to submit a file and get its content from a scalar rather than a file in the filesystem, you can use:
vvvvvvv $mech->submit_form(with_fields => { logfile => [ [ undef, 'whatever', Content => $content ], 1 ] } );
That logfile
is the name attribute of the input field that you want the file content to be placed in. In their example it's logfile, but in your real form on the website you're trying to submit it's likely something else.
$mech->submit_form
calls $mech->form_with_fields
. The docs for that method say:
Returns undef if no form is found.
When it then does set_fields
it will fail because undef
was returned.
Use the correct field name and it should work.
Upvotes: 3