首页 > 解决方案 > DBM 文件可以被制作它的机器上的 Perl 脚本读取,但在其他机器上“文件类型或格式不合适”

问题描述

我有一个 Perl 脚本,它使用DB_File模块创建参考文件的 DBM 索引。然后另一个 Perl 脚本使用该 DBM 文件。如果我在同一台机器上同时运行设置和使用脚本,它工作正常。

但是,如果我在机器 A 上制作 DBM 文件并将其复制到机器 B,则机器 B 上的使用脚本无法使用 DBM。

参考文件(names.txt):

2   |   Bacteria    |   Bacteria <bacteria> |   scientific name |
4640    |   Musa    |       |   scientific name |
9606    |   Homo sapiens    |       |   scientific name |

制作 DBM 的安装脚本:

#!/usr/bin/perl

    use strict;
    use warnings;
    use DB_File;
    use Fcntl;

my $namesfile = "names.txt";

my $namesfileDBMids = $namesfile . '_IDs.dbm';     
my %namesfileDBMids = (); # Start the hash that will fill the DBM file.
tie (%namesfileDBMids, "DB_File", $namesfileDBMids, O_RDWR|O_CREAT, 0666, $DB_HASH) or die "Can't open $namesfileDBMids.\n$!\n";

open (my $names_filehandle, $namesfile) or die "Could not open $namesfile.\n$!\n"; # Open the input file and fill the hash.
while (1) { # Run this loop until "last" is called.
            my $line = <$names_filehandle>; # Read the next line from the names file.

            if (! defined $line) { last }; # If there is no next line, exit the loop. You've processed the whole file.

            my @line = split(/\|/, $line); # Otherwise, split the line by | characters.
            my $name = $line[1];
            $name =~ s/^\s+|\s+$//g; # Trim whitespace off the start and end.
            my $ID = $line[0];
            $ID =~ s/^\s+|\s+$//g;
            $namesfileDBMids{$ID} = $name; # Store in the hash.
}

close $names_filehandle;
untie %namesfileDBMids;
print "Finished indexing.\n";  

最后,这是使用 DBM 的使用脚本:

#!/usr/bin/perl

    use strict;
    use warnings;
    use DB_File;
    use Fcntl;

my $namesfileDBMids = "names.txt_IDs.dbm";

my $ID_to_look_up = 9606;

my %namesfileDBMids = (); # Set up a hash to hold the DBM file.
tie (%namesfileDBMids, "DB_File", $namesfileDBMids) or die "Can't open $namesfileDBMids: $!\n";

if (exists $namesfileDBMids{$ID_to_look_up}) {
    my $name = $namesfileDBMids{$ID_to_look_up};
    print "Found a name for ID $ID_to_look_up: $name\n";
} else {
    print "Couldn't find $ID_to_look_up in the names file.\n";
}

当使用脚本可以访问 DBM 文件时,它会返回以下行:

Found a name for ID 9606: Homo sapiens

当使用脚本无法访问 DBM 文件时,它要么返回这个(在机器 B 上,当 DBM 来自 A 时):

Can't open names.dmp_IDs.dbm: Inappropriate file type or format

或者这个(当 DBM 来自 B 时,在机器 A 上):

Can't open names.dmp_IDs.dbm:

我不确定为什么 A 上没有错误消息。这是我访问受限的 Linux 服务器。

标签: perldbm

解决方案


您的不同版本的 Perl 很可能是使用在二进制级别不兼容的不同版本的 DBM 库构建的。

我建议切换到基于文本的交换格式,如 YAML 或 JSON。


推荐阅读