Skip to content

Instantly share code, notes, and snippets.

@jhthorsen
Created February 16, 2012 23:51
Show Gist options
  • Save jhthorsen/1848879 to your computer and use it in GitHub Desktop.
Save jhthorsen/1848879 to your computer and use it in GitHub Desktop.
A pice of code which automatically will install perl dependencies
use IO::Socket::INET; use File::Basename; use File::Path; BEGIN {
our $SOURCE = $ENV{'AUTO_INSTALL_DEPS_SOURCE'} || 'http://api.metacpan.org:80/source/%s';
our $LIB = "$ENV{'HOME'}/.perl5";
push @INC, $LIB, sub {
my($module) = $_[1] =~ /(.*)\.pm$/; $module =~ s!/!::!g;
my($hostname) = $SOURCE =~ m!//([^/]+)!;
my $s = IO::Socket::INET->new(PeerAddr => $hostname) or die "Cannot download required library from $hostname: $@";
File::Path::make_path(dirname "$LIB/$_[1]");
open my $FH, '+>', "$LIB/$_[1]" or die "Write $LIB/$_[1]: $!";
printf STDERR "GET $SOURCE HTTP/1.1\n", $module if $ENV{'AUTO_INSTALL_DEPS_DEBUG'};
$s->send(sprintf "GET $SOURCE HTTP/1.1\r\nHost: $hostname\r\nConnection: close\r\n\r\n", $module);
while(1) { $s->recv($_, 0xfff); m!HTTP/1.1 (\d+)! and $1 != 200 and return; s!^.*\r\n\r\n!!s and last }
while(1) { print $FH $_; $s->recv($_, 0xfff); length or last }
seek $FH, 0, 0; $FH;
} } 1;
@jhthorsen
Copy link
Author

How nice: No more asking the user to download pure perl dependencies after downloading a script :)
The idea is that the code above can bootstrap an application by downloading the missing modules from http://metacpan.org. The downloaded modules will be installed in $HOME/.perl5 which is not really portable, but this is more a proof of concept.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment