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

I've been trying to familiarize myself with using WWW::Mechanize.
I'll post my following code and maybe someone can help me sort it out!
use strict; use WWW::Mechanize; use HTTP::Cookies; my $mech = WWW::Mechanize->new( agent =>"Mozilla/5.0 (X11; U; Li +nux i68+6; en-US; rv:1.7) Gecko/20040918 Firefox/0.9.3 "); my $i = 0; my $usr = "username"; my $pw = "password"; $mech->get("http://www.http://www.soundclick.com/mysoundclick"); $mech->cookie_jar( HTTP::Cookies->new() ); if( $mech->content() =~ /CFForm_1/ ){ $mech->input_name("email"); $mech->input_name("password"); $mech->field( email => $usr ); $mech->field( password => $pw ); $mech->click(); if ($mech->success() ) { if ( $mech->content() =~ /My personal member profile/ ) { print "User $usr logged in successfully!\n" ; } else { print " User $usr was unable to log in successfully!\n"; } } }
When the script is executed, no output whatsoever.
What gives?

Replies are listed 'Best First'.
Re: Using WWW::Mechanize
by Fletch (Bishop) on Apr 26, 2007 at 19:10 UTC

    If it's not a paste-o, the line:

    $mech->get("http://www.http://www.soundclick.com/mysoundclick");

    jumps out as a likely candidate for causing misbehavior. Also, I believe setting the cookie_jar isn't necessary with mechanize (although if it is it'd be best to do so before making a request rather than after).

    Update: Just to clarify, I believe if you want cookies within a session to be kept you don't need to do anything; if you want them to persist between invocations you need to set it to one of the on-disk cookie jar implementations.

Re: Using WWW::Mechanize
by akho (Hermit) on Apr 26, 2007 at 19:17 UTC
    The ->get URL is wrong.

    Upd Sorry, that was probably a typo; there is a different problem.

    First: $mech->get returns the redirect page, and that does not have the necessary form. Use https://www.soundclick.com/community/memberlogin.cfm?action=logoutdone&email=&ErrorMessage=Please%20log%20in as the URL.

    Second: WWW::Mechanize does not have an input_name method, only form_name.

    This seems to work:

    use strict; use WWW::Mechanize; use HTTP::Cookies; my $mech = WWW::Mechanize->new( agent =>"Mozilla/5.0 (X11; U; Li +nux i686; en-US; rv:1.7) Gecko/20040918 Firefox/0.9.3 "); my $i = 0; my $usr = "username"; my $pw = "password"; $mech->get("https://www.soundclick.com/community/memberlogin.cfm?actio +n=logoutdone&email=&ErrorMessage=Please%20log%20in"); $mech->cookie_jar( HTTP::Cookies->new() ); if( $mech->content() =~ /CFForm_1/ ){ $mech->form_name("CFForm_1"); $mech->field( email => $usr ); $mech->field( password => $pw ); $mech->click(); if ($mech->success() ) { if ( $mech->content() =~ /My personal member profile/ ) { print "User $usr logged in successfully!\n" ; } else { print " User $usr was unable to log in successfully!\n"; } } }
      I'm still receiving no output whatsoever on the cmd prompt when I launch it.

        If you're anything like me, it didn't work for me either. I added a last line:

        else { print $mech->content }

        ... and got the following output:

        LWP will support https URLs if the Crypt::SSLeay module is installed. More information at <http://www.linpro.no/lwp/libwww-perl/README.SSL>.

        Try going there, reading that, and installing Crypt::SSLeay.



        --chargrill
        s**lil*; $*=join'',sort split q**; s;.*;grr; &&s+(.(.)).+$2$1+; $; = qq-$_-;s,.*,ahc,;$,.=chop for split q,,,reverse;print for($,,$;,$*,$/)
Re: Using WWW::Mechanize
by eXile (Priest) on Apr 27, 2007 at 14:19 UTC
    Not a solution, but a debug tool:

    WWW::Mechanize::Shell lets you do all kinds of Mechanize stuff in a command shell, and spit whatever you did out as a valid perl mechanize script.