"use" remote modules?

L

lord0

Hi,

Is it possible to "use" or otherwise import modules held on a REMOTE host
that they may be used? I had considered some sort of SOAP solution but any
ideas/urls etc would be appreciated

Lord0
 
B

Benjamin Goldberg

lord0 said:
Hi,

Is it possible to "use" or otherwise import modules held on a REMOTE
host that they may be used? I had considered some sort of SOAP solution
but any ideas/urls etc would be appreciated

Perl 5.6.1 and latter allow you to put a coderef into @INC.

When you use or require a file, that coderef will be called with two
arguments -- the first one being the coderef itself, the second being
the filename. The coderef may return undef, indicating to perl to
continue searching @INC, or it may return a filehandle, which should
then be used as the required file.

Here's some untested code to do what you want:

use lib sub {
require LWP::Simple;
my $name = "http://www.foo.com/" . pop;
my $doc = LWP::Simple::get($name) or return;
open(my ($fh), "<:scalar", \$doc) or die horribly;
$fh;
};
 
E

Eric J. Roode

Yes, it resulted in compilation error

Change the "use" above to "require". The "use" is happening when the BEGIN
block is compiled, before the "push" happens.
 
E

Eric J. Roode

-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

Eric J. Roode wrote:



If I change the "use" to "require" inside the BEGIN block I get:

BEGIN{
push @INC,\&get_remote_module('remote_module.pm');
require remote_module;
}

Undefined subroutine &main::get_remote_module called at test.pl line
4. BEGIN failed--compilation aborted at test.pl line 6.

So I then try moving the sub get_remote_module inside the BEGIN block
to allow the @INC push to see it in it's lexical scope and get:

BEGIN{
push @INC,\&get_remote_module('remote_module.pm');
require remote_module;
sub get_remote_module{
require LWP::Simple;
my $name='http://www.remotehost.org/cgi-bin/'.pop;
my $doc=LWP::Simple::get($name) or return;
open(my($fh),"<:scalar",\$doc) or die "urg!\n$!";
return $fh;
}
}

Not a CODE reference at test.pl line 5.
BEGIN failed--compilation aborted at test.pl line 13.

Oops, yes, I should have noticed this the first time around. Sorry.

A code reference is a reference to a subroutine. It does *not* include
any arguments to the subroutine. Try that last one again, but with the
first line as:

push @INC, \&get_remote_module;

- --
Eric
$_ = reverse sort qw p ekca lre Js reh ts
p, $/.r, map $_.$", qw e p h tona e; print

-----BEGIN PGP SIGNATURE-----
Version: PGPfreeware 7.0.3 for non-commercial use <http://www.pgp.com>

iQA/AwUBPxp4M2PeouIeTNHoEQLLLgCeN5w9IUHCHIZGZSU9kIL86TJQZWkAniN8
tg7LbcwkXBGpCYuuttgswlQb
=Tw1H
-----END PGP SIGNATURE-----
 
B

Benjamin Goldberg

Michele said:
^^^^^^^^^^^^^^

What is the possible use of that? I guess there must have been a good
reason why it has been implemented that way, but I cannot see what it
may be...

Well, coderefs aren't the only funny things you can put into @INC.

You can put in an arrayref, which has a coderef as the first element...
when used, the coderef will be called with the first argument as the
arrayref, and the second as the filename. This way, you can pass extra
data in the rest of the arrayref.

You can put in a blessed object, in which case it's ->INC method will be
called, with the filename as an argument. The blessed object itself
will, as it always is with methods, be the first argument to the
subroutine.
[snip]
open(my ($fh), "<:scalar", \$doc) or die horribly;
^^^^^^

Browsing through the docs I finally ('perldoc PerlIOl') understood the
meaning of that layer, but it seems that it wouldn't have been
necessary while opening \$doc anyway, so I ask you if there's any
advantage specifying it explicitly or if it "just" for a matter of
clarity (always an important issue, IMHO).

Both for clarity, and so that the program would die if run under 5.6.1
or earlier.

One can, under 5.6.1, get the same effect by tieing to IO::Scalar, but
that would've been more typing for me :)
Also, as I said, it was not exactly straightforward to get to the
pertinent piece of documentation. How is one supposed to learn "this
kind of things", apart being so fortunate as to decide to read by
chance such a precious post as this one?

I've no idea... I happen to subscribe to the perl5porters mailing list,
and so learned of it that way.
 
L

Lawrence Tierney

Hello again,

I would like to thank you all for your help - I now have the code up
and running - groovy. This has been one of the most enlightening
threads I have been involved with - I had no idea you could add
code-refs etc to @INC.

All the better cos I didn't get hit with any RTFM's etc

Keep up the good work and thanks again

Lord0
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,781
Messages
2,569,616
Members
45,306
Latest member
TeddyWeath

Latest Threads

Top