#!/usr/bin/env perl use strict; use warnings; use Carp; #use TownDatabase; my $towndb = TownDatabase->new(file => ['ustowns.dat', 'eutowns.dat']); while(1) { my $town = $towndb->get(); print "\nYou selected:\n"; print 'ID: ', $town->{id}, "\n"; print 'Short name: ', $town->{short}, "\n"; print 'Long name: ', $town->{long}, "\n"; print 'Source: ', $town->{source}, "\n"; print "\n"; } package TownDatabase; use strict; use warnings; use Carp; sub new { my ($proto, %config) = @_; my $class = ref($proto) || $proto; my $self = bless \%config, $class; $self->{towns} = {}; if(defined($self->{file})) { if(ref $self->{file} eq 'ARRAY') { foreach my $fname (@{$self->{file}}) { $self->loadFile($fname); } } else { $self->loadFile($self->{file}); } } return $self; } sub get { my ($self) = @_; while(1) { print "Select town: "; my $input = <>; chomp $input; if(defined($self->{towns}->{$input})) { return $self->{towns}->{$input}; } print "Unknown town!\n"; } } sub loadFile { my ($self, $fname) = @_; croak("File not found: $fname\n") unless(-f $fname); print "Loading file $fname...\n"; 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; $self->{towns}->{$short} = { id => $id, short => $short, long => $long, source => $fname, }; } close $ifh; }