# HG changeset patch # User Meredith Howard # Date 1518628857 18000 # Node ID 1b5b084bccbb256b067abd1f2268640413bb32a7 # Parent 55a3eaa985257ed357d5719fd6e36c488d0a0ff2 add Autofetch diff --git a/.lib/perl5/Autofetch.pm b/.lib/perl5/Autofetch.pm new file mode 100644 --- /dev/null +++ b/.lib/perl5/Autofetch.pm @@ -0,0 +1,29 @@ +package Autofetch; + +use strict; +use warnings; +use 5.010; + +sub fetch { + my ($path, $module) = @_; + system 'cpanm', -nq, -l => $path, $module; +} + +sub cachepath { + $ENV{HOME} . '/.cache/lib/' . ($_[0] =~ y|/ |-_|r); +} + +sub modulefy { + $_[0] =~ s/\.pm$//r =~ s|/|::|gr; +} + +push @INC, sub { + my (undef, $file) = @_; + state $path = cachepath($0); + + fetch($path, modulefy($file)); + + return IO::File->new($path . '/lib/perl5/' . $file); +}; + +1;