开发者

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;


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.


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.


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

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜