准备把本站的 Tag索引 写入 DBM ,折腾了一段Perl 小程序,使用了 NDBM_File 模块, 结果稀奇古怪。
代码如下;
把上述文件的
是什么原因呢?? 难道 NDBM_File 模块还有啥意外的用法? 看来 Perl 真的荒废很久了。。。
代码如下;
#!/usr/bin/perl -w
use strict;
use CGI;
my $tagdir ='/blog/tag';
my $q = CGI->new;
print $q->header(-charset => 'utf-8');
use NDBM_File;
use Fcntl;
&do_tag_db();
sub do_tag_db
{
my $all_tag_text = $tagdir .'/tag.txt';
return 1 unless (-s $all_tag_text);
my $all_tag_db = $tagdir .'/tag.db';
return 1 if (-s $all_tag_db);
if (open my $fh, $all_tag_text) {
tie (my %DB, 'NDBM_File', $all_tag_db,O_RDWR|O_TRUNC|O_CREAT, 0666);
my @list =split ',', <$fh>;
close $fh;
foreach my $tmp (@list) { $DB{$tmp} = &getCacheFileName($tmp) ; }
untie %DB;
}
return 0;
}
sub getCacheFileName {
my($str) = @_;
$str =~ s!([^a-zA-Z0-9_.~-])!lc sprintf "%02x", ord($1)!eg;
$str;
}
1;
程序正常运行。但是在目标目录并没有发现所需要的 tag.db 文件, 而是一对文件,名字为 tag.db.dir 和 tag.db.pag 。 把上述文件的
use NDBM_File;换成
...
tie (my %DB, 'NDBM_File', $all_tag_db,O_RDWR|O_TRUNC|O_CREAT, 0666);
use DB_File;即改用 DB_File, 则一切正常。 tag.db 正常生成。
...
tie (my %DB, 'DB_File', $all_tag_db,O_RDWR|O_TRUNC|O_CREAT, 0666);
是什么原因呢?? 难道 NDBM_File 模块还有啥意外的用法? 看来 Perl 真的荒废很久了。。。
按道理来说:
use DB_File 和 use NDBM_File 的用法应该一样,结果相同才是。