.lib/perl5/Autofetch.pm

Wed, 06 Feb 2019 21:03:11 -0600

author
Meredith Howard <mhoward@roomag.org>
date
Wed, 06 Feb 2019 21:03:11 -0600
changeset 725
6ff8e3f73482
parent 690
d005acd0e883
child 852
cf73e0ff4258
permissions
-rw-r--r--

switch from vundle to plug

package Autofetch;

use strict;
use warnings;
use File::Spec::Functions qw(catfile rel2abs);

sub fetch {
  my ($path, $module) = @_;
  system 'cpanm', -nq, -l => $path, $module;
}

sub cachepath {
  catfile(glob('~'), '.cache', 'lib', rel2abs($_[0]) =~ y|/ |-_|r);
}

sub modulefy { $_[0] =~ s/\.pm$//r =~ s|/|::|gr }

sub import {
  return if our $INSTALLED++;

  my $path    = cachepath($0);
  my $incpath = catfile($path, 'lib', 'perl5');

  push @INC, $incpath, sub {
    my (undef, $file) = @_;

    fetch($path, modulefy($file));

    return IO::File->new(catfile($incpath, $file));
  };
}

1;

mercurial