my $newcolor = 'purple';
my $seen = 0;
open my $infile, "<", 'colorfile.txt'
or die "Can't open color file for reading.\n$!";
while ( my $color = <$infile> ) {
$seen++ if $color =~ /$newcolor/;
}
close $infile;
unless ( $seen ) {
open my $outfile, ">>", 'colorfile.txt'
or die "Can't open the color file for append.\n$!";
print $outfile "$newcolor\n";
close $outfile or die "Couldn't close the output file.\n$!";
}
####
my $newcolor = 'purple';
open my $infile, "<", 'colorfile.txt'
or die "Can't open color file for input.\n$!";
my $seen = grep { /$newcolor/ } <$infile>;
close $infile;
open my $outfile, ">>" 'colorfile.txt'
or die "Can't open color file for output.\n$!";
print $outfile "$newcolor\n" unless $seen;
close $outfile
or die "Can't close the output file.\n$!";
####
use strict;
use warnings;
use Tie::File;
my $newcolor = 'purple';
tie my @array, 'Tie::File', 'colorfile.txt'
or die "Can't tie color file.\n$!";
push @array, "$newcolor\n" unless grep { /$newcolor/ } @array;
untie @array;