in reply to updating file

If this were for in-memory handling, instead of in a file, I'd think of Tie::IxHash. That implements a "hash" that still acts like a hash in all respects, thus for example, each key must be unique; but in addition, it preserves the order of addition, when you list its keys/values. Here, I only use the keys.
my @color = qw(red blue black); use Tie::IxHash; tie my(%color), Tie::IxHash; # Existing colors: foreach(@color) { $color{$_}++; } # Attempt to add some: foreach(qw(purple blue green red)) { $color{$_}++ or print "Ooh, that's a new one: $_\n"; } print "I've got:\n"; print " * $_\n" foreach keys %color;
Result:
Ooh, that's a new one: purple
Ooh, that's a new one: green
I've got:
 * red
 * blue
 * black
 * purple
 * green
As you can see, it's easy to spot new additions, dupes are prevented, plus the original order of addition is preserved.

If this is for a file, you can just append the new color addition to the end of the file, when you encounter a new one. You then don't even need to use Tie::IxHash — a plain hash will do.