Tutorial Addendum on Perl - Allotment A - Alive with DBM Files
| |
This affiliate describes:
Opening DBM Files with Assortment Objects
Perl allows us to admission DBM files by bounden them to assortment objects. There are two
related functions:
1. dbmopen() - A action to bind the defined assortment capricious to the specified
DBM file. The defined affectation amount will be acclimated as the permission code,
if the defined DBM book does not exist. Of course, ahead absolute entries
of the assortment capricious will be removed at the time of binding.
rc = dbmopen(hash_variable, dbm_name, mask);
2. dbmclose() - A action to un-bind the defined assortment capricious from the binded
DBM file.
rc = dbmclose(hash_variable);
DBM Archetype - English French Dictionary
As the first archetype with DBM files, I wrote several simple programs to create
a simple English French dictionary, to amend the dictionary, and to attending up word
in the dictionary.
The first program, DbmInsert.pl, creates the concordance as a DBM file, and inserts a couple
of entries:
#- DbmInsert.pl
#- Absorb (c) 1996 by Dr. Yang
#
($name) = @ARGV;
die "Missing DBM name.
" unless $name;
dbmopen(%map,$name,0666);
$map{ angel } = pomme ;
$map{ bolt } = tissu ;
$map{ computer } = ordinateur ;
$map{ acceptable } = bon ;
$map{ milk } = lait ;
$map{ amount } = prix ;
$map{ ten } = dix ;
dbmclose(%map);
exit;
The affectation code, 0666, is an octal amount apery a permission ambience
to acquiesce every user for account and autograph (rw_rw_rw_). Active this program
gives no problem:
>DbmInsert.pl dictionary
>dir dictionary.*
0 dictionary.dir
1,024 dictionary.pag
The additional program, DbmPrint.pl, prints the absolute agreeable of the dictionary:
#- DbmPrint.pl
#- Absorb (c) 1996 by Dr. Yang
#
($name) = @ARGV;
die "Missing DBM name.
" unless $name;
dbmopen(%map,$name,0666);
while (($key,$val)=each(%map)) {
print($key, = , $val, "
");
}
dbmclose(%map);
exit;
No agitation to run this program. Apprehension that the adjustment of the entries is maintained
as the aforementioned as they were amid by the first program:
>DbmPrint.pl dictionary
apple = pomme
bible = bible
cloth = tissu
computer = ordinateur
good = bon
milk = lait
price = prix
ten = dix
The third program, DbmUpdate.pl, deletes an entry, and updates addition entry:
#- DbmUpdate.pl
#- Absorb (c) 1996 by Dr. Yang
#
($name) = @ARGV;
die "Missing DBM name.
" unless $name;
dbmopen(%map,$name,0666);
delete($map{ angel });
$map{ bolt } = vetement ;
dbmclose(%map);
exit;
|
dictionary, files, dbmclose, program, variable, dbmopen, specified, unless, dbmprint, missing, copyright, dbminsert, @argv, , dbm files, hash variable, dbm file, dbm name, name 0666, %map name, %map exit, dbmopen %map, dbmclose %map, missing dbm, yang name, name @argv, @argv die, unless name, name dbmopen, %map name 0666, dbmclose %map exit, dbmopen %map name, name dbmopen %map, yang name @argv, unless name dbmopen, specified hash variable, |
Also see ...
Run this program, and book concordance again. You will see that the new access is amid at the end of the file: DbmUpdate.pl dictionarydbmprint.pl dictionarybible = bibleco
This affiliate describes:How to accessible files for ascribe and output.How to book out data to an achievement channel.How to apprehend in data from an ascribe channe
This affiliate explains:How to accessible a agenda and apprehend its entries.How to bisect a agenda tree.A simple argument seek program.p
Displaying Agenda Timberline DirTree.plBoth Unix and Windows systems adapt book directories into a timberline structure.The afterward program. DirTree.pl, shows you how to bisect the agen
Searching Files DirGrep.plMy next program, DirGrep.pl, is to seek for curve that bout the defined regular announcement in all argument files of the defined agenda tree.
Printing Achievement to a Book HandlePrinting achievement to a book handle can be done by calling the print() function. Examples of print() action calling syntaxes:
This affiliate describes:Why we charge to catechumen Perl scripts to executable programs. How to install Perl Dev Kit and use it to catechumen Perl scripts.b
Running PDK PerlAppRunning PerlApp to catechumen a Perl Software to an executable program is simple. Let yield my DirGrep.pl as an example: DirGrep.pl
This affiliate describes:Three congenital data types: scalars, arrays, and akin arrays.How to assemble scalar objects.How scalar altar are interpreted in operations.
Scalar Item InterpretationsWhen a scalar item is acclimated in an operation, it could be interpreted in three ways depending on the blazon of amount the operation is expecting:p