197k views
2 votes
Write a perl program that reads from a file containing a list of names and then displays that list to a user. The program should then ask the user if they want to sign up on the list. If they say yes, then the program should get the name of the user and add it to the file containing the list of names. If the user does not want to sign up then the program should exit.

1 Answer

2 votes

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.

User GrZeCh
by
4.1k points