# if you want to delete, make the line with null value; $data[$line] = ""; #### 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}; } }