From 18e7135cca2d7ff6fccc2b14f8c1bbf7cf4554ff Mon Sep 17 00:00:00 2001 From: Daniel Friesel Date: Sun, 22 Nov 2020 19:44:25 +0100 Subject: Stations::get_station_by_name: catch invalid regular expressions --- lib/Travel/Status/DE/IRIS/Stations.pm.PL | 27 +++++++++++++-------------- 1 file changed, 13 insertions(+), 14 deletions(-) diff --git a/lib/Travel/Status/DE/IRIS/Stations.pm.PL b/lib/Travel/Status/DE/IRIS/Stations.pm.PL index b19043f..3547448 100644 --- a/lib/Travel/Status/DE/IRIS/Stations.pm.PL +++ b/lib/Travel/Status/DE/IRIS/Stations.pm.PL @@ -41,24 +41,18 @@ our $VERSION = '1.51'; my @stations = ( EOF -for my $station (@{$stations}) { - if ($station->{latlong}) { +for my $station ( @{$stations} ) { + if ( $station->{latlong} ) { $buf .= sprintf( "['%s','%s',%s,%s,%s],\n", - $station->{ds100}, - $station->{name}, - $station->{eva}, - $station->{latlong}[1], + $station->{ds100}, $station->{name}, + $station->{eva}, $station->{latlong}[1], $station->{latlong}[0], ); } else { - $buf .= sprintf( - "['%s','%s',%s],\n", - $station->{ds100}, - $station->{name}, - $station->{eva} - ); + $buf .= sprintf( "['%s','%s',%s],\n", + $station->{ds100}, $station->{name}, $station->{eva} ); } } @@ -148,7 +142,12 @@ sub get_station_by_name { my $min_dist = min(@distances); my @station_map = pairwise { [ $a, $b ] } @stations, @distances; - my @substring_matches = grep { $_->[1] =~ m{$name}i } @stations; + my @substring_matches; + + # $name may be an invalid regular expression + eval { push(@substring_matches, grep { $_->[1] =~ m{\Q$name\E}i } @stations) }; + eval { push(@substring_matches, grep { $_->[1] =~ m{$name}i } @stations) }; + my @levenshtein_matches = map { $_->[0] } grep { $_->[1] == $min_dist } @station_map; @@ -297,4 +296,4 @@ station data used by this module is also available under a CC-BY 4.0 license on L. EOF -write_file($ARGV[0], {binmode => ':utf8'}, $buf); +write_file( $ARGV[0], { binmode => ':utf8' }, $buf ); -- cgit v1.2.3