我可以使用文件句柄而不是文件名来创建 DBM 文件吗?

发布于 2024-09-05 09:03:19 字数 288 浏览 1 评论 0原文

我正在使用 MLDBM 来保存一些 Perl 数据结构,我想知道是否有以下替代方案:

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

首先,我希望能够使用 STDOUT,而不是已知的文件名。然后可以将其重定向到 shell 端的文件。

我一直在搜索“tie”、“DBM”和“filehandle”等关键字,但搜索结果往往是谈论将文件句柄与事物联系起来,而不是将事物与文件句柄联系起来。

有什么建议吗?

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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

狼亦尘 2024-09-12 09:03:20

好吧, MLDBM 不会关心,因为它只是将参数传递给底层 dbm 库(例如, DB_FileGDBM_File)。但我不知道有任何 dbm 库接受文件句柄而不是文件名。另外,dbm 文件需要可查找,因此 shell 必须重定向到实际文件,而不是管道。 STDOUT 可能会以只写方式打开,这对于 dbm 文件不起作用。

如果您只是使用 MLDBM 来实现持久性,而不是因为数据库对于内存来说太大,那么您可以尝试其他方法。使用 Storable 来保存您的数据结构。它可以读取和写入已打开的文件句柄。

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.

肥爪爪 2024-09-12 09:03:20

请记住,STDOUT 是一个流,是必须像磁带一样按顺序读取的字节序列。 DBM 模块提供面向记录的持久性,您可以在其中读取和写入任意记录。

要通过 STDOUT 伪造 DBM,您需要输出某种日志格式。写入 STDOUT 似乎比使用 DBM 具有更高的优先级,因此也许不同的格式会更合适。

有了有关您申请的更多信息,我们可以提供对您更有用的建议。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文