开发者

How to use perl dbmopen on Windows and Linux

I have a perl script that runs fine on Linux but fails on Windows at this point:

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

$dictfile points to the proper location on the respective platforms. Changing the 0666 file permissions does not help. The file to open is a text file encoded in gb18030.

Is there a trick? Do I need to declare the encoding to open it on Window? Or possibly a different perl distro 开发者_开发百科on Windows. I'm using Strawberry Perl.

Thanks.


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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜