How to Quickly Check If Linux 'Unzip' Is Installed Using Perl

How can I quickly check if Linux `unzip` is installed using Perl?

Just run it.

Seriously.

Presumably, the reason why you want to know if it is installed, is because you need to run it later. In that case, it isn't enough to know whether it is installed, anyway – you also need to know whether it is executable, whether it is in the path, whether the user id the script is running under has the necessary privileges to run it, and so on. You can check all that by simply just running it.

Checking whether a program exists

To me a better check is to run the program at the beginning of the script (with -V say).

I'd use the same invocation as you use to run the job later (via shell or not, via execvp). Once at it, make sure to see whether it threw errors. This is also discussed in your link but I would in fact get the output back (not send it away) and check that. This is the surest way to see whether the thing actually runs out of your program and whether it is what you expect it to be.

Checking for the executable with -x (if you know the path) is useful, too, but it only tells you that a file with a given name is there and that it is executable.

The system's which seems to be beset with critism for its possible (mis)behavior, it may or may not be a shell-builtin (which complicates how exactly to use it), is an external utility, and its exact behavior is system dependent. The module File::Which pointed out in Borodin's answer would be better -- if it is indeed better than which. (What it may well be, I just don't know.)


Note. I am not sure what "bash command" means: a bash shell built-in, or the fact that you use bash when on terminal? Perl's qx and system use the sh shell, not bash (if they invoke the shell, which depends on how you use them). While sh is mostly a link, and often to bash, it may not be and there are differences, and you cannot rely on your shell configuration.

Can also actually run a shell, qx(/path/bash -c 'cmd args'), if you must. Mind the quotes. You may need to play with it to find the exact syntax on your system. See this page and links.

Perl reading zip files with IO::Uncompress::AnyUncompress

The files in the archive are compressed in different data streams, so you need to iterate through the streams to get the individual files.

use strict;
use warnings;
use IO::Uncompress::Unzip qw(unzip $UnzipError);

my $zipfile = 'zipfile.zip';
my $u = new IO::Uncompress::Unzip $zipfile
or die "Cannot open $zipfile: $UnzipError";

die "Zipfile has no members"
if ! defined $u->getHeaderInfo;

for (my $status = 1; $status > 0; $status = $u->nextStream) {
my $name = $u->getHeaderInfo->{Name};
warn "Processing member $name\n" ;

if ($name =~ /\/$/) {
mkdir $name;
}
else {
unzip $zipfile => $name, Name => $name
or die "unzip failed: $UnzipError\n";
}
}

How can I find the version of an installed Perl module?

Why are you trying to get the version of the module? Do you need this from within a program, do you just need the number to pass to another operation, or are you just trying to find out what you have?

I have this built into the cpan (which comes with perl) with the -D switch so you can see the version that you have installed and the current version on CPAN:


$ cpan -D Text::CSV_XS

Text::CSV_XS
-------------------------------------------------------------------------
Fast 8bit clean version of Text::CSV
H/HM/HMBRAND/Text-CSV_XS-0.54.tgz
/usr/local/lib/perl5/site_perl/5.8.8/darwin-2level/Text/CSV_XS.pm
Installed: 0.32
CPAN: 0.54 Not up to date
H.Merijn Brand (HMBRAND)
h.m.brand@xs4all.nl

If you want to see all of the out-of-date modules, use the -O (capital O) switch:


$ cpan -O
Module Name Local CPAN
-------------------------------------------------------------------------
Apache::DB 0.1300 0.1400
Apache::SOAP 0.0000 0.7100
Apache::Session 1.8300 1.8700
Apache::SizeLimit 0.0300 0.9100
Apache::XMLRPC::Lite 0.0000 0.7100
... and so on

If you want to see this for all modules you have installed, try the -a switch to create an autobundle.



Related Topics



Leave a reply



Submit