Persistent::File - A Persistent Class implemented using a Text File
use Persistent::File; use English; # import readable variable names like $EVAL_ERROR eval { ### in case an exception is thrown ### ### allocate a persistent object ### my $person = new Persistent::File('people.txt'); ### define attributes of the object ### $person->add_attribute('firstname', 'ID', 'VarChar', undef, 10); $person->add_attribute('lastname', 'ID', 'VarChar', undef, 20); $person->add_attribute('telnum', 'Persistent', 'VarChar', undef, 15); $person->add_attribute('bday', 'Persistent', 'DateTime', undef); $person->add_attribute('age', 'Transient', 'Number', undef, 2); ### query the datastore for some objects ### $person->restore_where(qq{ lastname = 'Flintstone' and telnum =~ /^[(]?650/ }); while ($person->restore_next()) { printf "name = %s, tel# = %s\n", $person->firstname . ' ' . $person->lastname, $person->telnum; } }; if ($EVAL_ERROR) { ### catch those exceptions! ### print "An error occurred: $EVAL_ERROR\n"; }
This is a Persistent class that uses text files to store and retrieve objects. This class can be instantiated directly or subclassed. The methods described below are unique to this class, and all other methods that are provided by this class are documented in the Persistent documentation. The Persistent documentation has a very thorough introduction to using the Persistent framework of classes.
This class is part of the Persistent base package which is available from:
http://www.bigsnow.org/persistent ftp://ftp.bigsnow.org/pub/persistent
Before we get started describing the methods in detail, it should be noted that all error handling in this class is done with exceptions. So you should wrap an eval block around all of your code. Please see the Persistent documentation for more information on exception handling in Perl.
use Persistent::File; eval { my $person = new Persistent::File($file, $field_delimiter); }; croak "Exception caught: $@" if $@;
Allocates an object. This method throws Perl execeptions so use it with an eval block.
Parameters:
eval { ### set the data store ### $person->datastore($file, $field_delimiter); ### get the data store ### $file = $person->datastore(); }; croak "Exception caught: $@" if $@;
Returns (and optionally sets) the data store of the object. This method throws Perl execeptions so use it with an eval block.
File to use as the data store.
Delimiter used to separate the attributes of the object in the data store. This argument is optional and will be initialized to the value of the special Perl variable $; (or $SUBSCRIPT_SEPARATOR if you are using the English module) as a default.
Returns:
File used as the data store.
eval { $person->insert(); }; croak "Exception caught: $@" if $@;
Inserts an object into the data store. This method throws Perl execeptions so use it with an eval block.
See the Persistent documentation for more information.
eval { $person->update(); }; croak "Exception caught: $@" if $@;
Updates an object in the data store. This method throws Perl execeptions so use it with an eval block.
Values of the Identity attributes of the object. This argument is optional and will default to the Identifier values of the object as the default.
This argument is useful if you are updating the Identity attributes of the object and you already have all of the attribute values so you do not need to restore the object (like a CGI request with hidden fields, maybe). So you can just set the Identity attributes of the object to the new values and then pass the old Identity values as arguments to the update method. For example, if Pebbles Flintstone married Bam Bam Rubble, then you could update her last name like this:
### Pebbles already exists in the data store, but we don't ### ### want to do an extra restore because we already have ### ### all of the attribute values ### $person->lastname('Rubble'); $person->firstname('Pebbles'); ### set the rest of the attributes ... ### $person->update('Flintstone', 'Pebbles');
Or, if don't want to set all of the object's attributes, you can just restore it and then update it like this:
### restore object from data store ### if ($person->restore('Flintstone', 'Pebbles')) { $person->lastname('Rubble'); $person->update(); }
A true value if the object previously existed in the data store (it was updated), and a false value if not (it was inserted).
eval { $person->save(); }; croak "Exception caught: $@" if $@;
Saves an object to the data store. The object is inserted if it does not already exist in the data store, otherwise, it is updated. This method throws Perl execeptions so use it with an eval block.
eval { $person->delete(); }; croak "Exception caught: $@" if $@;
Deletes an object from the data store. This method throws Perl execeptions so use it with an eval block.
A true value if the object previously existed in the data store (it was deleted), and a false value if not (nothing to delete).
eval { $person->restore(@id); }; croak "Exception caught: $@" if $@;
Restores an object from the data store. This method throws Perl execeptions so use it with an eval block.
Values of the Identity attributes of the object. This method throws Perl execeptions so use it with an eval block.
A true value if the object previously existed in the data store (it was restored), and a false value if not (nothing to restore).
use Persistent::File; eval { my $person = new Persistent::File('people.txt', '|'); $person->restore_where( "lastname = 'Flintstone' and telnum =~ /^[(]?650/", "lastname, firstname, telnum DESC" ); while ($person->restore_next()) { print "Restored: "; print_person($person); } }; croak "Exception caught: $@" if $@;
Restores objects from the data store that meet the specified conditions. The objects are returned one at a time by using the restore_next method and in a sorted order if specified. This method throws Perl execeptions so use it with an eval block.
Since this is a Perl based Persistent class, the restore_where method expects the $where argument to use Perl expressions.
Conditional expression for the requested objects. The format of this expression is similar to a SQL WHERE clause. This argument is optional.
Sort expression for the requested objects. The format of this expression is similar to a SQL ORDER BY clause. This argument is optional.
The number of objects that match the conditions.
Persistent, Persistent::Base, Persistent::DBM, Persistent::Memory
You may notice some lock files (with a '.lock' extension) in the same directory as your data files. These are used to control access to the data files.
This software is definitely a work in progress. So if you find any bugs please email them to me with a subject of 'Persistent Bug' at:
winters@bigsnow.org
And you know, include the regular stuff, OS, Perl version, snippet of code, etc.
David Winters <winters@bigsnow.org>
Copyright (c) 1998-2000 David Winters. All rights reserved. This program is free software; you can redistribute it and/or modify it under the same terms as Perl itself.
To install Persistent::Base, copy and paste the appropriate command in to your terminal.
cpanm
cpanm Persistent::Base
CPAN shell
perl -MCPAN -e shell install Persistent::Base
For more information on module installation, please visit the detailed CPAN module installation guide.