sid_com
sid_com

Reputation: 25117

What is the preferred method of accessing WWW::Mechanize responses?

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;

Upvotes: 3

Views: 228

Answers (3)

Andy Lester
Andy Lester

Reputation: 93666

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

Upvotes: 1

Eugene Yarmash
Eugene Yarmash

Reputation: 149776

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.

Upvotes: 2

rjh
rjh

Reputation: 50284

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.

Upvotes: 3

Related Questions