That code could be prettier, but it has a serious bug in it:
# if you want to delete, make the line with null value;
$data[$line] = "";
This doesn't actually delete the line; it replaces it with an empty line. Oh, and there's another bug -- why join lines on newlines when you haven't chomped away existing newlines? Here's much better code:
my $filename = 'yourfilename.txt';
my $newfile = 'new_filename.txt';
filter_file( $filename, $newfile );
# this line may be unnecessary depending on your filesystem
unlink $filename;
rename( $filename, $newfile );
sub filter_file
{
my ($from, $to) = @_;
open my $fh, $from or die "Can't read '$from': $!\n";
open my $out_fh, '>', $to or die "Can't write '$to': $!\n";
while (<$fh>)
{
next if should_delete_line($_);
print {$out_fh};
}
}
|