"Rob Dixon" <[EMAIL PROTECTED]> writes:

>>So it would be useful for LWP::Simple to have a global flag saying
>>'die on error, rather than returning undef'.

>All of the comparable Perl operations that I know about, including
>all the Net:: modules, return a false value if they've failed.
>In all these cases you're writing something like
>
>  open my $fh, 'file' or die $!;

Yes, but this isn't a perfect analogy to LWP::Simple::get() for
a few reasons:

- It is possible for get() to return false if it succeeds.  After all
  it's entirely allowed for the content at a URL to be '0' or the
  empty file.  That's different from a download failure.

- Unlike common operations like open(), close() and print(), the
  return value from get() carries information apart from success or
  failure.  You can't say

      get($url) or die;

  or even

      defined(get $url) or die;

  but instead you must save the value into a variable and then
  separately test it before using, perhaps with

      my $got = get 'http://perl.org/';
      die "could not get http://perl.org/"; if not defined $got;
      do_something_with($got);

  or to avoid repeating the URL in your code,

      my $url = 'http://perl.org/';
      my $got = get $url;
      die "could not get $url" if not defined $got;
      do_something_with($got);

  Compare this to the idiom if get() throws an exception on error:

      do_something_with(get('http://perl.org/'));

  The trouble is that once you add error checking and reporting, which
  every program should have, LWP::Simple no longer lives up to its
  name.

  Admitted, some of this applies to open() too - you have to give the
  filename both in the open() call and in any error message thrown.

- With open() and similar functions you can find an error string in
  $!, but get() cannot set $! and does not (currently) set any
  equivalent variable to store an error message.  All get() can report
  is success or failure, with no information about why the failure
  happened.  Yes, if you want to do complex error handling use some
  bigger module than LWP::Simple, but even simple programs are better
  if they report details about what failed rather than a Redmondian
  'Could not get URL'.

  Nobody encourages programmers to write

      open FH, 'file' or die "couldn't open file";

  indeed, not including $! in error messages is one of the big
  failings noted in the Camel Critiques.  But this style of error
  message is the only one possible with LWP::Simple.

  OTOH, if get() threw an exception it would be able to include a
  helpful error message - bad URL syntax? couldn't resolve hostname?
  404 not found? - much better than a bare 'undef'.  But this would
  happen without the programmer having to do anything special.  In
  other words, I am concerned to make it easy to do the right thing.

-- 
Ed Avis <[EMAIL PROTECTED]>

Reply via email to