Tested and looked into the code for LWP::UserAgent. You are right, it does dictate the return code.

I agree with you, this is wrong, as it quietly modifies the true meaning of the response code.

Strictly speaking, in case that the host cannot be contacted or there is no response, undef shall be returned instead of a HTTP::Response object. A seperate field shall be used to give the reason why undef is returned.

But LWP::UserAgent does give you a hint in its _msg field, which you can use:
require LWP::UserAgent; use Data::Dumper; use strict; use warnings; my $res = LWP::UserAgent->new()->get('http://www.hopingthereisnosuchth +ing.com/ah'); print "return code = $res->{'_rc'}\n"; print "msg = $res->{'_msg'}\n";
Under the above case, you get:
return code = 500 msg = Can't connect to www.hopingthereisnosuchthing.com:80 (Bad hostna +me 'www.hopingthereisnosuchthing.com')

By the way, it is better not to use the internal field names like _rc and _msg. HTTP::Response has two methods defined to return those two fields, so just call code() and message().


In reply to Re: beyond LWP return code by pg
in thread beyond LWP return code by Anonymous Monk

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.