To quote the docs:
If LWP is unable to fetch the URL, or the response is not HTML (as determined by
content_is_html
in HTTP::Headers), thennew_from_url
dies, and the HTTP::Response object is found in$HTML::TreeBuilder::lwp_response
.
Try this:
use strict;
use warnings;
use HTML::TreeBuilder 5; # need new_from_url
use Try::Tiny;
my $url="http://some_url.com/blahblah" ;
my $p = try { HTML::TreeBuilder->new_from_url($url) };
unless ($p) {
my $response = $HTML::TreeBuilder::lwp_response;
if ($response->is_success) {
warn "Content of $url is not HTML, it's " . $response->content_type . "\n";
} else {
warn "Couldn't get $url: ", $response->status_line, "\n";
}
}