tags:

views:

41

answers:

2

I'm using MLDBM to persist some Perl data structures and I'm wondering if there's an alternative to the following:

tie %hash, "MLDBM", $dbm_file, O_CREAT | O_RDWR, 0644;

Primarily, I'd like to be able to use STDOUT, rather than a known file name. This could then be redirected to a file on the shell-side.

I've been searching with keywords like "tie", "DBM" and "filehandle", but the hits tend to talk about tying filehandles to things, as opposed to things to filehandles.

Any suggestions?

+2  A: 

Remember that STDOUT is a stream, a sequence of bytes that must be read sequentially like a tape. The DBM modules provide record-oriented persistence where you can read from and write to arbitrary records.

To fake DBM over STDOUT, you would need to output some sort of journal format. Writing to STDOUT seems to have higher priority than using DBM, so maybe a different format would be more appropriate.

With more information about your application, we could offer suggestions that will be more useful to you.

Greg Bacon
+3  A: 

Well, MLDBM wouldn't care, as it just passes the parameters to the underlying dbm library (e.g., DB_File or GDBM_File). But I'm not aware of any dbm library that accepts a filehandle instead of a filename. Also, a dbm file will need to be seekable, so the shell would have to be redirecting to an actual file, not a pipe. And STDOUT would probably be opened write-only, which wouldn't work for a dbm file.

If you're just using MLDBM for persistence, and not because the database is too big for memory, then you could try a different approach. Use Storable to persist your data structures. It can read & write to an already-open filehandle.

cjm