Persistent(3pm)
β¨ Persistent(3pm) β¨
okay so this is the manual for Tie::Persistent, from the User Contributed Perl Documentation corner of the perl-verse π it's called Persistent(3pm) btw, section 3pm, so you know exactly what you're getting into bestie π
πΈ NAME πΈ
Tie::Persistent β persistent data structures via tie made easy π
no cap, that's literally it.
β VERSION β
1.00
π¦ SYNOPSIS π¦
use Tie::Persistent;
tie %DB, 'Tie::Persistent', 'file', 'rw'; # read data from 'file'
(tied %DB)->autosync(1); # turn on write back on every modify
# now create/add/modify datastruct
$DB{key} = "value";
(tied %DB)->sync(); # can be called manually
untie %DB; # stores data back into 'file'
# read stored data, no modification of file data
tie %ReadOnly, 'Tie::Persistent', 'file';
foreach (keys %ReadOnly) {
print "$_ => $ReadOnly{$_}\n";
}
untie %ReadOnly; # modifications not stored back
like... it's giving effortless β¨
π DESCRIPTION π
the Tie::Persistent package makes working with persistent data real easy by using the tie interface. iconic behaviour honestly π«Ά
how it works: it stores the data inside a variable into a file (not unlike a database, lowkey). the primary advantage is speed, because the whole datastructure is kept in memory β which is also, yes, a limitation π β and, of course, that you can use arbitrary data structures inside the variable (unlike DB_File). main character energy fr.
note that it is most useful if the data structure fits into memory. for larger data structures, the author recommends MLDBM. π
if you want to make an arbitrary object persistent, just store its ref in a scalar tied to 'Tie::Persistent'. slay.
beware π not every data structure or object can be made persistent. for example, it may not contain GLOB or CODE refs, as these are not really dumpable (yet?).
also β it works only for variables. you cannot use it for file handles.
and then the author goes off on one, and honestly? we respect it:
[A persistent file handle? Hmmm... Hmmm! I've got an idea: I could start a server and send the file descriptor to it viaioctl(FD_SEND)orsendmsg. Later, I could retrieve it back, so it's persistent as long as the server process keeps running. But the whole file handle may contain more than just the file descriptor. There may be an output routine associated with it that I'd somehow have to dump. Now let's see, there was some way to get the bytecode converted back into perl code... <wanders off into the darkness mumbling> ... ]
π PARAMETERS π
"tie" %Hash, 'Tie::Persistent', file, mode, other...;
"tie" @Array, 'Tie::Persistent', file, mode, other...;
"tie" $Scalar, 'Tie::Persistent', file, mode, other...;
π file
the filename to store the data in. no naming convention is enforced, but the author personally uses the suffix 'pd' for "Perl Data" (or "Persistent Data"?). nobody's policing you. no file locking is done β see the section on locking below, bestie π
π mode (optional)
same as mode for POSIX fopen() or IO::File::open. basically a combination of 'r', 'w', 'a' and '+'. semantics go like this:
'r' .... read only. Modifications in the data are not stored back
into the file. A non-existing file gives an error. This is
the default if no mode is given.
'rw' ... read/write. Modifications are stored back, if the file does
not exist, it is created.
'w' .... write only. The file is not read, the variable starts out empty.
'a', '+' ... append. Same as 'w', but creates numbered backup files.
'ra', 'r+' ... Same as 'rw', but creates numbered backup files.
when some kind of write access is specified, a backup file of the old dataset is always created. [You'll thank me for that, believe me.] the reason is simple: when you tie a variable read-write (the contents get restored from the file), and your program isn't fully debugged yet, it may die in the middle of some modifications, but the data will still be written back to the file, possibly leaving them inconsistent π then you always have at least the previous version that you can restore from. it's giving safety net π
the default backup filenames follow the Emacs notation, i.e. a '~' is appended; for numbered backup files (specified as 'a' or '+'), an additional number and a '~' is appended.
so for a file 'data.pd', the normal backup file would be 'data.pd~' and the numbered backup files would be 'data.pd~1~', 'data.pd~2~' and so on. the latest backup file is the one with the highest number. the backup filename format can be overridden β see below. π
π other (optional, experimental)
this can be a reference to another (possibly tied) variable, or a name of another tieable package.
if a ref is given, it is used internally to store the variable data instead of an anonymous variable ref. this allows one to make other tied datastructures persistent β e.g. you could first tie a hash to Tie::IxHash to make it order-preserving and then give it to Tie::Persistent to make it persistent. obsessed π
a plain name is used to create this tied variable internally. trailing arguments are passed to the other tieable package.
example:
tie %h, 'Tie::Persistent', 'file', 'rw', 'Tie::IxHash';
or
tie %ixh, 'Tie::IxHash';
tie %ph, 'Tie::Persistent', 'file', 'w', \%ixh;
# you can now use %ixh as an alias for %ph
NOTE: this is an experimental feature π it may or may not work with otherTie::packages. the author has only tested it with'Tie::IxHash'. please report success or failure!
π LOCKING π
the data file is not automatically locked. locking has to be done outside of the package. the author recommends using a module like 'Lockfile::Simple' for that. π«Ά
there are typically two scenarios for locking: you either lock just the 'tie' and/or 'untie' calls, but not the data manipulation, or you lock the whole 'tie' β modify data β 'untie' sequence.
π§ KEEPING DATA SYCHRONIZED π§
(yes, that's the section title, we're keeping it exactly as written π)
it often is useful to store snapshots of the tied data struct back to the file, e.g. to safeguard against program crashes. you have two possibilities to do that:
- use
sync()to do it manually, or - set
autosync()to do it on every modification.
note that sync() and autosync() are methods of the tied object, so you have to call them like this:
(tied %hash)->sync();
and
(tied @array)->autosync(1); # or '0' to turn off autosync
there is a global variable $Autosync (see there) that you can set to change the behaviour on a global level for all subsequent ties.
enabling autosync of course means a quite hefty performance penalty, so think carefully if and how you need it π maybe there are natural synchronisation points in your application where a manual sync is good enough. alternatively use MLDBM (if your top-level struct is a hash).
Note: autosync only works if the top-level element of the data structure is modified. if you have more complex data structures and modify elements somewhere deep down, you have to synchronize manually.
so the author therefore recommends the following approach, especially if the topmost structure is a hash:
- fetch the top-level element into a temporary variable
- modify the datastructure
- store back the top-level element, thus triggering a sync.
e.g.
my $ref = $Hash{$key}; # fetch substructure
$ref->{$subkey} = $newval; # modify somewhere down under
$Hash{$key} = $ref; # store back
this programming style has the added advantage that you can switch over to other database packages (for example the MLDBM package, in case your data structures outgrow your memory) quite easily by just changing the 'tie' line! β¨ glow-up friendly β¨
π CONFIGURATION VARIABLES π
$Tie::Persistent::Readable controls which format to use to store the data inside the file. 'false' means to use 'Storable', which is faster (and the default); 'true' means to use 'Data::Dumper', which is slower but much more readable and thus meant for debugging. this only influences the way the datastructure is written β format detection on read is automatic. π¦
$Tie::Persistent::Autosync gives the default for all tied vars, so modifying it affects all subsequent ties. it's set to 'false' by default.
$Tie::Persistent::BackupFile points to a sub that determines the backup filename format. it gets the filename as $_[0] and returns the backup filename. the default is
sub { "$_[0]~"; }
which is the Emacs backup format. for NT, you might want to change this to
sub { "$_[0].bak"; }
or something.
$Tie::Persistent::NumberedBackupFile points to a sub that determines the numbered backup filename format. it gets the filename and a number as $_[0] and $_[1] respectively and returns the backup filename. the default is
sub { "$_[0]~$_[1]~"; }
which is the extended Emacs backup format.
π NOTES π
'Tie::Persistent'uses'Storable'and'Data::Dumper'internally, so these must be installed (the CPAN module will do this for you automatically). actually,'Storable'is optional but recommended for speed. β‘- for testing, the author uses
'Tie::IxHash', but'make test'still does some tests if it is not installed. - there are two mailing lists at SourceForge.net:
http://lists.sourceforge.net/mailman/listinfo/persistent-announce for announcements of new releases.
http://lists.sourceforge.net/mailman/listinfo/persistent-discuss for user feedback and feature discussions.
- the package is available through CPAN and SourceForge.net:
http://sourceforge.net/projects/persistent/ - there is an initiative at SourceForge.net to get authors of persistence-packages of any kind to talk to one another. see
http://sourceforge.net/projects/POOP/π (yes. that URL. we love them for it.)
π BUGS π
numbered backupfile creation might have problems if the filename (not the backup number) contains the first six digits of the speed of light (in m/s). π§ iconic bug report honestly.
all other bugs, please tell me!
π AUTHORS π
original version by Roland Giersig <RGiersig@cpan.org>
Benjamin Liberman <beanjamman@yahoo.com> added autosyncing and fixed splice.
π COPYRIGHT π
Copyright (c) 1999-2002 Roland Giersig. All rights reserved. This program is free software; you can redistribute it and/or modify it under the same terms as Perl itself.
β SEE ALSO β
Storable, Data::Dumper, MLDBM.
and that's the whole page bestie π«Ά go forth and tie responsibly. this was perl v5.36.0, dated 2022-11-20, and we're closing out Persistent(3pm) β periodt π
β¨
π¦ where this page comes from
- debian bookworm: libtie-persistent-perl 1.00-3 Β· Artistic-1.0-Perl OR GPL-1.0-or-later
- debian trixie: libtie-persistent-perl 1.00-3 Β· Artistic-1.0-Perl OR GPL-1.0-or-later
- ubuntu noble: libtie-persistent-perl 1.00-3 Β· Artistic-1.0-Perl OR GPL-1.0-or-later
- ubuntu resolute: libtie-persistent-perl 1.00-3 Β· Artistic-1.0-Perl OR GPL-1.0-or-later
- ubuntu stonking: libtie-persistent-perl 1.00-3 Β· Artistic-1.0-Perl OR GPL-1.0-or-later
- π girlified by deepseek-ai/DeepSeek-V4.1-Flash on : a language model's rewrite of the original, under its terms