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

Are both of these versions OK or one of them prefers?

#!/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
source share
3 answers

The content () method is sometimes more convenient:

$mech->content(...)

Returns the content that the fur uses internally for the last page. This is usually the same as $ mech-> response () → content (), but this can be different for HTML documents if "update_html" is overloaded and / or additional named arguments are passed to content ():

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

Returns the text version of the page, with all HTML markup torn. This function requires the installation of HTML :: TreeBuilder or a fatal error.

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

HTML-, , . $base_href - $mech- > base(), . HTML, . HTML:: Display.

+2

. , HTTP::Response , , , if Mechanize, HTTP-. , , content - , .

Btw, $response->is_success $mech->success , .

+3

$ mech-> is here on purpose, so you can get around to get an answer to the result. The simpler the better.

+1
source

Source: https://habr.com/ru/post/1738785/


All Articles