Beefy Boxes and Bandwidth Generously Provided by pair Networks
more useful options

Remote Module "use"

by idsfa (Vicar)
on Oct 26, 2004 at 20:36 UTC ( #402795=snippet: print w/replies, xml ) Need Help??

This snippet looks on a specified remote site for any modules not found in the local @INC path. Nice for keeping in sync with a central repository.

Note that this method can be applied to any function that returns a filehandle.

In case it isn't obvious, you'll need to change the web site and Remote::Class appropriately before this will work.

Updated: It does not install the module (unlike CPANPLUS, the workhorse behind the Acme module mentioned below), so you do not need privileged access to use this trick. OTOH, you'll download the module each time you run your code.

use LWP::Simple;

   push @INC, sub
                 my $URL = "";
                 my $module = get("$URL/$_[1]") or return undef;
                 open my $fh, '<', \$module or return undef;
                 return $fh;

use Remote::Class;
Replies are listed 'Best First'.
Re: Remote Module "use"
by davorg (Chancellor) on Oct 27, 2004 at 11:08 UTC
Re: Remote Module "use"
by radiantmatrix (Parson) on Nov 04, 2004 at 14:56 UTC

    The caveat that modules will be downloaded each time the app is run can be worked around using LWP::Simple's mirror sub intead of get.

    Of course, you'll have to provide the output file name to mirror:

    my $response = $ua->mirror( $url, $file ); #EG: $response = $ua->mirror( ' +5.800/lib/LWP/', '/usr/lib/perl/5.8.4/site_lib/LWP/UserAgent.p +m' );

    By doing this, the LWP module checks to see if the file already exists; if it does, a new copy is only downloaded if the remote file is newer than the local copy.

    require General::Disclaimer;
    "Users are evil. All users are evil. Do not trust them. Perl specifically offers the -T switch because it knows users are evil." - japhy
Re: Remote Module "use"
by casiano (Pilgrim) on Aug 09, 2008 at 17:43 UTC
    I have recently released Remote::Use at CPAN, which addresses the problem of loading modules from a binary compatible Perl Module Server. It is in partially in debt with this node and idsfa comment.

    There was an actual motivation to have - as idsfa remarks - a solution that does not install the module (as Module::AutoINC), but one that only requires limited knowledge and no privileges.

    At the institution I work the student laboratories contain hundreds of computers that can be started in Linux or Windows. The OS is replicated from a master copy in a server.
    The administrators are reluctant to install all the Perl Modules I need for teaching. The problem is that I keep discovering interesting new modules and introducing them in my lectures, which means I am continuously bothering them, asking for a new module to be introduced in the master copy/computer.
    However, I can still install that modules in a machine that is accesible to the students.

    Remote::Use helps to solve these sort of problems. It provides ways to run a Perl program even if some libraries aren't availables at start time. The libraries will be downloaded from a specified server using a specified application that runs on top of some protocol. The clients must be binary compatibles with the server if binary libraries - See Remote::Use::Tutorial for examples - are involved. Typical downloaders are "Rsync" and "Wget" but any suitable alternative like lwp-mirror or "CURL" can be used. This means that many different protocols can be used for the transference: SSH, SFTP, HTTP, HTTPS, FTP, etc. This way, my students can download the modules their programs use to a scratch directory. Once the modules are downloaded they will not be downloaded again, unless the modules are removed from the scratch disk.

    Read Remote::Use::Tutorial if you want to know more.

    The way to make it work is simple. Consider the following program:

    $ cat -n 1 #!/usr/bin/perl -I../lib -w 2 # The module Math::Prime::XS isn't installed in the machine 3 # but will be downloaded from some remote server 4 use Math::Prime::XS qw{:all}; 5 6 @all_primes = primes(9); 7 print "@all_primes\n"; 8 9 @range_primes = primes(4, 9); 10 print "@range_primes\n";
    The client machine does not have installed Math::Prime::XS. As the name suggest, the modules involves some binary libraries. By declaring the use of Remote::Use first, the libraries will be downloaded and the program can complete its execution:
    $ time perl -MRemote::Use=config,rsyncconfig receiving file list ... done >f+++++++++ sent 42 bytes received 16141 bytes 10788.67 bytes/sec total size is 16043 speedup is 0.99 receiving file list ... done >f+++++++++ sent 42 bytes received 94 bytes 90.67 bytes/sec total size is 0 speedup is 0.00 receiving file list ... done >f+++++++++ sent 42 bytes received 5733 bytes 11550.00 bytes/sec total size is 5635 speedup is 0.98 2 3 5 7 5 7 real 0m2.349s user 0m0.116s sys 0m0.060s
    The libraries are stored in a cache directory. consequently succesive executions don't pay extra overhead:
    $ time perl -MRemote::Use=config,rsyncconfig 2 3 5 7 5 7 real 0m0.066s user 0m0.056s sys 0m0.008s
    The programmer has however to set a configuration file rsyncconfig setting the parameters that govern the downloading process:
    $ cat rsyncconfig package rsyncconfig; sub getarg { my ($class, $self) = @_; return ( host => 'orion:', prefix => '/tmp/perl5lib/', command => 'rsync -i -vaue ssh', ppmdf => '/tmp/perl5lib/.orion.installed.modules', ); } 1;


Re: Remote Module "use"
by Anonymous Monk on Mar 10, 2009 at 13:20 UTC
    Log In?

    What's my password?
    Create A New User
    Node Status?
    node history
    Node Type: snippet [id://402795]
    [Your Mother]: The Inquisition? What a show!
    [LanX]: which thread are you talking about?
    [erix]: great hats
    [erix]: the latter-day part of [Answered; thanks.] Can this be explained in layman's terms?

    How do I use this? | Other CB clients
    Other Users?
    Others rifling through the Monastery: (5)
    As of 2017-01-18 00:05 GMT
    Find Nodes?
      Voting Booth?
      Do you watch meteor showers?

      Results (159 votes). Check out past polls.