pwolfenden has asked for the wisdom of the Perl Monks concerning the following question:
I've written a small suite of methods using LWP::UserAgent to connect to a commercial API and do some useful things - like log in and out, and log out automatically if the script exits prematurely, etc.
I decided to put this logic into a Perl package (let's call it "My::Client" for the sake of this discussion), so I used h2xs to build myself a nice Makefile and everything was going nicely until I realized that in order to allow users to specify HTTP proxy settings (URL and credentials) on a "per client" basis (instead of setting $ENV{'HTTPS_PROXY'}, etc.) I would need to tweak the behavior of the get_basic_credentials() method. This is something that the LWP::Useragent documention suggests can be achieved by subclassing the package and overriding the get_basic_credentials() method (and for my purposes I also want to add a set_proxy_credentials() method). Let's give the name "RequestAgent" to this subclassed version of LWP::Useragent (in honor of the RequestAgent package defined in lwp-request).
I first naively attempted to "nest" the RequestAgent package declaration inside the declaration of My::Client, but of course that didn't work. After reading a bit more about Perl packages in general and AutoSplit in particular, I decided to define the RequestAgent and My::Client packages in the same file. But that didn't work, either. It seems that MakeMaker insists on having a nice match between the name of the package file and the name of the package defined inside.
So then I tried defining RequestAgent in a separate package file (with its own h2xs-generated suite of associated files), and I was able to build that just fine. But the My::Client package needs to "use" the RequestAgent package, and this made things complicated when I installed the packages in a local directory. Although I believe that all the use/include issues that I encountered can be overcome via the judicious use of @INC-modifying BEGIN blocks, it seems silly to have to pollute the Perl package namespace this way when all I really want to do is make local use of a trivial modification to the LWP::UserAgent package.
So I'm wondering - perhaps it would be better to modify the package LWP::UserAgent itself so that callback methods can be passed to setter methods at runtime (as function references) rather than being overridden by derived classes at compile time? That seems more in line with my notion of what a "callback method" ought to be in any case.
What do y'all think?
Thanks in advance for your time.
PS - I'm using a Perl 5.6.1 distribution.
|
---|