Zachary Zachary - 7 months ago 29
Perl Question

Can't call method "header" on an undefined value at WWW/Mechanize.pm line 2566

I am just doing a testing using

WWW::Mechanize
module on Facebook, when I try to run the code below, it return me an error


Can't call method "header" on an undefined value at C:/Strawberry/perl/vendor/lib/WWW/Mechanize.pm line 2566.


#!/usr/bin/perl -w
use WWW::Mechanize;
my $mech = WWW::Mechanize->new();
# Connect to server
$mech->get( "https://www.facebook.com" );
$mech->success or die $mech->response->status_line;

# Log into server

$mech->field('email', 'xxx@xxx.com');
$mech->field('pass', 'xxxxxxx');
$mech->click_button(value => 'Log In');

Answer

Your page is opening in some other language other than English. That's why you are getting that error. If you will open the page in English forcefully then the error will disappear. Try below address:

$mech->get( "https://en-gb.facebook.com/" );

or, you may click directly on instance of HTML::Form::SubmitInput obtained using this:

$mech->current_form()->find_input( undef, 'submit');

or, as there is only one click button in the form you can use click with no arguments.

$mech->click() 

or as suggested by @Borodin you can directly use(as the email and password field aren't translated):

$mech->submit_form( with_fields => { 
                        email => 'xxx@xxx.com', 
                        pass => 'xxxxxxx' 
                    }
                  );