“Now get Cost Data from a Database!”
open COST, "<cost.txt" or die "open cost.txt: $!";
next unless $line =~ m/^\d/;
($prod, $cost) = unpack 'A4 @20 A7', $line;
$costs = $cost_db->prepare('SELECT prod,cost FROM costs');
while ( ($prod, $cost) = $costs->fetchrow_array ) {
Notes:
Here we have changed just 4 lines to fetch the costs from the database.