Bob R
Bob R

Reputation: 617

Perl : version->parse with invalid input

I have an array of version numbers that I have read in from the output of a terminal command, unfortunately a few of them are not valid (5.2.5_076_06-beta) to be used with version::parse, I have the output "Invalid version format (version required) at get_version.pl line 8." this corresponds to the line containing version->parse($test); and the entire script terminates. How do I work around this?

use version;

my $cmd  =  "ls -l /nfs/install/ | awk '{print \$9}'";
my @vers = `$cmd`;

foreach my $test ( @vers ) {
    try { 
        version->parse($test);
    }
    catch
    {
        my $index = 0;
        $index++ until $vers[$index] eq $test;
        print $vers[$index];
        splice(@vers, $index, 1);

    }
}

my @sorted_vers = sort { version->parse( $a ) <=> version->parse( $b ) } @vers;

foreach my $version (@sorted_vers)
{
        print $version;
}

Upvotes: 0

Views: 113

Answers (1)

Grinnz
Grinnz

Reputation: 9231

The version module is for parsing Perl module versions, which have a very specific format. For your task of sorting arbitrary non-Perl versions, try Sort::Versions.

use Sort::Versions;
my @sorted_vers = sort versioncmp @vers;

Upvotes: 3

Related Questions