#!/usr/bin/env perl use strict; use warnings; use Carp; while(1) { my $town = getTown('towns.dat'); print "\nYou selected:\n"; print 'ID: ', $town->{id}, "\n"; print 'Short name: ', $town->{short}, "\n"; print 'Long name: ', $town->{long}, "\n"; print "\n"; } sub getTown { my ($fname) = @_; my $towns = loadFile($fname); while(1) { print "Select town: "; my $input = <>; chomp $input; if(defined($towns->{$input})) { return $towns->{$input}; } print "Unknown town!\n"; } } sub loadFile { my ($fname) = @_; open(my $ifh, '<', $fname) or croak($!); my %towns; while((my $line = <$ifh>)) { chomp $line; # Skip comments and empty lines next if($line eq '' || $line =~ /^\#/); print "# $line #\n"; my ($id, $short, $long) = split/\:/, $line; $towns{$short} = { id => $id, short => $short, long => $long, }; } close $ifh; return \%towns; }