views:

79

answers:

3

Hello! Are both of these versions OK or is one of them to prefer?

#!/usr/bin/env perl
use strict;
use warnings;
use WWW::Mechanize;

my $mech = WWW::Mechanize->new();
my $content;

# 1
$mech->get( 'http://www.kernel.org' );
$content = $mech->content;
print $content;

# 2
my $res = $mech->get( 'http://www.kernel.org' );
$content = $res->content;
print $content;
+3  A: 

They are both acceptable. The second one seems cleaner to me because it returns a proper HTTP::Response object which you can query and call methods on, and also means that if you make another Mechanize request, you'll still have access to the old HTTP response. With your first approach, each time you make a request, the content method will change to something new, which sounds error-prone.

Btw, for either method, you should check $response->is_success or $mech->success before accessing the content, as the request may have failed.

rjh
From the perldoc: standalone WWW::Mechanize instances have autocheck turned on. However, if WWW::Mechanize is subclassed, it's off.
eugene y
Oh wow, the last time I used Mechanize, that option didn't exist. That was in 2008... now I feel old :(
rjh
I added autocheck because of how staggeringly common it was for people to come into the #perl IRC channel and complain that $mech->content was empty, because they'd not bothered to check $mech->success. Now, the most common case is the default.
Andy Lester
+2  A: 

The content() method is sometimes more convenient:

$mech->content(...)

Returns the content that the mech uses internally for the last page fetched. Ordinarily this is the same as $mech->response()->content(), but this may differ for HTML documents if "update_html" is overloaded, and/or extra named arguments are passed to content():

$mech->content( format => 'text' )

Returns a text-only version of the page, with all HTML markup stripped. This feature requires HTML::TreeBuilder to be installed, or a fatal error will be thrown.

$mech->content( base_href => [$base_href|undef] )

Returns the HTML document, modified to contain a mark-up in the header. $base_href is $mech->base() if not specified. This is handy to pass the HTML to e.g. HTML::Display.

eugene y
$mech->content( format => 'text' ) doesn't work with my machine.(HTML::TreeBuilder is installed)
sid_com
Check your version, you need WWW::Mechanize 1.05_03 or higher (released in 2004)
rjh
OK it works, maybe I was trunk when I tried it. But was there recently a WWW::Mechanize-version where this feature didn't work with perl 5.10.0 or 5.10.1?
sid_com
+1  A: 

$mech->content is specifically there so you can bypass having to get the result response. The simpler it is, the better.

Andy Lester