NDBM_File(3pm)

πŸ’– NDBM_File πŸ’–

okay so bestie, this is the manual page for NDBM_File, part of the Perl Programmers Reference Guide β€” and yes it's a perl module page, don't @ me, we're doing this.


✨ NAME ✨

NDBM_File β€” tied access to ndbm files πŸŽ€


✨ SYNOPSIS ✨

use Fcntl;   # For O_RDWR, O_CREAT, etc.
use NDBM_File;

tie(%h, 'NDBM_File', 'filename', O_RDWR|O_CREAT, 0666)
  or die "Couldn't tie NDBM file 'filename': $!; aborting";

# Now read and change the hash
$h{newkey} = newvalue;
print $h{oldkey};
...

untie %h;

✨ DESCRIPTION ✨

So NDBM_File sets up a whole situationship between a Perl hash variable and a file in NDBM_File format. You get to poke at the data in that file like it's just a regular Perl hash β€” no cap β€” but the minute your program exits, the data stays in the file, waiting for next time your program runs. It's giving committed. πŸ’…

You use NDBM_File together with Perl's built-in tie function to make the connection between the variable and the file. The arguments to tie should be:

  1. The hash variable you want to tie. 🫢
  2. The string NDBM_File. (This is how you tell Perl to use the NDBM_File package to do all the hash functions.)
  3. The name of the file you want to tie to the hash.
  4. Flags. Use one of these, bestie:
    • O_RDONLY β€” read-only access to the data in the file.
    • O_WRONLY β€” write-only access to the data in the file.
    • O_RDWR β€” both read and write access. ✨

And if you want the file to get created when it doesn't exist yet, add O_CREAT to any of those three, like the example does. But if you omit O_CREAT and the file doesn't already exist, the tie call will fail. Tragic but true.

  1. The default permissions to use if a new file gets created. Real talk: the actual permissions get modified by the user's umask, so you should probably use 0666 here. (See "umask" in perlfunc.)

✨ DIAGNOSTICS ✨

When it fails, the tie call returns an undefined value and probably sets $! to hold the reason the file couldn't be tied. πŸ™ˆ

🌸 "ndbm store returned -1, errno 22, key "..." at ..." 🌸

This warning shows up when you try to store a key or a value that is too long. It means the change was not recorded in the database β€” like, it just didn't save, periodt. See BUGS AND WARNINGS below. 😭


πŸ”’ SECURITY AND PORTABILITY πŸ”’

Do not accept NDBM files from untrusted sources. Like, seriously, don't. 🚫

On modern Linux systems these are typically GDBM files, which are not portable across platforms. πŸ’€

The GDBM documentation doesn't imply that files from untrusted sources can be safely used with libgdbm.

Systems that don't use GDBM compatibility for ndbm support will be using a platform specific library, possibly inherited from BSD systems, where it may or may not be safe to use an untrusted file.

A maliciously crafted file might cause perl to crash or even expose a security vulnerability. That's the scary part, bestie, don't skip it.


πŸ› BUGS AND WARNINGS πŸ›

There are a number of limits on the size of the data you can store in the NDBM file. The most important one: the length of a key, plus the length of its associated value, may not exceed 1008 bytes. That's the whole vibe check. πŸ“

See "tie" in perlfunc, perldbmfilter, Fcntl.


and that's the whole page, bestie!! πŸ¦‹ this was perl v5.42.3, dated 2026-08-03 β€” NDBM_File(3pm), iconic, no notes, bye πŸ’–βœ¨πŸ‘‘

πŸ“¦ where this page comes from