0

我有一个在 Linux 上运行良好但在 Windows 上失败的 perl 脚本:

$freq{total} = 0;
dbmopen(%freq,$dictfile,0666) || die "Error: Cannot open dbmfile $dictfile";

$dictfile指向相应平台上的正确位置。更改0666文件权限没有帮助。要打开的文件是一个以gb18030.

有诀窍吗?我是否需要声明编码才能在 Window 上打开它?或者可能是 Windows 上的不同 perl 发行版。我正在使用草莓 Perl。

谢谢。

4

1 回答 1

0

Edit: Sorry, if I'm stating the obvious, but I just re-read the question. When you say

The file to open is a text file encoded in gb18030.

Do you mean a plain text file?

If so I think thats your problem. dbmopen is for indexed database file, ideally created by dbmopen in a previous run of you perl program. For plain text files you cannot bind them to hashes.

My previous resonse...

It works for me on Windows with Strawberry perl 5.12.1 running on Windows7x64. Which windows perl are you using? Make sure your installation has at least one of the DBM modules with it.

Some other points which might help:

  • You should include $! in your die statement, it will give you the error message for the failed open. So hopefully answer your question.
  • dbmopen will clear the contents of the %freq hash, so you will lose $freq{total} (because its 0 you may not notice). Usual pattern is: dbmopen, change some hash values, dbmclose

Edits:

$! is the variable which contains the error test of any failed "system" call. So you open line should be something like:

dbmopen(%freq,$dictfile,0666) || die "Error: Cannot open dbmfile $dictfile: $!";

To check for the standard DBM modules you can run the following from the command prompt

for %m in ( DB_File GDBM_File SDBM_File NDBM_File ODBM_File ) do @perl -M%m -e "print qq(%m: $%m::VERSION\n)"

For me that gives:

DB_File: 1.82
GDBM_File: 1.10
SDBM_File: 1.06
Can't locate NDBM_File.pm in @INC (@INC contains: C:/Nerd/StrawberryPerl/perl/site/lib C:/Nerd/StrawberryPerl/perl/vendor/lib C:/Nerd/StrawberryPerl/perl/lib .)
.
BEGIN failed--compilation aborted.
Can't locate ODBM_File.pm in @INC (@INC contains: C:/Nerd/StrawberryPerl/perl/site/lib C:/Nerd/StrawberryPerl/perl/vendor/lib C:/Nerd/StrawberryPerl/perl/lib .)
.
BEGIN failed--compilation aborted.

Which effectively meands I have DB_File, GDBM_File, and SDBM_File. But not NDBM_File or ODBM_File. Sorry I don't know how to find out which module dbmopen uses by default.

Personally I always use a specific module and then use the tie operator instead of dbmopen.

于 2011-05-06T15:11:18.613 回答