When I have to do something like that I usually write compatible warppers.

What I mean by this is that I have a small module, one for each module my app knows how to use:

package MyApp::URLFetcher::LWP; use base qw/MyApp::URLFetcher/; use LWP::Simple; # define the most convenient interface you can think of # if you need just the contents of a URL in a string, do that sub get_url_to_str { my $url = shift; get($url); # our chosen interface is simple with LWP }
package MyApp::URLFetcher::NetFTP; use base qw/MyApp::URLFetcher/; use Net::FTP; sub get_url_to_str { my $self = shift; my $url = shift; # I don't know Net::FTP # either way, our interface is a bit harder to pull off # with Net::FTP # but this complexity will be in the software anyway # it's better to hide it here my $f = Net::FTP->new; # ... # ... $f->get("..."); }
I usually write one driver, the simplest one, and then add others later.

Then in your app, say you need a URL:

use UNIVERSAL::require; my $fetcher_class = choose_fetcher_class(); my $fetcher = $fetcher_class->new; # this is the good part # this line of code does not care what driver it's using, as # long as the subclass of MyApp::URLFetcher is implemented correctly # isn't polymorphism great? my $content = $fetcher->get($url); sub choose_fetcher_class { foreach my $driver (qw/LWP NetFTP/){ my $class = "MyApp::URLFetcher::$driver"; # UNIVERSAL::require let's us avoid an eval return $class if $class->require; } die "couldn't load any driver"; }

Update: Oops! I forgot the classes in the foreach loop

Also, you might want to look into Module::Pluggable and friends, they might be useful for "finding" drivers, without the code that uses them even caring about their names. I don't think they have an API to skip failed modules, and furthermore to only get the first successful one.

-nuffin
zz zZ Z Z #!perl

In reply to Re: Gracefully choosing which module to use by nothingmuch
in thread Gracefully choosing which module to use by kaif

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.