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

Hai monks, Greets.

I just want to know, what difference it will make when I post using LWP and WWW::Mechanize. Below is what I experimented, which triggers this question.

Using LWP
use strict; use warnings; use LWP::UserAgent; use Data::Dumper; my $url = "http://192.168.1.5/src/Login_validate.php"; my $mech = LWP::UserAgent->new(); my $response=$mech->post($url,[ 'name' => "username", 'passwd' => "pas +sword"]); if(!$response->is_success()) { print "Failiure\n"; } print Dumper \$mech->get("http://192.168.1.5/src/allbooks.php"); __DATA__ In allbooks.php I checked if(isset($_SESSION['id']) and if not I print "Session expired" error m +essage. So when I executed the above code, it prints "Session expired" message +.
Using WWW::Mechaniize
use strict; use warnings; use WWW::Mechanize; use Data::Dumper; my $url = "http://192.168.1.5/src/Login_validate.php"; my $mech = WWW::Mechanize->new(); my $response=$mech->post($url,[ 'name' => "username", 'passwd' => "pas +swd"]); if(!$response->is_success()) { print "Failiure\n"; } print Dumper \$mech->get("http://192.168.1.5/src/allbooks.php"); __END__ The above code print the html contents, echoed by allbooks.php and I'm + not getting the session expired message.

So can any one please explain why LWP and WWW::Mechanize works differently in my case?

--Lakshmanan G.

The great pleasure in my life is doing what people say you cannot do.


Replies are listed 'Best First'.
Re: DIfference between LWP and WWW::Mechanize
by jettero (Monsignor) on Apr 10, 2009 at 13:22 UTC
    WWW::Mechanize @ISA LWP::UserAgent.

    There are quite a few differences. Mech automatically keeps track of the state of forms and cookies and things, more like a real browser. It literally is an LWP-UA, it just uses more of the features of the UA than LWP itself does (by default). For the full story, read the source. Mech is quite readable.

    Bottom line: If Mech is making your life easier ... then use it!!

    -Paul

Re: DIfference between LWP and WWW::Mechanize
by Anonymous Monk on Apr 10, 2009 at 13:26 UTC
    You can see for yourself if you add debugging
    $ua->add_handler("request_send", sub { shift->dump; return }); $ua->add_handler("response_done", sub { shift->dump; return });
      Can't locate object method "add_handler" via package "LWP::UserAgent"

      Vivek
      -- In accordance with the prarabdha of each, the One whose function it is to ordain makes each to act. What will not happen will never happen, whatever effort one may put forth. And what will happen will not fail to happen, however much one may seek to prevent it. This is certain. The part of wisdom therefore is to stay quiet.
        And?
        libwww-perl-5.825 > LWP::Debug
        perldoc LWP::Debug
Re: DIfference between LWP and WWW::Mechanize
by nagalenoj (Friar) on Apr 10, 2009 at 13:58 UTC
    www::mechanize is an excellent package, but I've never been able to get a version which passed the tests on the Windows or Linux boxes I've used. So I'll have to disagree with the comments that this should be a replacement for LWP::UserAgent. The fact that this package often (but not always) fails tests on certain platforms makes me reticent to require it for an application.

    source: Cpan reviews

    But, unfortunately I have not tested in non-unix machine.

      It's a dependency (HTTP::Server::Simple) that's failing. Mechanize only uses HSS for testing, and Mechanize isn't affected by whatever is causing the HSS test to fail. In other words, not a problem.

Re: DIfference between LWP and WWW::Mechanize
by isotope (Deacon) on Apr 10, 2009 at 16:35 UTC
    Use the cookie jar with LWP.


    --isotope