[Date Prev][Date Next][Thread Prev][Thread Next] [Search] [Date Index] [Thread Index]

Re: [MacPerl-AnyPerl] About This .pm Stuff



At 00.01 -0400 1999.05.02, Richard Gordon wrote:
>1. I am using a text file as the data source. I read it into an
>array, split, do the the tie, then load the tied hash with the array.
>Is there a way to read the text file directly into the hash without
>passing it thru an array first?

It depends on what your data looks like.  For data where you have key/value
pairs separated by a |, you can do:

  %hash = map {split /\|/, $_, 2} <FILE>;

If you have a line without a pipe, though, this will give you problems,
because only one element from that line will go into the hash, and the next
key will become the previous key's value instead of becoming a new key.  It
all depends on your data.


>2. The tie statement is supposed to be creating a file that I called
>source.dbm in a particular directory, but nothing is there. I am using
>tie (%thehash, '/home/source.dbm', O_ RDWR|O_CREAT, 0644)
>and assumed that if Perl couldn't find the existing file, it would
>create it and that I could skip reading the original text file from
>another directory into an array in the future because it would be
>persistent. What am I missing here?

Well, you can't have a space between O_ and RDWR, and you should not put
".dbm" on the filename.  Some DBM libraries will put an extension on for
you, some won't.  Just leave it off and you'll be better off.  Also, the
tie statement is wrong.  You need:

  tie VARIABLE, CLASSNAME, LIST

So:

  tie(%thehash, 'AnyDBM_File', $filename, $flags, $permissions) or die $!;

Let us know if that doesn't work.  Also, are you using -w and use strict?
Further, are you checking for errors, as my example did?

Bart gave good suggestions for question 3, and I don't think I have
anything else to add to it.

--
Chris Nandor          mailto:pudge@pobox.com         http://pudge.net/
%PGPKey = ('B76E72AD', [1024, '0824090B CE73CA10  1FF77F13 8180B6B6'])



==== Want to unsubscribe from this list?
==== Send mail with body "unsubscribe" to macperl-anyperl-request@macperl.org