tags:

views:

136

answers:

3

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

  • Whether perl has installed in that system
  • Perl should have the version 5 or more
  • Whether the module JSON::Any is available

Any suggestion would greatly appreciated :-)

+8  A: 

I wouldn't spend time adding another bash script but if you really want you have to check only if perl -v returns 0 in bash. If it does return 0 you have perl installed.

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

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:

Sorin Sbarnea
I dont want to print more details , I just want to know the version number only , when I issue perl -v command , any suggestions
abubacker
You check the version number in perl itself with `use 5.006_001`. perl -v just checks if a perl binary can be executed (=> Check if perl is installed), you can ignore everything it prints, just the return value of the execution matters.
dbemerlin
The question does ask "Is perl installed on the system". Your answer precludes automation of the "perl not found" response (perl missing, or more likely, not on PATH). Granted, every system I work with has Perl installed by the system - but the questioner is worried about 'is perl there at all'.
Jonathan Leffler
+2  A: 
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!)

Jonathan Leffler
I would prefer `perl -MJSON::Any -e1 2>/dev/null` - is there any point in printing the version and sending it to a bit bucket
justintime
@justintime: As I said, if you want to check the version of JSON::Any, capture the output - it saved me having to explain how to get hold of the version as a separate exercise.
Jonathan Leffler
+7  A: 

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 <[email protected]>',
    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 [email protected], web forum PerlMonks, IRC channel #toolchain on MagNET (irc://irc.perl.org/toolchain)

daxim