Here is a rather verbose solution:
#!/usr/bin/perl -w
use strict;
use Data::Dumper;
print "Enter the part you which to search for: ";
my $part = <STDIN>;
chomp($part);
my $searchresult = &search(part => $part);
if (defined $searchresult) {
print "Located part No. $part: $$searchresult[1]\n";
} else {
print "Your part ($part) could not be found...\n";
}
# This routine will accept a part number as an anonymous
# hash, and seach thru' a text file returning the entire
# record (pipe delimited) of the 1st occurrence
sub search {
my %args = @_;
my $retval;
local*FH;
open (FH, './parts.txt') || die "Cannot open file: ($!)";
my @parts = <FH>;
foreach my $line (@parts) {
my @fields = split(/\|/, $line);
if ($args{part} eq $fields[0]) {
$retval = \@fields;
last;
}
}
close FH;
return $retval;
}
There are a number of problems with this kind of design (including, but not limited to:)
- Enforcement of primary key integrity (there is none)
- Locking of the file during updates
- Scalability of the file
IMHO, this would be considered a one off solution, not really production quality. One step toward production quality would be to stuff it all into a database!
Putting this kind of information in a database is quite useful as the data can but cut up in any particular way, normalised (part number, description, who ordered the part), the data can be backed up using standard database backup methods, it can be rolledback (if your DB engine supports it) etc etc etc.