The London Perl and Raku Workshop takes place on 26th Oct 2024. If your company depends on Perl, please consider sponsoring and/or attending.

NAME

XBase - 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

This module can read and write XBase database files, known as dbf in dBase and FoxPro world. It also reads memo fields from the dbt and fpt files, if needed. An alpha code of reading index support for ndx, ntx, mdx, idx and cdx is available for testing -- see the DBD::Index(3) man page. Module XBase provides simple native interface to XBase files. For DBI compliant database access, see the DBD::XBase and DBI modules and their man pages.

The following methods are supported by XBase module:

General methods

new

Creates the XBase object, loads the info about the table form the dbf file. The first parameter should be the name of existing dbf file (table, in fact) to read. A suffix .dbf will be appended if needed. This method creates and initializes new object, will also check for memo file, if needed.

The parameters can also be specified in the form of hash: value of name is then the name of the table, other flags supported are:

memofile specifies non standard name for the associated memo file. By default it's the name of the dbf file, with extension dbt or fpt.

ignorememo ignore memo file at all. This is usefull 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 implamentations 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 to treat the decimal value of character fields as 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.

create

Creates new database file on disk and initializes it with 0 records. A dbt (memo) file will be also created if the table contains some memo fields. Parameters to create are passed as hash.

You can call this method as method of another XBase object and then you only need to pass name value of the hash; the structure (fields) of the new file will be the same as of the original object.

If you call create using class name (XBase), you have to (besides name) also specify another four values, each being a reference to 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, just to make FoxPro happy), and version to force 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 class method (XBase->create).

The new file mustn't exist yet -- XBase will not allow you to overwrite existing table. Use drop (or unlink) to delete it first.

drop

This method closes the table and deletes it on disk (including associated memo file, if there is any).

last_record

Returns number of the last record in the file. The lines deleted but present in the file are included in this number.

last_field

Returns number of the last field in the file, number of fields minus 1.

field_names, field_types, field_lengths, field_decimals

Return 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 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 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 empty list.

get_record_nf

Instead if the names of the fields, you can pass list of numbers of the fields to read.

get_record_as_hash

Returns hash (in list context) or reference to 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 number of the record and hash as parameters, sets the fields, unspecified are undefed/emptied.

update_record_hash

Like set_record_hash but fields that do not have value specified in the hash retain their value.

To explicitely delete/undelete a record, use methods delete_record or undelete_record with 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 a code to update field MSG in 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 next record.

prepare_select

As parameters, pass list of field names to return, if 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 on file, use arrayref instead of the file name and in that array include file name and the tag name, and optionaly the index type. The fetch will then return records in the ascending order, according to the index.

Prepare will return object cursor, the following method are methods of the cursor, not of the table.

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 cursor created via prepare_select_with_index. Will roll to the first record what is equal to 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 untill the end of the index, so you have to check whether you are still on records with 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 reference to an array containing array of values for each undeleted record at once. As parameters, pass list of fields to return for each record.

To print the content of the file in a readable form, use method dump_records. It prints all not deleted records from the file. By default, all fields are printed, separated by colons, one record on a row. The method can have parameters in a form of a hash with the following 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's undef, 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 dbfdump(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 then 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; dbfdump(1)