rename outfile before writing

Hi - I wrote the below program and I had one issue
all I am doing in the below program is I get the inputfile and get the file modifed date and substring in the inputfile and using it to generate outfilename,
I want to change the outfile name before I copy the content to the outfile.
so I updated outFileName variable but it did not work, it is still copying the content from testfile.txt to testfile2.txt
but not to the updated file name

use 5.006;
use strict;
use warnings;
use Pod::Usage;
use Getopt::Long;
use Cwd;
use DateTime;
use Time::Piece;
use File::Basename;

my $helpme = 0;
my $man = 0;
my $inputFile =  'C:\\temp\\testfile.txt';
my $outFileName = 'C:\\temp\\testfile2.txt';
my $filesubstr = '';
my $fileDate;
my $fileName;
my $fileContent;


open(INFILE, "<", $inputFile) || die "error";
open(OUTFILE, '>', $outFileName) or die "error";

while (<INFILE>) {
    chomp; 
    my @lines = split /\~/, $_;
    $fileName = basename($inputFile, '.txt');  
    #print " filename: $fileName \n";    
    $filesubstr = substr($fileName, -5);
    $fileContent = $_;    
    $fileDate = POSIX::strftime("%Y:%m:%d %H:%M%S",localtime( ( stat $inputFile )[9]));  

}
$outFileName = "NewFile_".$fileDate."_".$filesubstr.".txt";

print OUTFILE $fileContent;


close INFILE;
close OUTFILE;

Open in new window

shragiAsked:
Who is Participating?
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:
$outFileName = "NewFile_".$fileDate."_".$filesubstr.".txt";
open(OUTFILE, '>', $outFileName) or die "error";
print OUTFILE $fileContent;
0

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
shragiAuthor Commented:
Hi Ozo - I just figured it out and about to delete the question, but thanks for the quick response.
I wrote the same solution and it worked.

Thanks,
0
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.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.