#!/usr/bin/env perl use 5.010; use strict; use warnings; use constant { PATH => 0, VERSION => 1, WANT => 2, }; use Benchmark 'cmpthese'; use Test::More; my @tests = ( [qw{ /a/b/version1/c/d version1 /a/b/version1 }], [qw{ /some/path/3.5.2+tcl-tk-8.5.18+sqlite-3.10.0/a/b/c 3.5.2+tcl-tk-8.5.18+sqlite-3.10.0 /some/path/3.5.2+tcl-tk-8.5.18+sqlite-3.10.0 }], ); plan tests => 2*@tests; for my $test (@tests) { is _regex(@$test[PATH, VERSION]), $test->[WANT]; is _rindex(@$test[PATH, VERSION]), $test->[WANT]; } cmpthese 0 => { regex0 => sub { _regex(@{$tests[0]}[PATH, VERSION]); }, regex1 => sub { _regex(@{$tests[1]}[PATH, VERSION]); }, rindex0 => sub { _rindex(@{$tests[0]}[PATH, VERSION]); }, rindex1 => sub { _rindex(@{$tests[1]}[PATH, VERSION]); }, }; sub _regex { my ($path, $version) = @_; $path =~ s/.*\Q$version\E\K.*//s; return $path; } sub _rindex { my ($path, $version) = @_; $path = substr $path, 0, length($version) + rindex $path, $version; return $path; }