Perl Information Center Tutorials - Databases
These tutorials were written to help you get a quick, but thorough, understanding of Perl -
the scope of the language as well as it's specific capabilities.
| Beginners
| Built-In Functions
| Advanced
| CGI Applications
|
|
|
|
|
|
Database Support
Perl provides a basic database capability called DBM which stores a
hash in two files - a .dir file that contains the hash keys and a
.pag file that contains the values corresponding to the keys.
|
| dbmclose, dbmopen, tie, untie
|
Database Handling Syntax
To open a database, use the dbmopen function.
dbmopen (%myhash, "mydbm", 0666)
The contents of the database are placed in the hash
name (%myhash in the example above). Any values the
hash had before executing dbmopen are lost. Any changes
made to the hash while the Perl script is running are
also made immediately to the database files.
To close an open database, use the dbmclose function.
dbmclose (%myhash)
DBM does not support file locking.
Perl also provides the tie and untie functions as preferred
replacements for dbmopen/dbmclose. Examples of both are given
below.
Database Functions Reference
Here's a quick reference of the available database functions, in
alphabetical order, along with code examples.
- dbmopen - open a dbm file
dbmopen HASH, DBNAME, MASK # mask provides file permissions
Examples:
dbmopen %myhash, "myfile", 0666
- dbmclose - close a dbm file
dbmclose HASH
Examples:
dbmclose %myhash
- tie - bind a variable to an object class
tie VARIABLE, CLASSNAME, LIST
Examples:
tie %myhash, "NDBM_File", "myfile", 1, 0666
- untie - break a tie binding to a variable
untie VARIABLE
Examples:
untie %myhash
If you have any suggestions for additions to these tutorials, please let me know.
|