Skip to content

Commit

Permalink
Use Scalar::Util::blessed to check for object
Browse files Browse the repository at this point in the history
  • Loading branch information
nigelhorne committed Nov 5, 2024
1 parent 50a321d commit 365e312
Show file tree
Hide file tree
Showing 3 changed files with 198 additions and 192 deletions.
1 change: 1 addition & 0 deletions Makefile.PL
Original file line number Diff line number Diff line change
Expand Up @@ -349,6 +349,7 @@ WriteMakefile(
'Module::Info' => 0,
'File::Spec' => 0,
'CHI' => 0,
'Scalar::Util' => 0,
'Storable' => 0,
'Text::CSV' => 0,
'Text::xSV::Slurp' => 0,
Expand Down
12 changes: 10 additions & 2 deletions lib/Geo/Coder/Free/MaxMind.pm
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ use Locale::CA;
use Locale::US;
use CHI;
use Locale::Country;
use Scalar::Util;

our %admin1cache;
our %admin2cache; # e.g. maps 'Kent' => 'P5'
Expand Down Expand Up @@ -107,6 +108,8 @@ The admin2.db is far from comprehensive, see Makefile.PL for some entries that a
sub new
{
my $class = shift;

# Handle hash or hashref arguments
my %args = (ref($_[0]) eq 'HASH') ? %{$_[0]} : @_;

if(!defined($class)) {
Expand All @@ -116,21 +119,26 @@ sub new

# FIXME: this only works when no arguments are given
$class = __PACKAGE__;
} elsif(ref($class)) {
# clone the given object
} elsif(Scalar::Util::blessed($class)) {
# If $class is an object, clone it with new arguments
return bless { %{$class}, %args }, ref($class);
}

my $directory = $args{'directory'} || Module::Info->new_from_loaded(__PACKAGE__)->file();
$directory =~ s/\.pm$//;

if(!-d $directory) {
Carp::croak(ref($class), ": directory $directory doesn't exist");
}

Database::Abstraction::init({
cache_duration => '1 day',
%args,
directory => File::Spec->catfile($directory, 'databases'),
cache => $args{cache} || CHI->new(driver => 'Memory', datastore => {}),
});

# Return the blessed object
return bless { }, $class;
}

Expand Down
Loading

0 comments on commit 365e312

Please sign in to comment.