0

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.

tahoar
  • 1,788
  • 3
  • 20
  • 36

1 Answers1

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.

Sodved
  • 8,428
  • 2
  • 31
  • 43
  • Thanks Sodved. I'm using Strawberry perl 5.10.1 on WinXP SP3 32-bit and Wine 1.3.19. How do I check if I installed a DBM module? I didn't see any choices during the installation. Also, I'm not a perl coder. How do I add the $! to the die statement? – tahoar May 06 '11 at 15:23
  • Thanks again. I'm trying to patch this perl script with minimal re-write (my perl skills are nill). The command shows that DB_File: 1.82 and GDBM_File: 1.090001 and SDBM_File: 1.06 are available. Adding the $! variable within the quotes did not add anything to the error message. I think I'll uninstall 5.10 and install 5.12. – tahoar May 06 '11 at 15:58
  • @tahoar try running the program when the file does not already exist. If it works then theres a problem with your file. See the comment I put at the top of my answer – Sodved May 06 '11 at 17:11
  • Same problem 5.10 and 5.12 StrawberryPerl and ActivePerl. The script tests if the db file exists and uses `dbmopen`. If not present, `dbmopen` opens a raw text file, hash it, save a db file and use it. This works on Linux but fails on Windows. Strangely, using the Linux-created db file on Windows also fails to open with `dbmopen`. I have two different scripts that use this approach to load dictionaries. Both fail on Windows. I'll have to find alternates to these on Windows platform. Thanks again for your troubleshooting help, but this remains an unresolved incompatibility. – tahoar May 07 '11 at 05:02