Answer:
#!/usr/bin/perl
use warnings;
use strict;
my $file = 'File1';
my $name;
my %h = ( ansy => 'Yes', ansn => 'No' );
open(Handle, '>>$file') or die $!;
while(<Handle>){
print $_;
}
print "Do you want to enter your name ? \\";
my $answer = <STDIN>;
chomp $answer;
print "You have entered input as '$answer' \\" ;
my %h = ( ansy => 'Yes', ansn => 'No' );
if( $answer eq $h{ansy} ) {
# if condition is true then print the following
printf "Please enter your name ? \\";
my $answer1 = <STDIN>;
chomp $answer1;
printf "Your have entered '$answer1' and it will be added to list \\";
print Handle '$answer1';
} elsif( $answer eq $h{ansn} ) {
# if condition is true then print the following
printf "You have entered No and we will exit";
exit();
} else {
# if none of the above conditions is true
printf "please enter either Yes or No \\";
}
close(Handle);
Step-by-step explanation:
Above Code will work as per requirements. File1 is the path of your file.