Beefy Boxes and Bandwidth Generously Provided by pair Networks
Pathologically Eclectic Rubbish Lister
 
PerlMonks  

use alternate module if pm is not installed

by agweih (Initiate)
on Apr 07, 2021 at 08:46 UTC ( #11130939=perlquestion: print w/replies, xml ) Need Help??

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

Dear monks,

my perlscript should run on different systems (windows, linux) where I don't know which modules are installed and I don't have permission to install modules.

Therefore I'm looking for a way to fallback to core modules or my own pm with basic functionality if a module is not installed.

My naive thought was using something like this:

use JSON || JSON::PP;

this does of course not work.

What's the best way to do this?

thanks in advance!

greetings, Toni

Replies are listed 'Best First'.
Re: use alternate module if pm is not installed
by choroba (Archbishop) on Apr 07, 2021 at 09:17 UTC
    use is require plus import, so
    eval { require Module; Module->import; 1 } or do { require Module::Core; Module::Core->import; };

    Possibly wrapped in a BEGIN block.

    Some modules already do that, see e.g. JSON::MaybeXS.

    map{substr$_->[0],$_->[1]||0,1}[\*||{},3],[[]],[ref qr-1,-,-1],[{}],[sub{}^*ARGV,3]
Re: use alternate module if pm is not installed
by hippo (Chancellor) on Apr 07, 2021 at 09:20 UTC

    This is one use case of Module::Load::Conditional which is itself a core module.

    Addendum: Having gone searching for something even more intuitive I have found Best which does exactly what you are after. Even though it isn't a core module, the docs include a section for coping with that. Perhaps this combination approach will serve you well.


    🦛

Re: use alternate module if pm is not installed
by haukex (Bishop) on Apr 07, 2021 at 09:15 UTC

    One way to do this is:

    BEGIN { unless ( eval 'use DesiredModule; 1' ) { require AlternateModule; AlternateModule->import(); } }

    However, I've also made the argument that using two different modules is not necessarily the best approach. There's ways to get modules onto systems where you don't have permissions (local::lib or even App::FatPacker), plus if you're going to be writing your own module anyway, why not just always use that?

    Updates: Added the ->import call, as per choroba's post. It's often not required for OO modules. If you need to pass a specific import list, you can do eval 'use DesiredModule qw/some_func other_func/; 1' and AlternateModule->import(qw/some_func other_func/);. Another possible variation is that you can replace the inner block require ... ->import(); with eval 'use AlternateModule; 1' or die $@||'unknown error';. Stringy eval is often the wrong choice, but in this case it's fine because it's using a fixed string of code.

Re: use alternate module if pm is not installed
by Fletch (Chancellor) on Apr 07, 2021 at 13:42 UTC

    Not specifically useful for this case maybe but also check out the if module to conditionally load a module.

    The cake is a lie.
    The cake is a lie.
    The cake is a lie.

Re: use alternate module if pm is not installed
by agweih (Initiate) on Apr 07, 2021 at 10:11 UTC

    thx for your quick and helpful responses!

    I'll try out all of them

Re: use alternate module if pm is not installed
by xiaoyafeng (Deacon) on Apr 07, 2021 at 14:00 UTC
    I think the best way is installing modules on the two system respectively. perlbrew on linux and portable strawberry perl on windows can resolve you problem, you may try cpan -a on the old system( all modules you are needed are installed) to list all modules, then migrate them all to another.




    I am trying to improve my English skills, if you see a mistake please feel free to reply or /msg me a correction

Re: use alternate module if pm is not installed
by Anonymous Monk on Apr 07, 2021 at 23:02 UTC
    If your script actually needs to run on "an unpredictable system," upon which you do not have the privilege to install modules globally, then your situation becomes identical to that of installing upon "shared hosting." You need to create a local directory, put this first in PERL5LIB, and configure cpan so that it will install modules into that local directory. Then, install every module that you need and the correct versions thereof into that place.
A reply falls below the community's threshold of quality. You may see it by logging in.

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://11130939]
Approved by Corion
Front-paged by davies
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others pondering the Monastery: (4)
As of 2021-04-18 17:01 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found

    Notices?