Search code examples
bashperlperl-module

How to check availability of Perl, its version and presence of a required module?


I have written a Perl script, I just want to give it to every one, for that I planned to write a bash script which is used to test the environment of a user and find whether that environment is capable of running the Perl script.

I want to test the things like:

  1. Whether Perl has installed in that system
  2. Perl should have the version 5 or more
  3. Whether the module JSON::Any is available

Any suggestion would greatly appreciated :-)


Solution

  • if perl -MJSON::Any -e 'print "$JSON::Any::VERSION\n"' >/dev/null 2>&1
    then : OK
    else echo "Cannot find a perl with JSON::Any installed" 1>&2
         exit 1
    fi
    

    I often use '${PERL:-perl}' and similar constructs to identify the command (for awk vs nawk or gawk; troff vs groff; etc).

    If you want to test the version of JSON::Any, capture the output from the command instead. If you want to test the version of Perl, add 'use 5.008009;' or whatever number you think is sensible. (It wasn't so long ago that they finally removed Perl 4 from one of the NFS-mounted file systems at work - but that was not the only Perl on the machine - at least, not in the last decade or more!)