Reputation: 83
I'm trying to create a text file that will store user input such as their initials. This code will prompt the user for the information and create a text file but the text file remains empty. How can I get the information to store itself inside the text file? I've tried using the variant $initials but it's not storing the information. I can't seem to figure out what I'm doing wrong.
use strict;
use warnings;
print "Enter users initials: ";
my $initials = <STDIN>; # prompts user for input
open( MYFILE, '>>initials.txt' ); #creates file initials.txt
while (<MYFILE>) {
chomp;
print MYFILE $initials;
}
close(MYFILE);
Upvotes: 0
Views: 1113
Reputation: 50637
This will append text from $initials to end of the file.
use strict;
use warnings;
print "Enter users initials: ";
my $initials = <STDIN>; # prompts user for input
open my $fh, '>>', 'initials.txt' or die $!; #creates file initials.txt
print $fh $initials;
close $fh;
Upvotes: 1
Reputation: 62037
There is no need for the while loop or chomp
:
use strict;
use warnings;
print "Enter users initials: ";
my $initials = <STDIN>; # prompts user for input
open( MYFILE, '>>initials.txt' ); #creates file initials.txt
print MYFILE $initials;
close(MYFILE);
Upvotes: 1