Search code examples
perlversion

How to check if my build version is above x.x.x in perl


I'm trying to check if my etcd version is above 3.4.0 or not

Below is the command I use to fetch the version

podman exec -it etcd-pod-etcd etcdctl version

The final output from the above command will be in string format $output = '3.4.15'

I tried to validate the version in the following way $output =~ 3.4

But the problem here is if the version changes to 3.5.x I will have to change my script again.

What is the best way to check if the version is above 3.4.x? Is there any modules in Perl to do the following?


Solution

  • Perl's version objects and version pragma make it trivial:

    #!/usr/bin/env perl
    use strict;
    use warnings;
    use 5.010; # This needs at least 5.10, which hopefully isn't an issue these days
    use feature qw/say/;
    use version 0.77;
    
    my $vers = version->parse('3.4.15');
    if ($vers > v3.4.0) {
        say "Version $vers is new enough.";
    } else {
        say "Version $vers is too old. You need to upgrade.";
    }