I have an external command dep, that requires also a min version.
For example, xargs.

I've looked on cpan something to test that we a) have an external command available, and b) have at least that version, I can't find this Test::ExternalCommand like module.
Am I looking in the wrong place?

This has been my hack .. I know it's .. well.. hackish.

#!/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 retu +rn; $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 --ve +rsion"); 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', d +idn'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; }

I'm sure there's something out there that doesn't list look like it works, but actually works. (?) Any suggestions where that may be?

Would be useful for testing c libs etc. Maybe then, this should simply be coded into the makefile? They must have this down pat by now (?)

Code updated


In reply to test for non perl command dependency by leocharre

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.