put#
Store a value under a key, with flags that say where and whether.
Synopsis#
my $status = $db->put($key, $value);
$status = $db->put($key, $value, R_NOOVERWRITE);
What you get back#
0 when the value was stored, 1 when R_NOOVERWRITE found the key already there, -1 for an error.
Flags#
none - store the value, replacing the key’s; in an
R_DUPdatabase, add it after the key’s other values.R_NOOVERWRITE- store only if the key is not there yet.R_CURSOR- replace the value the database’s cursor stands on; the key given is ignored.R_SETCURSOR- store, then set the cursor on the key (its first value, in anR_DUPdatabase).R_IAFTER,R_IBEFORE- put a new array element after or before element$key, the ones after it moving up; in anR_DUPdatabase, a new value after or before the key’s first.$keymust be a variable: the method writes it back.
Examples#
print $db->put('a', 1); # 0
print $db->put('a', 2, R_NOOVERWRITE); # 1, and 'a' is still 1
Insert into an array:
@lines = qw(one three);
my $i = 0;
$db->put($i, 'two', R_IAFTER); # one two three
Edge cases#
R_IAFTERandR_IBEFOREleave$keyas it was; they return -1 for an element past the end or created empty, and in a hash database withoutR_DUP($DB_File::Errorthen says the flag is illegal).R_CURSORwith no cursor set yet returns -1. After a delete at the cursor, a$DB_HASHor$DB_BTREEdatabase returns-30987(Berkeley DB’sDB_NOTFOUND) and stores nothing, while an array gets the deleted element back with the new value.A fixed-length array refuses a value longer than its records: -1, with
$DB_File::Errorgiving both lengths.On a database opened read-only it returns -1.
Differences from upstream#
Fully compatible with upstream. Covered by t/81-xs-native/DB_File/010-phenotype.t.
See also#
get- reads a value backdel- removes a key, or the item at the cursorseq- sets the cursorR_CURSORwrites at