Tutorial Addendum on Perl - Allotment A - Alive with DBM Files
| |
Run this program, and book concordance again. You will see that the new access is amid
at the end of the file:
>DbmUpdate.pl dictionary
>dbmprint.pl dictionary
bible = bible
computer = ordinateur
good = bon
milk = lait
price = prix
ten = dix
cloth = vetement
The fourth program, DbmLookup.pl, looks up a chat from the dictionary:
#- DbmLookup.pl
#- Absorb (c) 1996 by Dr. Yang
#
($name, $key) = @ARGV;
die "Missing DBM name.
" unless $name;
die "Missing key.
" unless $key;
dbmopen(%map,$name,0666);
if ($map{$key}) {
print($key, = , $map{$key}, "
");
} abroad {
print($key, " = (not defined)
");
}
dbmclose(%map);
exit;
Try it with a brace of words:
>dbmlookup.pl concordance apple
apple = (not defined)
>dbmlookup.pl concordance milk
milk = lait
This DBM archetype will appearance you how to administer a table with assorted columns. In the afterward
Perl program, BookInsert.pl, I abutting all columns into a individual cord with "
" as
the delimiter, and stored it into the hash:
#- DbmInsert.pl
#- Absorb (c) 1996 by Dr. Yang
#
($name) = @ARGV;
die "Missing DBM name.
" unless $name;
dbmopen(%map,$name,0666);
$id = &lastKey();
$id++;
$key = $id;
$title = "Programming Perl";
$author = "Larry Wall";
$date = "Jan 1991";
$isbn = "0937175641";
$edition = "1";
$val = join(" ",$title,$author,$date,$edition,$isbn);
$map{$key} = $val;
$id++;
$key = $id;
$title = "Learning Perl";
$author = " Randal L. Schwartz";
$date = "Nov 1993";
$isbn = "1565920422";
$edition = "3";
$val = join(" ",$title,$author,$date,$edition,$isbn);
$map{$key} = $val;
dbmclose(%map);
exit;
sub lastKey {
bounded $max = 0;
bounded $key, $val;
while (($key,$val)=each(%map)) {
$max = $key if ($key>$max);
}
acknowledgment $max;
}
To accompany aback alone columns from the hash, I acclimated the breach action as apparent
in the afterward program, BookPrint.pl
#- BookPrint.pl
#- Absorb (c) 1996 by Dr. Yang
#
($name) = @ARGV;
die "Missing DBM name.
" unless $name;
dbmopen(%map,$name,0666);
$rec = 0;
while (($key,$val)=each(%map)) {
$rec++;
$id = $key;
($title,$author,$date,$edition,$isbn) = split(" ",$val);
}
dbmclose(%map);
exit;
Here is the achievement of active these programs together:
>bookinsert.pl bookbase
>bookprint.pl bookbase
Record: 1
id = 1
columnist = Larry Wall
date = Jan 1991
copy = 1
isbn = 0937175641
Record: 2
id = 2
columnist = Randal L. Schwartz
date = Nov 1993
copy = 1
isbn = 1565920422
|
print, author, edition, title, dictionary, program, unless, missing, dbmlookup, bookprint, copyright, columns, dbmopen, dbmclose, @argv, , map{ key}, %map exit, dbmclose %map, title author, date edition, key val, edition isbn, author date, name 0666, missing dbm, @argv die, dbm name, unless name, %map name, dbmopen %map, yang name, author date edition, date edition isbn, title author date, dbmclose %map exit, %map name 0666, dbmopen %map name, isbn map{ key}, val each %map, edition isbn map{, author larry wall, yang name @argv, unless name dbmopen, name dbmopen %map, larry wall date, |
Also see ...
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
List Amount ConstructorsList amount constructors are acclimated to assemble account objects. A account item is about identical to an arrangement object. It represents an ordered account of