#!/usr/bin/perl use Test::Simple 'no_plan'; use File::Which; # gnu xargs v 4.4 or more ok( command_dependency( 'xargs', '4.2.27', 0 ), "have at least xargs 4.2.27"); ok( command_dependency( 'find', '4', 1 ), 'find 4'); ok( command_dependency( 'tar', '1.15' ), 'tar 1.15'); sub command_dependency { my ($name, $version, $debug)=@_; $^O=~/win/ and warn("Will not work on non posix systems.") and return; $debug or local $SIG{__WARN__} = sub { 1 }; no warnings; require File::Which; my $which = File::Which::which($name) or return; warn("Got which $name: $which"); if (defined $version){ # version have.. my $_version; # get version output if ( system("$which --version") == 0 ){ # some cli respond to -v instead of --version $_version = `$which --version`; } elsif ( system("$which -v") ==0 ){ $_version = `$which -v`; } else { warn("Command '$name' does not return version with -v or --version"); return; } chomp $_version; warn("got '$_version'"); # resolve to actual version string if( $_version=~/version\s+([\d\.]+)\b/ ){ warn("match 1"); $_version = $1; } elsif ( $_version=~/v\s*([\d\.]+)\b/ ){ warn("match 2"); $_version = $1; } elsif ( $_version=~/\b([\d\.]+)\b/ ){ warn("match 3"); $_version = $1; } else { warn("match missing, match into version output for '$name', didn't resolve to actual version string in '$_version'"); return; } # test against version argument to sub my @version = split(/\./, $version); my @_version = split(/\./, $_version); # only test as far as argument provided go my $count= scalar @version; # i know, there are quicker ways to do this.. but.. # this is more clear for anybody viewing the code warn "count.. $count\n"; my $index = 0; for ( 0 .. $count ){ defined $version[$index] or next; if ( $version[$index] > $_version[$index] ){ return 1; # obviously higher overall. } elsif ( $version[$index] < $_version[$index] ){ return 0; # no high enough } $index++; # just high enough next; } # if we got here, have the version we want at least return 1; } # did not check version, but have command return 1; }