3

I am using LWP::UserAgent like below

my $ua = LWP::UserAgent->new;

my $response = $ua->request ( ...);

if ( $response->is_success() )
{
...
}
print $response->is_success();

Problem I am facing is that is_success() is returning blank. I was expecting 1 (TRUE) or 0 (FALSE). What am I doing wrong here? Is print statement right?

  • 1
    Your code looks fine to me. Is your web server defiantly returning a HTTP status code for the given request (verify using something like LiveHeaders for Firefox)? What is the actual request or is it to a private server? What does `$response->status_line` give you? – didster Oct 15 '12 at 10:21
  • @didster Does a blank mean `FALSE` here? –  Oct 15 '12 at 10:33
  • @Drt, yes. Not returning anything is usual method in Perl to signify false, though 0 or empty string would work too. – Oleg V. Volkov Oct 15 '12 at 10:38
  • 1
    Anything other than true means false ;o). I wouldn't pay too much attention to if a library in perl returns 0 or something that also means false such as an empty string. They are not consistent for one thing - many use `undef` for false for example. Its not returning true which is the main thing. The is_success boils down to a call in `HTTP::Status` like this `{ $_[0] >= 200 && $_[0] < 300; }` which basically says is the response code between 200 - 299. – didster Oct 15 '12 at 10:38
  • @didster, do I remember correctly that if you leave default redirection on, then 302 followed by 200 at new location will be true for `is_success` too? – Oleg V. Volkov Oct 15 '12 at 10:47
  • 1
    Can you try `is_error()` instead just to check if it has error or `status_line()` – jchips12 Oct 15 '12 at 10:48
  • @didster `$response->status_line` worked for me. Thanks. You can put it as an answer. –  Oct 15 '12 at 11:44
  • @didster and @jchips : `$response->status_line` actually returned `500 Can't Connect to database`. With `$response->is_success()`, I was unable to understand the response from db. Thanks –  Oct 16 '12 at 08:34

3 Answers3

5

Not returning anything is correct and usual way in Perl to return false result from function, don't expect literal 0 number when you only need logical false result. Your request is most likely returned with non 2xx or 3xx code.

Oleg V. Volkov
  • 21,719
  • 4
  • 44
  • 68
3

From the Perl documentation:

The number 0, the strings '0' and "" , the empty list () , and undef are all false in a boolean context. All other values are true. Negation of a true value by ! or not returns a special false value. When evaluated as a string it is treated as "" , but as a number, it is treated as 0. Most Perl operators that return true or false behave this way.

In other words, your mistake is assuming that boolean false is always represented by 0. It would be more accurate to say that in Perl false is represented by "empty", with what that means depending on the context.

This is useful, because it allows for clean code in a variety of contexts:

#evaluates false when there are no more lines in the file to process
while (<FILE>) { ... }

#evaluates false when there are no array elements
if (!@array) { ... }

#evaluates false if this variable in your code (being used as a reference) 
#hasn't been pointed to anything yet.
unless ($my_reference) { ... }

And so on...

In your case, it's not clear why you want false to be equal to zero. The if() statement in your code should work as written. If you need the result to be explicitly numeric for some reason, you could do something like this:

my $numeric_true_false = ($response->is_success() ? 1 : 0);
dan1111
  • 6,576
  • 2
  • 18
  • 29
3

From the discussion in comments:

$response->status_line 

actually returned 500 Can't Connect to database.

With $response->is_success(), I was unable to understand the response from db.

Used $response->status_line to find out exactly where the code was failing.