use Fcntl; sub add { my($file, $domain_string)=@_; my $tempfile="Clever_but_constant_name_here"; my $in, $out; sysopen $out, "$tempfile", O_WRONLY|O_CREAT|O_EXCL or die "Temporary file $tempfile exists"; open $in, "<$file" or die $!; local $/ = "\;\n\n"; my @array = <$in>; close $in; push @array, $domain_string; @array = sort @array; print $out @array; close $out; rename $tempfile, $file or die $!; }