#!perl use strict; use warnings; use Test::More; use Data::Dumper; my @cities = qw( Berlin Budapest London Paris Praha ); # Data driven tests: my @grepper_tests = ( { wanted_regex => qr/B/, expected_nr_found => 2, }, # many more tests to come here... ); my @found_cities; my $expected_nr_found; for my $cur_test (@grepper_tests) { my $wanted_regex = $cur_test->{wanted_regex}; cmp_ok(ref($wanted_regex), 'eq', 'Regexp', 'Regex type ok: ' . $wanted_regex); $expected_nr_found = $cur_test->{expected_nr_found}; @found_cities = grep $wanted_regex, @cities; cmp_ok(scalar(@found_cities), '==', $expected_nr_found, "I have found: " . join(", ", @found_cities)); } # Single written tests: @found_cities = grep /B/, @cities; $expected_nr_found = 2; cmp_ok(scalar(@found_cities), '==', $expected_nr_found, "I have found: " . join(", ", @found_cities)); # as soon as the regex is in a variable the whole searched array # is the result my $wanted_regex = qr/B/; cmp_ok(ref($wanted_regex), 'eq', 'Regexp', 'Regex type ok: ' . $wanted_regex); @found_cities = grep $wanted_regex, @cities; $expected_nr_found = 2; cmp_ok(scalar(@found_cities), '==', 2, "I have found: " . join(", ", @found_cities)); done_testing(); #### ~/perltest_regex_in_array$ perl 100_regex_in_array.pl ok 1 - Regex type ok: (?^:B) not ok 2 - I have found: Berlin, Budapest, London, Paris, Praha # Failed test 'I have found: Berlin, Budapest, London, Paris, Praha' # at 100_regex_in_array.pl line 31. # got: 5 # expected: 2 ok 3 - I have found: Berlin, Budapest ok 4 - Regex type ok: (?^:B) not ok 5 - I have found: Berlin, Budapest, London, Paris, Praha # Failed test 'I have found: Berlin, Budapest, London, Paris, Praha' # at 100_regex_in_array.pl line 47. # got: 5 # expected: 2 1..5 # Looks like you failed 2 tests of 5.