Beefy Boxes and Bandwidth Generously Provided by pair Networks
No such thing as a small change
 
PerlMonks  

Re: Where is LWP::Simple::get error information stored?

by BrowserUk (Pope)
on Sep 27, 2012 at 21:45 UTC ( #996081=note: print w/replies, xml ) Need Help??


in reply to Where is LWP::Simple::get error information stored?

how can I programmatically determine errors like this from LWP?

The LWP::Simple POD for get explains:

You will not be able to examine the response code or response headers (like 'Content-Type') when you are accessing the web using this function. If you need that information you should use the full OO interface (see the LWP::UserAgent manpage).

However, there is (kinda) a way around it. If you look at getprint(), it returns an HTTP error code, but of course, you probably want the content in your program, not sent to stdout. So, the trick I've used in the past is to open a ramfile and select it before calling getprint(). That way you get the content into a variable and the status code:

my $content; open RAM, '>', \$content; my $stdoutsaved = select; select *RAM; my $status = getprint( $url ); select $stdoutsaved; close RAM; ## Now the status is in $status and the content in $content.

Quite why get() wasn't implement to return both:

my( $status, $content ) = get( $url );

I guess we'll never know, but it is unlikely to change now.


With the rise and rise of 'Social' network sites: 'Computers are making people easier to use everyday'
Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
"Science is about questioning the status quo. Questioning authority".
In the absence of evidence, opinion is indistinguishable from prejudice.

RIP Neil Armstrong

Replies are listed 'Best First'.
Re^2: Where is LWP::Simple::get error information stored?
by ajam (Acolyte) on Sep 28, 2012 at 01:29 UTC

    Thanks for the explanation and an example work around. For the sake of maintainability though I think I will avoid using this crafty method.

      For the sake of maintainability though I think I will avoid using this crafty method.

      It is not at all clear to me why you think it need be a maintenance problem. Just wrap it over in a function something like:

      sub getWithStatus { my $url = shift; my $content; open my $RAM, '>', \$content; my $stdoutsaved = select; select *$RAM; my $status = getprint( $url ); select $stdoutsaved; close $RAM; return $status, $content; } ... my( $status, $content ) = getWithStatus( $url ); if( $status ne RC_OK ) { die "Get $url failed with HTTP status: $status"; } ...

      With the rise and rise of 'Social' network sites: 'Computers are making people easier to use everyday'
      Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
      "Science is about questioning the status quo. Questioning authority".
      In the absence of evidence, opinion is indistinguishable from prejudice.

      RIP Neil Armstrong

      Social/div

        Good point.

        For some reason I envisioned just pasting that inline. Obviously it is appropiate to put in a function.

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://996081]
help
Chatterbox?
and all is quiet...

How do I use this? | Other CB clients
Other Users?
Others contemplating the Monastery: (7)
As of 2018-07-19 15:48 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    It has been suggested to rename Perl 6 in order to boost its marketing potential. Which name would you prefer?















    Results (411 votes). Check out past polls.

    Notices?