XBase(3pm)
π XBase π
okay bestie, come get your dbf files β this is the manual page for XBase(3pm), a Perl module from the User Contributed Perl Documentation. π
πΈ NAME πΈ
XBase β a Perl module for reading and writing the dbf files π
π¦ SYNOPSIS π¦
use XBase;
my $table = new XBase "dbase.dbf" or die XBase->errstr;
for (0 .. $table->last_record) {
my ($deleted, $id, $msg)
= $table->get_record($_, "ID", "MSG");
print "$id:\t$msg\n" unless $deleted;
}
β¨ DESCRIPTION β¨
Okay so here's the vibe: this module can read and write XBase database files β you know them as dbf in the dBase and FoxPro world. π
It also reads memo fields from the dbt and fpt files, if you need those. And there's an alpha code of reading index support for ndx, ntx, mdx, idx and cdx available for testing β go peek at the DBD::Index(3) man page for that one. π
Module XBase provides a simple native interface to XBase files, no cap. And if what you actually want is DBI compliant database access, that's a whole different girl β go see the DBD::XBase and DBI modules and their man pages. π«Ά
The following methods are supported by the XBase module:
π General methods π
new
Creates the XBase object and loads the info about the table from the dbf file. The first parameter should be the name of an existing dbf file (a table, fr) that you want to read. A .dbf suffix will be appended if needed. This method creates and initializes the new object, and it will also check for the memo file, if needed. β¨
The parameters can also be specified in the form of a hash: the value of name is then the name of the table, and the other flags it supports are:
memofileβ specifies a non standard name for the associated memo file. By default it's the name of the dbf file, with extensiondbtorfpt.ignorememoβ ignore the memo file at all. This is useful if you've lost the dbt file and you do not need it. Default is false.memosepβ separator of memo records in the dBase III dbt files. The standard says it should be"\x1a\x1a". There are however implementations that only put in one"\x1a". XBase.pm tries to guess which is valid for your dbt, but if it fails, you can tell it yourself.nolongcharsβ prevents XBase from treating the decimal value of character fields as the high byte of the length β there are some broken products around producing character fields with decimal values set. π
my $table = new XBase "table.dbf" or die XBase->errstr;
my $table = new XBase "name" => "table.dbf",
"ignorememo" => 1;
recompute_lastrecnoβ forces XBase.pm to disbelieve the information about the number of records in the header of the dbf file and recompute the number of records. Use this only if you know that some other software of yours produces incorrect headers. β οΈ
close
Closes the object/file, no arguments. That's it, that's the whole method. π
create
Creates a new database file on disk and initializes it with 0 records. A dbt (memo) file will also be created if the table contains some memo fields. Parameters to create are passed as a hash. β¨
You can call this method as a method of another XBase object, and then you only need to pass the name value of the hash β the structure (fields) of the new file will be the same as the original object's. π―
If you call create using the class name (XBase), you have to (besides name) also specify another four values, each being a reference to a list: field_names, field_types, field_lengths and field_decimals. The field types are specified by one letter strings (C, N, L, D, ...). If you set some value as undefined, create will make it into some reasonable default. π
my $newtable = $table->create("name" => "copy.dbf");
my $newtable = XBase->create("name" => "copy.dbf",
"field_names" => [ "ID", "MSG" ],
"field_types" => [ "N", "C" ],
"field_lengths" => [ 6, 40 ],
"field_decimals" => [ 0, undef ]);
Other attributes are memofile for non standard memo file location, codepage to set the codepage flag in the dbf header (it does not affect how XBase.pm reads or writes the data though, it's just to make FoxPro happy), and version to force a different version of the dbt (dbt) file. The default is the version of the object from which you create the new one, or 3 if you call this as a class method (XBase->create).
β οΈ The new file mustn't exist yet β XBase will not allow you to overwrite an existing table. Usedrop(orunlink) to delete it first. π
drop
This method closes the table and deletes it on disk (including the associated memo file, if there is any). Poof, gone. β¨
last_record
Returns the number of the last record in the file. The lines deleted but present in the file are included in this number. π
last_field
Returns the number of the last field in the file, which is the number of fields minus 1.
field_names, field_types, field_lengths, field_decimals
Return the list of field names and so on for the dbf file. π
field_type, field_length, field_decimal
For a field name, returns the appropriate value. Returns undef if the field doesn't exist in the table.
π Reading the data one by one π
When you're dealing with the records one by one β reading or writing (the following six methods) β you have to specify the number of the record in the file as the first argument. The range is 0 .. $table->last_record. π
get_record
Returns a list of data (field values) from the specified record (line of the table). The first parameter in the call is the number of the record. If you do not specify any other parameters, all fields are returned in the same order as they appear in the file. You can also put a list of field names after the record number, and then only those will be returned. The first value of the returned list is always the 1/0 "_DELETED" value saying whether the record is deleted or not, so on success, get_record never returns an empty list. π
get_record_nf
Instead of the names of the fields, you can pass a list of numbers of the fields to read. π’
get_record_as_hash
Returns a hash (in list context) or a reference to a hash (in scalar context) containing field values indexed by field names. The name of the deleted flag is "_DELETED". The only parameter in the call is the record number. The field names are returned as uppercase. π
βοΈ Writing the data βοΈ
All three writing methods always undelete the record. On success they return true β the record number actually written. πͺ
set_record
As parameters, takes the number of the record and the list of values of the fields. It writes the record to the file. Unspecified fields (if you pass less than you should) are set to undef/empty.
set_record_hash
Takes the number of the record and a hash as parameters, sets the fields, unspecified ones are undefed/emptied.
update_record_hash
Like set_record_hash but fields that do not have a value specified in the hash retain their value. π«Ά
To explicitly delete/undelete a record, use the methods delete_record or undelete_record with the record number as a parameter.
Assorted examples of reading and writing:
my @data = $table->get_record(3, "jezek", "krtek");
my $hashref = $table->get_record_as_hash(38);
$table->set_record_hash(8, "jezek" => "jezecek",
"krtek" => 5);
$table->undelete_record(4);
This is code to update the field MSG in the record where ID is 123.
use XBase;
my $table = new XBase "test.dbf" or die XBase->errstr;
for (0 .. $table->last_record) {
my ($deleted, $id) = $table->get_record($_, "ID")
die $table->errstr unless defined $deleted;
next if $deleted;
$table->update_record_hash($_, "MSG" => "New message")
if $id == 123;
}
π Sequentially reading the file π
If you plan to sequentially walk through the file, you can create a cursor first and then repeatedly call fetch to get the next record. π§
prepare_select
As parameters, pass a list of field names to return; if there are no parameters, the following fetch will return all fields.
prepare_select_with_index
The first parameter is the file name of the index file, the rest is as above. For index types that can hold more index structures in one file, use an arrayref instead of the file name, and in that array include the file name and the tag name, and optionally the index type. The fetch will then return records in ascending order, according to the index. π
Prepare will return an object cursor β the following methods are methods of the cursor, not of the table. Don't forget it bestie. π
fetch
Returns the fields of the next available undeleted record. The list thus doesn't contain the "_DELETED" flag, since you are guaranteed that the record is not deleted. β¨
fetch_hashref
Returns a hash reference of fields for the next non deleted record.
last_fetched
Returns the number of the record last fetched.
find_eq
This only works with a cursor created via prepare_select_with_index. Will roll to the first record that is equal to the specified argument, or to the first greater if there is none equal. The following fetches then continue normally.
Examples of using cursors:
my $table = new XBase "names.dbf" or die XBase->errstr;
my $cursor = $table->prepare_select("ID", "NAME", "STREET");
while (my @data = $cursor->fetch) {
### do something here, like print "@data\n";
}
my $table = new XBase "employ.dbf";
my $cur = $table->prepare_select_with_index("empid.ndx");
## my $cur = $table->prepare_select_with_index(
["empid.cdx", "ADDRES", "char"], "id", "address");
$cur->find_eq(1097);
while (my $hashref = $cur->fetch_hashref
and $hashref->{"ID"} == 1097) {
### do something here with $hashref
}
The second example shows that after you have done find_eq, the fetches continue until the end of the index, so you have to check whether you are still on records with the given value. And if there is no record with value 1097 in the indexed field, you will just get the next record in the order. π
The updating example can be rewritten to:
use XBase;
my $table = new XBase "test.dbf" or die XBase->errstr;
my $cursor = $table->prepare_select("ID")
while (my ($id) = $cursor->fetch) {
$table->update_record_hash($cursor->last_fetched,
"MSG" => "New message") if $id == 123
}
π¦ Dumping the content of the file π¦
A method get_all_records returns a reference to an array containing an array of values for each undeleted record at once. As parameters, pass a list of fields to return for each record. π
okay bestie so to print the content of the file in a readable form, you're gonna use the dump_records method. π It prints all the not deleted records from the file. By default, all fields are printed, separated by colons, one record on a row. The method can take parameters as a hash with these keys:
rsβ Record separator, string, newline by default.fsβ Field separator, string, one colon by default.fieldsβ Reference to a list of names of the fields to print. By default it'sundef, meaning all fields.undefβ What to print for undefined (NULL) values, empty string by default.
Example of use is
use XBase;
my $table = new XBase "table" or die XBase->errstr;
$table->dump_records("fs" => " | ", "rs" => " <-+\n",
"fields" => [ "id", "msg" ]);'
Also note that there is a script dbf_dump(1) that does the printing. π«Ά
β¨ Errors and debugging β¨
If the method fails (returns false or null list), the error message can be retrieved via errstr method. If the new or create method fails, you have no object so you get the error message using class syntax "XBase->errstr()".
The method header_info returns (not prints) string with information about the file and about the fields. π
Module XBase::Base(3) defines some basic functions that are inherited by both XBase and XBase::Memo(3) module. π§¬
π DATA TYPES π
The character fields are returned "as is". No charset or other translation is done. The numbers are converted to Perl numbers. The date fields are returned as 8 character string of the 'YYYYMMDD' form and when inserting the date, you again have to provide it in this form. No checking for the validity of the date is done. The datetime field is returned in the number of (possibly negative) seconds since 1970/1/1, possibly with decimal part (since it allows precision up to 1/1000 s). To get the fields, use the gmtime (or similar) Perl function. β°
If there is a memo field in the dbf file, the module tries to open file with the same name but extension dbt, fpt or smt. It uses module XBase::Memo(3) for this. It reads and writes this memo field transparently (you do not know about it) and returns the data as single scalar. π
π INDEX, LOCKS π
New: A support for ndx, ntx, mdx, idx and cdx index formats is available with alpha status for testing. Some of the formats are already rather stable (ndx). Please read the XBase::Index(3) man page and the eg/use_index file in the distribution for examples and ideas. Send me examples of your data files and suggestions for interface if you need indexes. π¬
General locking methods are locksh, lockex and unlock for shared lock, exclusive lock and unlock. They call flock but you can redefine them in XBase::Base package. π
π INFORMATION SOURCE π
This module is built using information from and article XBase File Format Description by Erik Bachmann, URL
http://www.clicketyclick.dk/databases/xbase/format/
Thanks a lot. π
π·οΈ VERSION π·οΈ
1.08
π AVAILABLE FROM π
http://www.adelton.com/perl/DBD-XBase/
π AUTHOR π
(c) 1997--2017 Jan Pazdziora.
All rights reserved. This package is free software; you can redistribute it and/or modify it under the same terms as Perl itself.
Contact the author at jpx dash perl at adelton dot com. π§
π THANKS π
Many people have provided information, test files, test results and patches. This project would not be so great without them. See the Changes file for (I hope) complete list. Thank you all, guys! π«Ά
Special thanks go to Erik Bachmann for his great page about the file structures; to Frans van Loon, William McKee, Randy Kobes and Dan Albertsson for longtime cooperation and many emails we've exchanged when fixing and polishing the modules' behaviour; and to Dan Albertsson for providing support for the project. π
π SEE ALSO π
perl(1); XBase::FAQ(3); DBD::XBase(3) and DBI(3) for DBI interface; dbf_dump(1)
and that's a wrap, bestie! This page is from perl v5.34.0, dated 2022-06-13, and it's XBase(3pm). π
β¨
π¦ where this page comes from
- debian bookworm: libdbd-xbase-perl 1:1.08-2 Β· Artistic-1.0-Perl OR GPL-1.0-or-later
- debian trixie: libdbd-xbase-perl 1:1.08-2 Β· Artistic-1.0-Perl OR GPL-1.0-or-later
- ubuntu noble: libdbd-xbase-perl 1:1.08-2 Β· Artistic-1.0-Perl OR GPL-1.0-or-later
- ubuntu resolute: libdbd-xbase-perl 1:1.08-2 Β· Artistic-1.0-Perl OR GPL-1.0-or-later
- ubuntu stonking: libdbd-xbase-perl 1:1.08-2 Β· 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