In: Computer Science
Write a program that creates a file called "data.dat" in the current directory. Prompt the user for five numbers, and write them, one at a time, on both the screen and into the file. Close the file, then open it again for reading only, and display the contents on the screen. Handle error conditions that may occur.
Please Need this to be done in PERL. Thanks
PERL code given below :-
open(DATA, ">data.dat") or die "Couldn't open file data.dat, $!"; # create file data.dat and write
for( $a = 0; $a < 5; $a = $a + 1 ) {
print "Enter a number:\n";
my $num = <STDIN>;
chomp $num; # get user input
number
push @arr, $num; # store number in array
}
for( $a = 0; $a < 5; $a = $a + 1 ) {
print "\n",
$arr[$a]; # print number , one
at a time
print DATA $arr[$a],"\n"; # write
number in data.dat file
}
close DATA or "couldn't close"; # close the
data.dat file
open(DATA, "<data.dat") or die "Couldn't open file data.dat,
$!"; # open data.dat file for only Read
print "\n Read from file :\n";
while(<DATA>) {
print
"$_"; #
print all the number from file
}
close DATA or "couldn't close"; # close the file
Sreeenshot of code and output :-