Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Step 1: Open any text editor (such as Notepad) and create a file named empdata.t

ID: 3833004 • Letter: S

Question

Step 1: Open any text editor (such as Notepad) and create a file named empdata.txt. Have the following information (Full Name, Age, Gender, Marital Status) about a person inside the file ONE UNDER THE OTHER [DO NOT HAVE TITLES IN THE FILE, ONLY THE DATA] Save the file.

Here is an example, how the data in the file look like.

Paul Hendrickson

35

Male

Single

Step 2: Write a Perl script to:

a. Open the above file

b. Read the lines from the file and print them on the screen with a title added. Close the file.

So when you run the script you should display the data as follows.

Full Name: Paul Hendrickson

Age: 35

Gender: Male

Marital Status: Single

Note: Nicely align the output as shown above.

Step 3: Add more code to change 2 of the items (Age and Marital Status). Script then continues and asks the question:

Do you want to change the age (Y/y or N/n): Y

User enters ‘Y’.

Script then asks the question: What is the new age? 37

User enters 37.

Then the script asks Do you want to change the Marital Status (Y/y or N/n): Y

User enters ‘Y’.

Script then asks the question: What is the new marital status? Married

User Enters Married.

Then the script opens the same file empdata.txt. Rewrites the updated information back to the file. Note: If The user enters ‘N’ in any of the two questions above, no change happens to the existing data item pertaining to each question.

Step 4: Open the same file again in a text editor such as Notepad. You should now see the result like this:

Paul Hendrickson

37

Male

Married

Here is a complete run:

Full Name: Paul Hendrickson

Age: 35

Gender: Male

Marital Status: Single

Do you want to change the age (Y/y or N/n): Y

What is the new age? 37

Do you want to change the Marital Status (Y/y or N/n): Y

What is the new marital status? Married

**Opened the file in Notepad. The result is:

Paul Hendrickson

37

Male

Married

Explanation / Answer

For the first part i.e. to read from notepad the code will go as follows:
use strict;
use warnings;
use Path::Class;
use autodie; # die if problem reading or writing a file
my $dir = dir("/tmp"); # /tmp
my $file = $dir->file("empdata.txt"); # /tmp/empdata.txt
# Get a file_handle (IO::File object) you can write to
my $file_handle = $file->openw();
my @list = ('Full Name:', 'Age:', 'Gender:', 'Marital Status:');
foreach my $line ( @list ) {
# Add the line to the file
$file_handle->print($line . " ");
}

It will print with the title added.


Then for adding the extra part code will go as follows:

use strict;
use warnings;

print "Do you want to change the age (Y/y or N/n):";
my $input = <STDIN>;
chomp $input;
print " What is the new age?”;
chomp $input1;
print " Do you want to change the Marital Status (Y/y or N/n):";
chomp $input2;
print " What is the new marital status?”;
chomp $input3;

And then we have to write these values into notepad.

use strict;
use warnings;
use Path::Class;
use autodie; # die if problem reading or writing a file
my $dir = dir("/tmp"); # /tmp
my $file = $dir->file("empdata.txt"); # /tmp/empdata.txt
# Get a file_handle (IO::File object) you can write to
my $file_handle = $file->openw();
my @list = ('$input1', ‘$input3’);
foreach my $line ( @list ) {
# Add the line to the file
$file_handle->print($line . " ");
}

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote