|
Persistent::File - A Persistent Class implemented using a Text File |
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 the Persistent manpage documentation. The the Persistent manpage 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 the Persistent manpage 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.
Parameters:
Returns:
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.
Parameters:
Returns:
See the the Persistent manpage 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.
Parameters:
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();
}
Returns:
See the the Persistent manpage documentation for more information.
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.
Parameters:
Returns:
See the the Persistent manpage documentation for more information.
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.
Parameters:
Returns:
See the the Persistent manpage documentation for more information.
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.
Parameters:
Returns:
See the the Persistent manpage documentation for more information.
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.
Parameters:
Returns:
See the the Persistent manpage documentation for more information.
the Persistent manpage, the Persistent::Base manpage, the Persistent::DBM manpage, the Persistent::Memory manpage
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.
|
Persistent::File - A Persistent Class implemented using a Text File |