#!/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;
}
####
#!/usr/bin/env perl
use strict;
use warnings;
use Carp;
#use TownDatabase;
my $towndb = TownDatabase->new(file => 'towns.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 "\n";
}
package TownDatabase;
sub new {
my ($proto, %config) = @_;
my $class = ref($proto) || $proto;
my $self = bless \%config, $class;
$self->loadFile();
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) = @_;
open(my $ifh, '<', $self->{file}) 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;
$self->{towns} = \%towns;
}
##
##
my $us_towndb = TownDatabase->new(file => 'towns_usa.dat');
my $eu_towndb = TownDatabase->new(file => 'towns_eu.dat');
##
##
#!/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;
}
##
##
my $towndb = TownDatabase->new();
foreach my $region (qw[eu us au]) {
$towndb->loadFile($region . 'towns.dat');
}