baxy77bax has asked for the wisdom of the Perl Monks concerning the following question:

Hi,

What would be the proper way to decompress a module first and then use it in the script? Example:

BEGIN{system("tar -xzvf XX.tar.gz")} use strict; use lib "./XX.pm" use XX; ...
How to achieve the above from one script. Because if i do not have a module XX.pm available, the script complains ..
Thnx

Replies are listed 'Best First'.
Re: How to first decompress a module and then load it locally
by Corion (Patriarch) on Aug 28, 2020 at 13:48 UTC

    Maybe you can help us provide better solutions for the problem by telling us more about the actual problem you're trying to solve?

    Other than that, the straightforward approach is:

    BEGIN{system("tar -xzvf XX.tar.gz")} use strict; use lib "./" use XX;

    ... or, according to BEGIN:

    system("tar -xzvf XX.tar.gz"); use strict; require './XX.pm'; XX->import();

    Also see Module::Load maybe.

Re: How to first decompress a module and then load it locally
by marto (Cardinal) on Aug 28, 2020 at 13:50 UTC

    Have you considered packaging your script and it's dependencies, something like pp?

Re: How to first decompress a module and then load it locally
by LanX (Saint) on Aug 28, 2020 at 14:27 UTC
    By putting a code hook into @INC which does the decompression if the module is not already available.

    Then a normal use is enough.

    More details in require

    Cheers Rolf
    (addicted to the Perl Programming Language :)
    Wikisyntax for the Monastery

Re: How to first decompress a module and then load it locally
by perlfan (Parson) on Aug 28, 2020 at 14:17 UTC
    use lib "./XX.pm"

    Needs to be:

    use lib "."

    But I encourage you to look at FindBin for that sort of thing, in addition to the other help you receive here.

Re/Follow up: How to first decompress a module and then load it locally
by baxy77bax (Deacon) on Aug 29, 2020 at 11:24 UTC
    Quick follow up:

    Is it possible to include the tar.gz file in my script as well ? Can I have only one script (packed file or something) that when i execute it, it extracts my tar.gz and calls modules within ? thnx

      That's pretty much what pp does, generates an executable which is essentially a self extracting zip file. Your method won't work on all platforms.