6

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

jww
  • 83,594
  • 69
  • 338
  • 732
abubacker
  • 4,128
  • 5
  • 28
  • 35

3 Answers3

14

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)

daxim
  • 38,078
  • 4
  • 57
  • 123
  • According to CPAN standard, if I has my own .pm files where should I add it, both in Build.PL and file structure? and also if I have more that one Perl script, let say 'script1.pl' and 'script2.pl', how can I add it in Build.PL? module_name => 'script1.pl, script2.pl',? I couldn't find an example on this. – Jessada Thutkawkorapin Mar 05 '12 at 14:53
  • Example: if you have a main module with the package declaration `Fus::Roh::Da`, rename it `Da.pm` and move it to `lib/Fus/Roh/Da.pm`. In `Build.PL`, specify [`module_name => 'Fus::Roh::Da'`](http://p3rl.org/Module::Build::API#module_name). Simply place your two scripts into the subdirectory `script`, the rest works out automatically at install time. – You can test out your Build file by running `perl Build.PL --install_base /tmp ; ./Build install` and check the result in the `/tmp` dir. Run `./Build distclean` and manually remove the installed files to start over. – daxim Mar 05 '12 at 22:12
  • OK, I did what you said. Where is my script now? I expected to copy it somewhere into /usr/bin, change name from script.pl to script and add executable rights. What I get is an empty directory `/usr/lib/perl5/site_perl/auto/TerminalHero`. BTW, Build could gently ask about root password instead of yelling `!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! ERROR: Can't create '/usr/lib/perl5/site_perl/auto/TerminalHero' Do not have write permissions on '/usr/lib/perl5/site_perl/auto/TerminalHero' !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!` – ciembor Jun 30 '12 at 10:25
  • The last step must be `sudo ./Build install` or functionally similar. `Build` could ask for the root password, but that would inhibit unattended installations of which this step is only a small part; the yelling was also added for this circumstance because else it scrolls unnoticed off the screen. - You can configure `cpan` ([`make_install_make_command` and `mbuild_install_build_command`](http://p3rl.org/CPAN#Config-Variables)) and `cpanm` ([`-S` switch](http://p3rl.org/cpanm)) to make use of `sudo`, and then install your distro through one of these tools. – daxim Jun 30 '12 at 11:52
  • Yes, I know I can do that with sudo. But even after that (or after running build directly from root shell), my script isn't installed anywhere in system. Only thing I get is this empty directory. – ciembor Jun 30 '12 at 12:53
  • I cannot diagnose this in the comments. [Open a new question](http://stackoverflow.com/questions/ask), show your files and what commands you run. – daxim Jun 30 '12 at 18:16
8

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:

Community
  • 1
  • 1
sorin
  • 137,198
  • 150
  • 472
  • 707
  • 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 Apr 09 '10 at 07:39
  • 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. – Morfildur Apr 09 '10 at 13:26
  • 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 Apr 09 '10 at 13:56
  • *"...you should deal with them from inside your Perl script..."* - What if there is no Perl script. In my case, I have a Bash script that is checking prerequisites, and it needs to check for the presence of a Perl module. There are no Perl scripts available. – jww Oct 30 '17 at 15:07
2
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
  • 666,971
  • 126
  • 813
  • 1,185
  • 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 Apr 09 '10 at 07:51
  • @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 Apr 09 '10 at 13:54