Discussion:
Dynamically downloading modules needed by embedded interpreter
(too old to reply)
p***@bigfoot.com
2007-04-25 03:39:01 UTC
Permalink
Is it possible to figure out all modules a script might need before
running it from an embedded interpreter?

I'm looking into adding a perl interpreter to an internal company
application. The application will need to run specific perl scripts which
it will dynamically download. That part shouldn't be too tough... but
these scripts could refer to perl modules which are not available on the
client workstation. In fact, initially the client workstation will have
no perl installation at all.

Is there a way to pre-compile a script to figure out what modules it may
need so I can download them and store them in a cached lib directory?

Obviously I will need to recursively pre-compile each module after it's
downloaded in order to figure out if it requires any modules not already
cached as well, but I assume the same process could be used.

PS. Hopefully this is an appropriate list for this type of question. If
not, I'd appreciate any suggestions for a more appropriate list. Thanks.
Sherm Pendley
2007-04-25 03:55:54 UTC
Permalink
Post by p***@bigfoot.com
Is there a way to pre-compile a script to figure out what modules
it may need so I can download them and store them in a cached lib
directory?
Add code that prints %INC upon exiting, then run the script - that'll
give you a list of every file that was use()d or require()d during
the script's run.

sherm--

Web Hosting by West Virginians, for West Virginians: http://wv-www.net
Cocoa programming in Perl: http://camelbones.sourceforge.net
s***@gmail.com
2007-04-26 21:22:08 UTC
Permalink
Post by Sherm Pendley
Post by p***@bigfoot.com
Is there a way to pre-compile a script to figure out what modules
it may need so I can download them and store them in a cached lib
directory?
Add code that prints %INC upon exiting, then run the script - that'll
give you a list of every file that was use()d or require()d during
the script's run.
I'm doing that in my application for a similar purpose. One thing to
watch out for is that it won't give you the .so files for extension
modules. (Happily, I don't have to care about that yet.) It also won't
find most configuration files or whatever else is loaded during the
run.

This will also only find things that were actually used during the
run. use() is not a problem, since it's unconditional, but require/do/
etc. are runtime.

You could always do strace -f -eopen

If you don't want to run the script and aren't worried about the
caveats above, then you can mimic what -c does. (Which itself can run
whatever it wants in BEGIN{} blocks, so it's not guaranteed to be less
of a problem than running your code.)
p***@bigfoot.com
2007-04-30 18:24:16 UTC
Permalink
Post by Sherm Pendley
Post by p***@bigfoot.com
Is there a way to pre-compile a script to figure out what modules it may
need so I can download them and store them in a cached lib directory?
Add code that prints %INC upon exiting, then run the script - that'll
give you a list of every file that was use()d or require()d during the
script's run.
I need to dynamically download both the script AND any modules the script
may require. This means I need to dynamically download the script itself,
then just before running the script, figure out what modules are required
and download them as well.

To say it another way, I don't know the name of the script (or any modules
it may need) before it's requested.

The client app I intend to write will be written in C and include an
embedded perl interpreter. The user will run the client (again written
in C)... and issue a command. This command will run on the server, but it
MAY include a client-side component written in perl. The C app will send
a message to the server to see if there's a client-side perl component and
if so, download the perl script.

It's at this point I'd like to be able to somehow detect (without actually
running the script) what modules it may need, so I can download them.

Another possible alternative would be to somehow interject some code into
the perl interpreter just before it attempts to look for an external file
(ie searches %INC).... assuming that's even possible.
Sherm Pendley
2007-04-30 19:23:00 UTC
Permalink
Post by p***@bigfoot.com
Post by Sherm Pendley
Post by p***@bigfoot.com
Is there a way to pre-compile a script to figure out what modules
it may need so I can download them and store them in a cached lib
directory?
Add code that prints %INC upon exiting, then run the script -
that'll give you a list of every file that was use()d or require()
d during the script's run.
I need to dynamically download both the script AND any modules the
script may require. This means I need to dynamically download the
script itself, then just before running the script, figure out what
modules are required and download them as well.
To say it another way, I don't know the name of the script (or any
modules it may need) before it's requested.
The client app I intend to write will be written in C and include
an embedded perl interpreter. The user will run the client (again
written in C)... and issue a command. This command will run on the
server, but it MAY include a client-side component written in
perl. The C app will send a message to the server to see if
there's a client-side perl component and if so, download the perl
script.
It's at this point I'd like to be able to somehow detect (without
actually running the script) what modules it may need, so I can
download them.
If you want to detect that before the script is fetched, the only way
to do that would be to fetch an accompanying file that lists them.
Post by p***@bigfoot.com
Another possible alternative would be to somehow interject some
code into the perl interpreter just before it attempts to look for
an external file (ie searches %INC).... assuming that's even possible.
That's not only possible, it's pretty easy - just push a code ref
onto the end of @INC. The sub that's pointed to by the code ref will
be called when a requested module isn't found at any of the previous
directories pointed to by @INC; at that point you can download it.

sherm--

Web Hosting by West Virginians, for West Virginians: http://wv-www.net
Cocoa programming in Perl: http://camelbones.sourceforge.net
p***@bigfoot.com
2007-04-30 19:48:56 UTC
Permalink
Post by p***@bigfoot.com
Another possible alternative would be to somehow interject some code
into the perl interpreter just before it attempts to look for an
external file (ie searches %INC).... assuming that's even possible.
That's not only possible, it's pretty easy - just push a code ref onto
called when a requested module isn't found at any of the previous
Ahhh... very cool. Never new that was possible.

Presumably, since I'll be using an embedded interpreter there's a way to
make the code ref point to a C routine?
Sherm Pendley
2007-05-15 18:20:55 UTC
Permalink
Post by p***@bigfoot.com
Post by Sherm Pendley
Post by p***@bigfoot.com
Another possible alternative would be to somehow interject some
code into the perl interpreter just before it attempts to look
for an external file (ie searches %INC).... assuming that's even
possible.
That's not only possible, it's pretty easy - just push a code ref
will be called when a requested module isn't found at any of the
download it.
Ahhh... very cool. Never new that was possible.
Presumably, since I'll be using an embedded interpreter there's a
way to make the code ref point to a C routine?
Not directly, but it could easily point to an XS wrapper for the C
routine.

sherm--

Web Hosting by West Virginians, for West Virginians: http://wv-www.net
Cocoa programming in Perl: http://camelbones.sourceforge.net

Loading...