Write out data into a DBM database : DBM « Database « Perl






Write out data into a DBM database

   

#The dbmopen function associates a DBM database with a Perl hash or associative array. 
#The dbmopen function takes the following syntax: 

#dbmopen(%hash, $database, $mode) or die "Cant open \"$database\" due to $!";

#The $mode value contains the file permissions used to create the DBM file if it doesnt exist.
         
#When youre done with a DBM database, call dbmclose to close it: 
#dbmclose(%hash)
#You pass the hash to dbmclose, not the database file name.


#!/usr/bin/perl -w

$directory = "db";
$database  = "mydb";

# Read directory.
opendir(DIR, $directory) or die
  "Cant open \"$directory\" due to $!.";
  
@entries = readdir(DIR);

closedir(DIR);
@sorted = sort(@entries);

print "Read $directory.\n";
$mode = 0666;

dbmopen(%execs, $database, $mode) or die "Cant open \"$database\" due to $!";
  
foreach $entry (@sorted) {
    print "$entry\n";
    $fullname = $directory . "/" . $entry;

    # Dont store if . or ..
    if ( ( -x $fullname ) && 
         ( ! -d $fullname ) && 
         ($entry !~ /^\./ ) ) {

        $execs{$entry} = $fullname;

        print "Storing $entry=$fullname\n";
    }
}

dbmclose(%execs);

   
    
    
  








Related examples in the same category

1.Creating and Assigning Data to a DBM File
2.Open DBM up for read write access
3.Opening an MLDBM database is similar to opening a regular DBM database:
4.Reads DBM file, printing entries with tie and untie function
5.Store hashes in a DBM file:
6.Using DBM databases with reports
7.Copying from One DBM Format to Another