Beefy Boxes and Bandwidth Generously Provided by pair Networks
Just another Perl shrine

Re: Use a CPAN module that is not installed! Is this a good idea?

by usemodperl (Beadle)
on Jun 18, 2018 at 19:21 UTC ( #1216883=note: print w/replies, xml ) Need Help??

in reply to Use a CPAN module that is not installed! Is this a good idea?

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 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("$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

Log In?

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://1216883]
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others exploiting the Monastery: (5)
As of 2022-12-03 18:22 GMT
Find Nodes?
    Voting Booth?

    No recent polls found