even though i used the chomp on $_.
In your case, that $_
is an in-memory copy of current input line, so chomp
will not change the corresponding line of input file.
One way to change file in place is using Tie::File
, which will tie a regular file to an array, so you can modify that file by changing that array.
Here is an example:
#!/usr/bin/perl
use strict;
use warnings;
use Tie::File;
my $file = 'new.csv';
tie my @line, 'Tie::File', "$file" or die "Cannot tie $file: $!";
$line[0] .= ', new_data';
untie @line;