-
Notifications
You must be signed in to change notification settings - Fork 7
/
Geography.pm
64 lines (57 loc) · 1.49 KB
/
Geography.pm
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
# -*- perl -*-
#
# $Id: Geography.pm,v 1.7 2005/05/02 23:29:25 eserte Exp $
# Author: Slaven Rezic
#
# Copyright (C) 2000,2005 Slaven Rezic. All rights reserved.
# This package is free software; you can redistribute it and/or
# modify it under the same terms as Perl itself.
#
# Mail: [email protected]
# WWW: http://bbbike.de
#
package Geography;
sub new {
my($class, $city, $country, @args) = @_;
return if !defined $city && !defined $country;
my $pkg = 'Geography::' . ucfirst(lc($city)) . '_' . uc($country);
my $obj = eval 'use ' . $pkg . '; ' . $pkg . '->new(@args)';
if (!$obj) {
$obj = $class->fallback_constructor($city, $country, @args);
}
$obj;
}
sub fallback_constructor {
my($class, $city, $country, @args) = @_;
require File::Basename;
my $geo_dir = File::Basename::dirname(__FILE__). "/Geography";
my $city_obj;
if (opendir GEO, $geo_dir) {
my $search_term = quotemeta $city;
if (defined $country) {
$search_term .= ".*_" . quotemeta $country;
}
while(defined(my $f = readdir GEO)) {
next if -d $f || $f !~ /\.pm$/;
if ($f =~ /^$search_term/i) {
$f =~ s/\.pm$//;
my $citypkg = 'Geography::' . $f;
eval 'require ' . $citypkg;
die $@ if $@;
$city_obj = $citypkg->new;
last;
}
}
closedir GEO;
return $city_obj;
} else {
die sprintf("Kann das Verzeichnis %s nicht öffnen: %s",
$geo_dir, $!);
}
}
# XXX smarter? look at existing data directories?
sub default {
Geography->new("Berlin", "DE");
}
1;
__END__