Anand Ramakrishna wrote:
>
>
> Hi,
Hello,
> 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
You should use warnings and strict when you are developing a perl
program.
use warnings;
use strict;
> # 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");
"does not exist" is not the only reason that a file could not be
opened. You should include the $! variable so that you know exactly why
the open failed.
> # 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);
chomp() returns the _number_ of characters removed.
> # 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);
chop() returns the _character_ removed (in this case the newline
character.)
> }
>
> # 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");
You should _always_ verify that the file opened correctly.
> $line1 = <FILE>;
>
> while ($line1 ne "")
> {
> print FILE1 $line1;
> $line1 = <FILE>;
> }
The correct way to write the while loop is:
while ( my $line1 = <FILE> )
{
print FILE1 $line1;
}
Or even:
while ( <FILE> )
{
print FILE1;
}
Or even simply:
print FILE1 while <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");
You should _always_ verify that the file opened correctly.
> $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");
You should _always_ verify that the file opened correctly.
> $line1 = <FILE>;
>
> while ($line1 ne "")
> {
> print FILE1 $line1;
> $line1 = <FILE>;
> }
> }
This should do what you want:
#!/usr/local/bin/perl
use warnings;
use strict;
use File::Copy;
# 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.
my $oldfile = 'file.txt';
my $newfile = 'newfile.txt';
my $test;
# Check for newfile.txt
if ( -e $newfile ) {
print "WARNING !! FILE $newfile already exists\n";
print "Do you want to destroy all the contents and overwrite the
file\n";
do {
print "Type Y for Yes or N or NO\n";
$test = <STDIN>;
} until $test =~ /^[yYnN]/;
}
if ( $test =~ /^[nN]/ ) {
$newfile = 'text.txt';
print "Do you want to write to another file named $newfile\n";
do {
print "Type Y for Yes or N to quit\n";
$test = <STDIN>;
} until $test =~ /^[yYnN]/;
exit 0 if $test =~ /^[nN]/;
}
copy( $oldfile, $newfile ) or die "Cannot copy $oldfile to $newfile:
$!";
__END__
John
--
use Perl;
program
fulfillment
--
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]