#!/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; }