Hi,
I am a beginner to perl. I have written a simple program in Perl
# This Program will copy the contents of a file named file.txt to another file
# newfile.txt. If newfile.txt is already existing, the user is given an option
# to overwrite the file and chose a new file name to write the output.
In this program, when I initially dont have a file named newfile.txt, it goes fine. If
it finds a newfile.txt, then it prompts with a Warning message whether to overwrite or
use another file name text.txt. From here if I give a command to overwrite, it works
fine. If I give a command NO, it still overwrites the newfile.txt. Even if I give some
invalid command other than Y or N it still overwrites that file. I am unable to fix
this. Kindly help me in this regard. I have pasted the entire code below.
#!/usr/local/bin/perl
# This Program will copy the contents of a file named file.txt to another file
# newfile.txt. If newfile.txt is already existing, the user is given an option
# to overwrite the file and chose a new file name to write the output.
# Open the first file file.txt
open (FILE, "file.txt") || die ("ERROR !! file.txt does not exist\n");
# Check for newfile.txt
if (-e "newfile.txt")
{
print "WARNING !! FILE newfile.txt already exists\n";
print "Do you want to destroy all the contents and overwrite the file\n";
print "Type Y for Yes or N or NO\n";
$test = <STDIN>;
$test = chomp ($test);
# Check if the command is either Yes or No. If not, be in the loop till
# Yes or No command is given.
until (($test ne 'Y') && ($test ne 'N') &&
($test ne 'y') && ($test ne 'n'))
{
print "Type Y for Yes or N for NO\n";
$test = <STDIN>;
$test = chop ($test);
}
# If command is NO, then ask if the contents will be copied in another file
# name text.txt.
if ($test eq 'N' || $test eq 'n')
{
print "Do you want to write to another file named text.txt\n";
print "Type Y for Yes or N to quit\n";
$var = <STDIN>;
$var = chop ($var);
# Check if the command is either Yes or No. If not, be in the loop till
# Yes or No command is given.
until ($var ne 'Y' && $var ne 'N' && $var ne 'y' && $var ne 'n')
{
print "Type Y for Yes or N to quit\n";
$var = <STDIN>;
$var = chomp ($var);
}
# If command is YES, copy the contents to a new file called text.txt.
# If command is NO quit program without copying.
if ($var eq "Y" || $var eq "y")
{
open (FILE1, ">text.txt");
$line1 = <FILE>;
while ($line1 ne "")
{
print FILE1 $line1;
$line1 = <FILE>;
}
}
else
{
die "Program terminated without copying the file\n";
}
}
# Destroy the contents of newfile.txt and copy the contents in this file.
else
{
open (FILE1, ">newfile.txt");
$line1 = <FILE>;
while ($line1 ne "")
{
print FILE1 $line1;
$line1 = <FILE>;
}
}
}
# If newfile.txt does not exist, create a file in that name and copy the
# contents to this file.
else
{
open (FILE1, ">newfile.txt");
$line1 = <FILE>;
while ($line1 ne "")
{
print FILE1 $line1;
$line1 = <FILE>;
}
}
Anbudan,
Anand~
-----------------------------------------------------------------------------------
If you can't beat your computer at Chess, try kickboxing.
DISCLAIMER:
Information contained and transmitted by this E-MAIL is proprietary to Hexaware
Technologies and is intended for use only by the individual or entity to which it is
addressed and may contain information that is privileged, confidential or exempt from
disclosure under applicable law. If this is a forwarded message, the content of this
E-MAIL may not have been sent with the authority of the Company. If you are not the
intended recipient, an agent of the intended recipient or a person responsible for
delivering the information to the named recipient, you are notified that any use,
distribution, transmission, printing, copying or dissemination of this information in
any way or in any manner is strictly prohibited. If you have received this
communication in error, please delete this mail & notify us immediately at
[EMAIL PROTECTED]