This seems crazy but it actually works! Tests show this only happens if the module doesn't use other modules or compile. If dependencies can be handled then Perl could use even more CPAN modules that are not installed! Thoughts?
#!/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__ use strict; use warnings; 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 { my ($module) = @_; if ($module =~ /install the (\S+) module/) { my $modname = $1; require HTTP::Tiny; my $pm = HTTP::Tiny->new->get("https://metacpan.org/pod/$m +odname"); for ($pm->{content} =~ /Source<\/a>\s*\(<a href="([^"]+)"> +raw/) { $pm = HTTP::Tiny->new->get($1); eval $pm->{content}; last } } }

Hack away...


In reply to 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.