SPUG: NDBM questions
Colin Meyer
cmeyer at helvella.org
Thu Mar 14 18:16:23 CST 2002
On Thu, Mar 14, 2002 at 02:47:37PM -0800, Benjamin Franks wrote:
>
> I have a small NDBM database that is normally written to by a C application.
> As the keys and values stored in an NDBM database are character strings,
> I'm casting ... something roughly like.
>
> struct custom_struct {
> some complex struct stuff;
> };
>
> datum key, val;
> struct custom_struct aaa;
>
> key.dptr="bbb";
> key.dsize=strlen(key.dptr);
>
> val.dptr=(char *) &aaa;
> val.dsize=sizeof(struct custom_struct);
>
> /* db is pointer to DBM already opened */
> dbm_store(db, key, val, DBM_REPLACE);
>
> Now when I read the data from the database at a later time, I use memcpy
> to move from the stored string back to my struct, as in:
>
> val = dbm_fetch(db, key)
> memcpy((char *) &aaa, val.dptr, val.dsize);
> /* now work on struct aaa */
>
> Ok, now for the question. I'm writing a Perl application that reads from
> the same database. However, if I tie a %hash to the ndbm file and then
> print out the keys and values, the values are garbled. Is there a Perl
> equivalent to the C memcpy for byte string copies?
Perl doesn't grok C structs directly, so a memcpy in Perl doesn't
make sense.
You could attempt a scheme of digging into the struct with unpack(), but
that would be difficult and probably not very portable.
Another method would be to code a custom XS map so that Perl could
understand your particular custom_struct. The problem here is the XS
learning curve, which isn't entirely easy.
I think that Inline::C is appropriate for your problem. You can use your
same C code for accessing the NDBM file, and provide some "accessor"
functions to be made visible to the Perl side.
Take a look at the Inline::C-Cookbook manpage, and search for "Object
Oriented Inline" for some example code. I think that this is very much
what you are looking for.
>
> One more question. Let's say I open a handle to this database (or a file
> in general) in a parent and fork off children. The children inherit the
> open handle. Now, if the children close the handle explicitly, does that
> mean it closed for the parent as well? Or will the parent handle remain
> open?
A child may close a filehandle, and the same filehandle remains open in
the parent.
Have fun,
-C.
>
> Thanks,
> --Ben
>
>
> - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
> POST TO: spug-list at pm.org PROBLEMS: owner-spug-list at pm.org
> Subscriptions; Email to majordomo at pm.org: ACTION LIST EMAIL
> Replace ACTION by subscribe or unsubscribe, EMAIL by your Email-address
> For daily traffic, use spug-list for LIST ; for weekly, spug-list-digest
> Seattle Perl Users Group (SPUG) Home Page: http://seattleperl.org
>
>
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
POST TO: spug-list at pm.org PROBLEMS: owner-spug-list at pm.org
Subscriptions; Email to majordomo at pm.org: ACTION LIST EMAIL
Replace ACTION by subscribe or unsubscribe, EMAIL by your Email-address
For daily traffic, use spug-list for LIST ; for weekly, spug-list-digest
Seattle Perl Users Group (SPUG) Home Page: http://seattleperl.org
More information about the spug-list
mailing list