CPAN as a cloud API. Good idea if you can't install modules. This version caches a copy of the downloaded module to a folder named .perlmod in the working directory.:
#!/usr/bin/perl -l # Use a CPAN module that is *not* installed! Is this a good idea? # Posted to perlmonks.org by an Anonymous Monk on Sun Jun 17 2018 # # In this example, if the Astro::MoonPhase module is not installed, # the use_cpan sub will parse the error, download the module page # from metacpan, parse that for the source code uri, download the # raw module source, eval that string, and proceed normally as if # the module was installed! It does not work with all modules and # the concept is presented to the Monastery for contemplation. #__QKB__ # Hacked by usemodperl: use Carp. Added module cache. use strict; use warnings; use Carp 'croak'; eval { require Astro::MoonPhase }; use_CPAN($@) if $@; my @phases = Astro::MoonPhase::phasehunt(); print q~LOCALTIME = ~, scalar localtime time; print q~New Moon = ~, scalar localtime $phases[0]; print q~First quarter = ~, scalar localtime $phases[1]; print q~Full moon = ~, scalar localtime $phases[2]; print q~Last quarter = ~, scalar localtime $phases[3]; print q~New Moon = ~, scalar localtime $phases[4]; sub use_CPAN { local ($_) = @_; if (/install the (\S+) module/) { my $module = $1; my $moddir = '.perlmod'; (my $modloc = $module) =~ s/::/__/g; $modloc = "$moddir/$modloc"; if (-d $moddir and -e $modloc) { open my $fh, '<', $modloc or croak "$!"; @_ = <$fh>; close $fh; $_ = join "\n", @_; eval $_ or croak; } else { require HTTP::Tiny; $_ = HTTP::Tiny->new->get("https://metacpan.org/pod/$modul +e"); croak unless $_->{content}; for ($_->{content} =~ /Source<\/a>\s*\(<a href="([^"]+)">r +aw/) { $_ = HTTP::Tiny->new->get($1); $_ = $_->{content} ? $_->{content} : croak; eval $_ or croak; eval mkdir $moddir; croak $@ if $@; if (open my $fh, '>', $modloc or croak "$!") { print $fh $_; close $fh; } last } } } }
The programmer is fighting against the two most destructive forces in the universe: entropy and human stupidity.—Damian Conway

In reply to Re: Use a CPAN module that is not installed! Is this a good idea? by usemodperl
in thread Use a CPAN module that is not installed! Is this a good idea? by Anonymous Monk

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.