Subject: | [PATCH] Support for systems without DB_File (really: android support) |
The attached patch moves the module away from using DB_File exclusively, and instead has it use AnyDBM_File -- This gets the module to install cleanly on Android :)
In case you're curious, most of the complexity comes from the fact that if the AnyDBM_File backend ends up being SDBM_File, and you ask it to create/use "foo.db", it will never touch that file, but instead use foo.db.pag and foo.db.dir; that means that we can't use '-r $dbfile', and instead have to try tie'ing and seeing if that succeeds.
Also, for what it's worth, this tested cleanly on OS X Mavericks for me.
Subject: | 0001-Support-for-systems-without-DB_File.patch |
From 6edab647e2c925fefb2c8f895c9805ab23e8483a Mon Sep 17 00:00:00 2001
From: Brian Fraser <fraserbn@gmail.com>
Date: Sun, 3 Aug 2014 13:13:10 +0200
Subject: [PATCH] Support for systems without DB_File
Sometimes, DB_File is not present in an OS (for example, Android)
which stops the module from building and installing.
This patch has the module use AnyDBM_File, and while still
preferring DB_File if available, it also uses any alternatives
if that fails.
---
Makefile.PL | 28 ++++++++++++++++------------
lib/Lingua/JA/Romanize/DictJA.pm | 33 +++++++++++++++++++++------------
lib/Lingua/JA/Romanize/Japanese.pm | 11 +++++++----
3 files changed, 44 insertions(+), 28 deletions(-)
diff --git a/Makefile.PL b/Makefile.PL
index 2461058..640890b 100755
--- a/Makefile.PL
+++ b/Makefile.PL
@@ -1,4 +1,5 @@
use strict;
+use File::Spec;
use ExtUtils::MakeMaker;
my $opt = {
@@ -6,7 +7,7 @@ my $opt = {
VERSION_FROM => 'lib/Lingua/JA/Romanize/Japanese.pm',
PREREQ_PM => {
'Test::More' => '0',
- 'DB_File' => '0',
+ 'AnyDBM_File' => '0',
'LWP::UserAgent' => '0',
},
};
@@ -18,16 +19,19 @@ WriteMakefile( %$opt );
sub MY::post_initialize {
my $self = shift;
- $self->{clean}->{FILES} .= "lib/Lingua/JA/Romanize/Japanese.bdb";
- $self->{PM}->{"lib/Lingua/JA/Romanize/Japanese.bdb"} = "blib/lib/Lingua/JA/Romanize/Japanese.bdb";
- my $str = "";
- $str;
-}
-sub MY::postamble {
- my $self = shift;
- my $str = "";
- $str .= "lib/Lingua/JA/Romanize/Japanese.bdb :\n";
- $str .= "\t\$(PERL) -Ilib -MLingua::JA::Romanize::DictJA -e 'Lingua::JA::Romanize::DictJA->update();'\n\n";
- $str;
+ push @INC, 'lib';
+ require Lingua::JA::Romanize::DictJA;
+
+ Lingua::JA::Romanize::DictJA->update();
+
+ my $dir = 'lib/Lingua/JA/Romanize/';
+ opendir my $dh, $dir or die $!;
+ while (my $file = readdir($dh)) {
+ next unless $file =~ /Japanese\.bd/;
+ $self->{clean}->{FILES} .= ' ' . File::Spec->catfile($dir, $file);
+ $self->{PM}->{"lib/Lingua/JA/Romanize/$file"} = "blib/lib/Lingua/JA/Romanize/$file";
+ }
+
+ "";
}
diff --git a/lib/Lingua/JA/Romanize/DictJA.pm b/lib/Lingua/JA/Romanize/DictJA.pm
index f0f035c..154ddbe 100755
--- a/lib/Lingua/JA/Romanize/DictJA.pm
+++ b/lib/Lingua/JA/Romanize/DictJA.pm
@@ -32,7 +32,7 @@ on installing this package.
=head1 REQUIRED MODULES
-L<DB_File> module is required to create cached dictionary files.
+L<AnyDBM_File> module is required to create cached dictionary files.
L<Jcode> module is required on Perl 5.8.0 or less to install
this package. L<Encode> module is used on Perl 5.8.1 or above.
L<LWP::UserAgent> module is optional and used to fetch external
@@ -94,9 +94,19 @@ sub update {
local $| = 1;
+ my $mode = 0644;
my $dbpath = $base . "/" . $DICT_DB;
- if ( -r $dbpath ) {
- print "DB_File is already exist: $dbpath\n";
+
+ print "Loading module: AnyDBM_File.pm\n";
+ &require_db_file(); # required
+
+ # Try to tie the file -- if the tie succeeds, then
+ # the file exists. We need to do it this way because
+ # the .db file may not actually be under the same name
+ # as $dbpath
+ my $btree = eval { DB_File::BTREEINFO->new(); };
+ if ( tie(my %tmp, 'AnyDBM_File', $dbpath, Fcntl::O_RDWR(), $mode, ($btree ? $btree : ())) ) {
+ print "AnyDBM_File already exist: $dbpath\n";
my $mess = 'Do you wish to overwrite this?';
my $yes = ExtUtils::MakeMaker::prompt( $mess, 'y' );
if ( $yes ne 'y' ) {
@@ -107,9 +117,6 @@ sub update {
print "Path: ", $dbpath, "\n";
}
- print "Loading module: DB_File.pm\n";
- &require_db_file(); # required
-
print "Loading module: LWP::UserAgent\n";
&require_lwp_useragent();
@@ -169,12 +176,12 @@ sub update {
# create DB_File
- print "Writing DB_File: $dbpath\n";
+ print "Writing AnyDBM_File: $dbpath\n";
my $dbhash = {};
my $flags = Fcntl::O_RDWR() | Fcntl::O_CREAT();
- my $mode = 0644;
- my $btree = DB_File::BTREEINFO->new();
- tie( %$dbhash, 'DB_File', $dbpath, $flags, $mode, $btree )
+ $btree = eval { DB_File::BTREEINFO->new(); };
+
+ tie(%$dbhash, 'AnyDBM_File', $dbpath, $flags, $mode, ($btree ? $btree : ()))
or die "$! - $dbpath\n";
my $cnt = 0;
foreach my $key ( keys %$tmphash ) {
@@ -286,10 +293,12 @@ sub is_japanese {
}
sub require_db_file {
- return if defined $DB_File::VERSION;
+ return if defined $AnyDBM_File::VERSION;
local $@;
+ @AnyDBM_File::ISA = qw(DB_File GDBM_File SDBM_File);
eval { require DB_File; };
- die "DB_File module is required.\n" if $@;
+ eval { require AnyDBM_File; };
+ die "AnyDBM_File module is required.\n" if $@;
}
sub require_encode {
diff --git a/lib/Lingua/JA/Romanize/Japanese.pm b/lib/Lingua/JA/Romanize/Japanese.pm
index 9d943a9..b53d6de 100755
--- a/lib/Lingua/JA/Romanize/Japanese.pm
+++ b/lib/Lingua/JA/Romanize/Japanese.pm
@@ -100,7 +100,11 @@ any later version.
package Lingua::JA::Romanize::Japanese;
use strict;
use Carp;
-use DB_File;
+BEGIN { @AnyDBM_File::ISA = qw(DB_File GDBM_File SDBM_File); }
+use AnyDBM_File;
+BEGIN {
+ eval { require DB_File };
+}
use Fcntl;
use base qw( Lingua::JA::Romanize::Base );
use vars qw( $VERSION );
@@ -239,12 +243,11 @@ sub _kana_line {
sub _open_dict {
my $self = shift;
my $file = shift || $self->_detect_dict();
- Carp::croak "$! - $file\n" unless ( -r $file );
my $dict = {};
my $flags = Fcntl::O_RDONLY();
my $mode = 0644;
- my $btree = DB_File::BTREEINFO->new();
- tie( %$dict, 'DB_File', $file, $flags, $mode, $btree )
+ my $btree = eval { DB_File::BTREEINFO->new() };
+ tie( %$dict, 'AnyDBM_File', $file, $flags, $mode, ($btree ? $btree : ()))
or Carp::croak "$! - $file\n";
$dict;
}
--
1.7.12.4 (Apple Git-37)