Make PostgresVersion code a bit more robust and simple.
authorAndrew Dunstan <andrew@dunslane.net>
Thu, 22 Apr 2021 19:25:37 +0000 (15:25 -0400)
committerAndrew Dunstan <andrew@dunslane.net>
Thu, 22 Apr 2021 19:27:05 +0000 (15:27 -0400)
per gripe from Alvaro Herrera.

src/test/perl/PostgresVersion.pm

index 14750365acfd9c8c2e733882b67d7c50b0c64246..3f3744ccfa9a5b97542692e7b8ce32f7974483ef 100644 (file)
@@ -34,7 +34,7 @@ PostgresVersion - class representing PostgreSQL version numbers
 
 =head1 DESCRIPTION
 
-PostgresVersion encapsulated Postgres version numbers, providing parsing
+PostgresVersion encapsulates Postgres version numbers, providing parsing
 of common version formats and comparison operations.
 
 =cut
@@ -73,25 +73,22 @@ sub new
        my $class = shift;
        my $arg   = shift;
 
+       chomp $arg;
+
        # Accept standard formats, in case caller has handed us the output of a
        # postgres command line tool
-       $arg = $1
-         if ($arg =~ m/\(?PostgreSQL\)? (\d+(?:\.\d+)*(?:devel)?)/);
+       my $devel;
+       ($arg,$devel) = ($1, $2)
+         if ($arg =~  m/^(?:\(?PostgreSQL\)? )?(\d+(?:\.\d+)*)(devel)?/);
 
        # Split into an array
        my @result = split(/\./, $arg);
 
        # Treat development versions as having a minor/micro version one less than
        # the first released version of that branch.
-       if ($result[$#result] =~ m/^(\d+)devel$/)
-       {
-               pop(@result);
-               push(@result, $1, -1);
-       }
+       push @result, -1 if ($devel);
 
-       my $res = [@result];
-       bless $res, $class;
-       return $res;
+       return bless \@result, $class;
 }