ajt has asked for the wisdom of the Perl Monks concerning the following question:

Hi, in his excellent AxKit (IP), Matt uses both LWP::UserAgent and HTTP::GHTTP. Actually he requires GHTTP and if the require fails (caught by an eval) he goes on to use LWP.

I asssume the logic Matt is using is: GHTTP is faster than LWP, but it's a lot less common, so he tries to use it first, then if that fails, he uses the more common LWP. Here is a fragment of his code:

eval { require HTTP::GHTTP; }; if ($@) { require LWP::Simple; import LWP::Simple; return get($sysid) || die "Cannot get $sysid"; } my $r = HTTP::GHTTP->new($sysid); $r->process_request; return $r->get_body;

I've implemented the same idea in some of my own scripts, and it seems to work, and GHTTP seems to be faster than LWP.

My question is, when there are several modules to use, one which is fast and rare, another that is very fast but OS specific, and one that is slow but works everywhere, is this method as used by Matt wise?

Replies are listed 'Best First'.
Re: use-ing one of two or more similar modules
by tomhukins (Curate) on Oct 11, 2001 at 22:44 UTC

    My opinion: yes, this is wise. LWP is a very popular Perl module, and HTTP::GHTTP is very rarely used, but the performance gains from HTTP::GHTTP are significant in terms of processor and memory use - see the module's documentation for details.

    Of course, if you take this approach, your code will have to support two modules, which might be too much effort. So, why not just support one module and expect users to install it from CPAN? Well, HTTP::GHTTP requires Gnome's libghttp, which many machines will not have. I don't know, but I wouldn't be surprised if this isn't available or is hard to install for Windows. If people are going to have trouble installing HTTP::GHTTP then it's worth supporting LWP.

    Update: ajt tells me that HTTP::GHTTP installs very easily under Windows - cool!

Re: use-ing one of two or more similar modules
by perrin (Chancellor) on Oct 12, 2001 at 01:12 UTC
    MLDBM does this as well. It defaults to the most compatible modules, but allows users to specicy less common alternatives. Of course Matt's approach is somewhat more transparent: install it and go!