NAME
Persistent::DBM - A Persistent Class implemented using a DBM File
SYNOPSIS
use Persistent::DBM;
use English; # import readable variable names like $EVAL_ERROR
eval { ### in case an exception is thrown ###
### allocate a persistent object ###
my $person = new Persistent::DBM('people.dbm');
### 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";
}
ABSTRACT
This is a Persistent class that uses DBM 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
DESCRIPTION
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.
METHODS
new -- Object Constructor
use Persistent::DBM;
eval {
my $obj = new Persistent::DBM($file, $field_delimiter, $type);
};
croak "Exception caught: $@" if $@;
Allocates an object. This method throws Perl execeptions so use it with an eval block.
Parameters:
datastore -- Sets/Returns the Data Store Parameters
eval {
### set the data store ###
$obj->datastore($file, $field_delimiter, $type);
### get the data store ###
$file = $obj->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:
- $file
-
File to use as the data store.
- $field_delimiter
-
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 or if set to undef.
- $type
-
Type of DBM file to use. This is probably one of these: NDBM_File, DB_File, GDBM_File, SDBM_File, or ODBM_File. This argument is optional and will default to
AnyDBM_File
. See theAnyDBM_File
documentation for more information.
Returns:
insert -- Insert an Object into the Data Store
eval {
$obj->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 Persistent documentation for more information.
update -- Update an Object in the Data Store
eval {
$obj->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:
- @id
-
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(); }
Returns:
- $flag
-
A true value if the object previously existed in the data store (it was updated), and a false value if not (it was inserted).
See the Persistent documentation for more information.
save -- Save an Object to the Data Store
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:
- $flag
-
A true value if the object previously existed in the data store (it was updated), and a false value if not (it was inserted).
See the Persistent documentation for more information.
delete -- Delete an Object from the Data Store
eval {
$obj->delete();
### or ###
$obj->delete(@id);
};
croak "Exception caught: $@" if $@;
Deletes an object from the data store. This method throws Perl execeptions so use it with an eval block.
Parameters:
- @id
-
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.
Returns:
- $flag
-
A true value if the object previously existed in the data store (it was deleted), and a false value if not (nothing to delete).
See the Persistent documentation for more information.
restore -- Restore an Object from the Data Store
eval {
$obj->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:
- @id
-
Values of the Identity attributes of the object. This method throws Perl execeptions so use it with an eval block.
Returns:
- $flag
-
A true value if the object previously existed in the data store (it was restored), and a false value if not (nothing to restore).
See the Persistent documentation for more information.
restore_where -- Conditionally Restoring Objects
use Persistent::DBM;
eval {
my $person = new Persistent::DBM('people.dbm', '|', 'NDBM_File');
$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:
- $where
-
Conditional expression for the requested objects. The format of this expression is similar to a SQL WHERE clause. This argument is optional.
- $order_by
-
Sort expression for the requested objects. The format of this expression is similar to a SQL ORDER BY clause. This argument is optional.
Returns:
See the Persistent documentation for more information.
SEE ALSO
Persistent, Persistent::Base, Persistent::File, Persistent::Memory
NOTES
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.
BUGS
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.
AUTHORS
David Winters <winters@bigsnow.org>
COPYRIGHT
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.