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

Write a Perl script that works as below. (a) It takes the name of a data file as

ID: 3824621 • Letter: W

Question

Write a Perl script that works as below.

(a) It takes the name of a data file as parameter. This file is the output of the command

ls –l | tail –n +2 >lsfile

Here is a sample content.

-rw-r--r-- 1 cfs264sp170225 cfs264sp1702   21 2017-04-16 d1

drwxr-xr-x 2 cfs264sp170225 cfs264sp1702 4096 2017-04-16 dir1

-rw-r--r-- 1 cfs264sp170225 cfs264sp1702 181 2017-04-16 lsfile

-rwxr-xr-x 1 cfs264sp170225 cfs264sp1702 179 2017-04-16 p1

-rwxr-xr-x 1 cfs264sp170225 cfs264sp1702   14 2017-04-16 p2

-r--r-xr-x 1 cfs264sp170225 cfs264sp1702 828 2017-04-16 signindata

-rw-r--r-- 1 cfs264sp170225 cfs264sp1702 210 2017-04-16 x

(b) The script examines each line of the file (given as parameter) and uses the information so read to determine the total space occupied by the entries listed in the file. The program prints nothing else.

Here is a possible output.

                         Total space = 5593 bytes

The program must be exclusively in Perl and must be executable as shown below from the command prompt assuming access permissions are set.

$ ./p2    <file-name>

For example,

$ ./p2    mylisting

Explanation / Answer

run as
$ ./p2 enter filename

#perl code below

#!/usr/bin/env perl

use v5.10.0;
use warnings;
use strict;

open(FILE, $ARGV[0]) or die("Could not open the file $ARGV[0]");

my $total_size = 0;
my $line = "";

while ($line = <FILE>){
$total_size += (split(" ", $line))[4];
}

print "Total space = $total_size bytes ";



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