How would I add carage returns to a text file file in UNIX Part 2

I got this example yesterday which works on 2 different files. Does anyone know how to manipulate just one file?
I need the input file and output file to be the same.
e.g. open(IN,"<test.txt");
      open(OUT,">test.txt");
This will create a blank file. But I need to open test.txt and insert the carage returns on test.txt. The code below make a duplicate of test.txt to out.txt.

open(IN,"<test.txt");
open(OUT,">out.txt");
while ( <IN> )
{
    chomp;  # remove line terminator
    print(OUT "$_\r\n"); # add window style line terminator
}
close(IN);
close(OUT);
ckasparAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

ozoCommented:
{
local @ARGV=("test.txt",);
local $^I="";
local $\="\r\n";
chomp,print while <>;
}

ckasparAuthor Commented:
I tried that and got this error -> Can't open $file: No such file or directory?
Any ideas?

#!/usr/bin/perl

my $file = "/home/scr/file";

{
  local @ARGV=qw($file);
  local $^I="";
  local $\="\r\n";
  chomp,print while <>;
}
ozoCommented:
#or
use Tie::File;
tie @array, 'Tie::File', "test.txt" or die "test.txt $!";
$_ .= "\r" for @array;
untie @array;
Bootstrap 4: Exploring New Features

Learn how to use and navigate the new features included in Bootstrap 4, the most popular HTML, CSS, and JavaScript framework for developing responsive, mobile-first websites.

ozoCommented:
#!/usr/bin/perl

my $file = "/home/scr/file";

{
  local @ARGV=($file);
  local $^I="";
  local $\="\r\n";
  chomp,print while <>;
}

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
ozoCommented:
qw($file) is an array contaning the literal string '$file'

       qw/STRING/
               Evaluates to a list of the words extracted out of STRING, using
               embedded whitespace as the word delimiters.  It can be under-
               stood as being roughly equivalent to:

                   split(' ', q/STRING/);

               the differences being that it generates a real list at compile
               time, and in scalar context it returns the last element in the
               list.  So this expression:

                   qw(foo bar baz)

               is semantically equivalent to the list:

                   'foo', 'bar', 'baz'

               Some frequently seen examples:

                   use POSIX qw( setlocale localeconv )
                   @EXPORT = qw( foo bar baz );

               A common mistake is to try to separate the words with comma or
               to put comments into a multi-line "qw"-string.  For this rea-
               son, the "use warnings" pragma and the -w switch (that is, the
               $^W variable) produces warnings if the STRING contains the ","
               or the "#" character.
Kim RyanIT ConsultantCommented:
$in_file = 'data.txt';
$out_file = 'tmp.txt';

open(IN,"<$in_file");
open(OUT,">$out_file");
while ( <IN> )
{
    chomp;  # remove line terminator
    print(OUT "$_\r\n"); # add window style line terminator
}
close(IN);
close(OUT);
rename($out_file,$in_file); # replace original file with modified version
Kim RyanIT ConsultantCommented:

$file = 'test.txt';
open(IN,"<$file");
@lines = <IN>; # get all lines in file
close(IN);
open(OUT,">$file");
foreach ( @lines )
{
    chomp;  # remove line terminator
    print(OUT "$_\r\n"); # add window style line terminator
}
close(OUT);
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Perl

From novice to tech pro — start learning today.