in reply to Changing data in a directory
You're treating $db as both a directory and a file. I suspect that
opendir(DATA, "$db") or die "File does not open: $!";
should be
open(DATA, "$db") or die "File does not open: $!";
It also helps to report the filename in the error message.
open(DATA, $db) or die "$db: $!";