开发者

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 fin开发者_开发技巧d 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 :-)


No, do not write a shell script. Perl already has a perfectly fine way of doing this. The correct way to do this is to build a CPAN-ready distribution using the normal toolchain. Some of this is explained in perlnewmod, perlmodstyle and perlmodinstall.

For a minimal working example, create a directory layout thus:

.
├── Build.PL
├── README
└── script
    └── abuscript.pl

In the Build.PL file, put:

use 5.000;
use Module::Build qw();
Module::Build->new(
    module_name        => 'abuscript',
    dist_version       => '1.000',
    dist_author        => 'abubacker <abubacker@example.com>',
    dist_abstract      => 'describe what the script does in one sentence',
    configure_requires => {
        'perl' => '5.000',
    },
    requires           => {
        'JSON::Any' => 0,
    },
)->create_build_script;

Change the details to suite your purposes.

In the README file, put some installation instructions, for instance:

To install this module, run the following commands:

perl Build.PL
./Build install

Once you're done with all that, you run:

perl Build.PL
./Build manifest
./Build dist

This will result in a .tar.gz archive which you will distribute. Tell your users to install it like any other CPAN module, or if they don't know what that means, they should read the README.

If you have time, I recommend converting your script to a module. The program pl2pm (comes with Perl) and the CPAN module Module-Starter-PBP help you.

If license permits, it is possible to upload your code to CPAN to make it even more convenient for your users. Ask for help in any of the following places first: mailing list module-authors@perl.org, web forum PerlMonks, IRC channel #toolchain on MagNET (irc://irc.perl.org/toolchain)


Regarding checking Perl availability the easiest way to do it is to check the return code (exit code) of the command perl -v,if this is not 0, you do not have Perl.

Now regarding Perl requirements, you should deal with them from inside your Perl script:

#!/usr/bin/env perl
use 5.006_001;
use ModuleName 2.0;

The above Perl code will run only with perl 5.6.1 or newer and with modele "ModuleName" version 2.0 or newer. There is no need to manually check the Perl version from bash, it is better and easier to do it directly from the Perl script.

References:

  • Requiring a minimal Perl version
  • Checking if a Perl module is installed


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!)

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜