#!/usr/bin/perl # FILE: convert_to_db # File that converts a flat text file into a database, using Berkley DB. # Requires two file names on command line: # - name of text file containing product information, and # - name of database file to create. # It assumes that the text file uses | for delimiters # and has field names for number, type, name, price, and desc #Use the DBM module. use DB_File; use warnings; #Get command line arguments: ($filename, $database) = @ARGV; my %inventory; #inventory will be the name of the hash # Open the input file. open (INPUT, "$filename") || die "Can not open the file $filename : $!\n"; #Open the DBM database file. tie (%inventory, "DB_File", "$database") or die "Can not open the DBM database $database: $!\n"; #Loop through the input file and put it into the DBM database. while () { chomp; #split the line #assumes the text file uses | for delimiters and has field names # for number, type, name, price, and desc: my ($number, $type,$name,$price,$desc) = split (/\|/); #save the information into the DBM file. $inventory{$number} = "${type}|${name}|${price}|${desc}"; } #Close the dbm database untie %inventory; #Close the input file. close (INPUT);