http://www.perlmonks.org?node_id=582572


in reply to Auto install of a perl package if 'use' statement fails

Yes. I've seen it done. I believe that Abigail did it, but I can't find the code.

Anyways the idea is simple. If you read the documentation for require, you'll find that you can put subroutines in @INC. Then you can pull tricks like this untested code:

use LWP::Simple qw(get); BEGIN { push @INC, sub { shift; my $file = shift; # eg "Foo/Bar.pm" my $content = get("http://myrepository.com/modules/$file"); if ($content) { open(my $fh, "<", \$content); return $fh; } else { return; } } }
(This one grabs the module off of a webserver, and loads it directly. No local installation needed! With older versions of Perl you'll need IO::Scalar instead of the open trick that I used to put the contents of a scalar into a filehandle.)

Replies are listed 'Best First'.
Re^2: Auto install of a perl package if 'use' statement fails
by Zubinix (Acolyte) on Nov 07, 2006 at 06:22 UTC
    Thanks for this. I will follow up on it :-)