.lib/perl5/Autofetch.pm

Sat, 17 Feb 2018 02:11:40 -0500

author
Meredith Howard <mhoward@roomag.org>
date
Sat, 17 Feb 2018 02:11:40 -0500
changeset 688
2ad9a3d85adc
parent 682
31d13f6d3a30
child 690
d005acd0e883
permissions
-rw-r--r--

dotjs is gone

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($ENV{HOME}, '.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