perl LWP::UserAgent gives a cryptic error message

566 views Asked by At

Here's the code:

$vizFile ='https://docs.recipeinvesting.com/t.aaaf.html'; 
my $ua = LWP::UserAgent->new;
$ua->timeout(10);
$ua->env_proxy;
my $response = $ua->get($vizFile);
if ($response->is_success) {print $response->decoded_content;}
else {print"\nError= $response->status_line]n";}

I get the message:

Error= HTTP::Response=HASH(0x3a9b810)->status_line]n

The url works fine if I put it in a browser.

This was working consistently (with plain http, using LWP::Simple), until the site made some changes.

Could it be the https that's making the difference?

Is there some way to get a less cryptic error message?

2

There are 2 answers

2
ikegami On BEST ANSWER

You can't put code in string literals and expect it to get executed. Sure, you can place variables for interpolation, but the making method calls falls on the other side of what's supported.

Replace

print"\nError= $response->status_line]n";

with

print "\nError= " . $response->status_line . "\n";

or

use feature qw( say );

say "\nError= " . $response->status_line;

This will print the status line as desired.

0
Polar Bear On

Please see following demo code, it is encouraged to include use strict; and use warnings; in the code what would assist you to avoid many potential problems

use strict;
use warnings;
use feature 'say';

use LWP::UserAgent;

my $url ='https://docs.recipeinvesting.com/t.aaaf.html'; 
my $ua  = LWP::UserAgent->new;

$ua->timeout(10);
$ua->env_proxy;

my $response = $ua->get($url);

if( $response->is_success ){
    say $response->decoded_content;
} else {
    die $response->status_line;
}

Documentation: LWP::UserAgent