Need help solving this assignment: the script should be in Perl please. Write Pe
ID: 3888388 • Letter: N
Question
Need help solving this assignment: the script should be in Perl please.
Write Perl script to develop a small console program driven by a text menu
· It displays a simple menu with 4 items indicated by number 1-4.
· Users will enter number 1 to 4 to run some commands.
· Item #1 to #3 will run some shell commands (it’s your choice what to run). When the command is finished, display the menu again.
· If 4 is entered, then quit the program.
Example:
Welcome to menu:
1. ls
2. ls -i
3. ls -a
4. Exit
Please enter a choice 1-4: 1
Explanation / Answer
Hi,
Please find the script below. Please let me know in case of any issue.
#!/usr/bin/perl
use strict;
use warnings;
# Subroutine prototypes defination for each option
sub do_exit();
sub system_time();
sub list_with_file_size();
sub list();
# Define the actions to take based on user input
#if input is 1, then list option will be selected and list function will be executed
my %action_to_take = (
'1' => &list,
'2' => &list_with_file_size,
'3' => &system_time,
'4' => &do_exit,
);
# Print the menu selection
print <<"EOT";
Select one of:
1. List directory contents with long format - show permissions
2. List directory contents with long format with file size
3. Get System time
4. Exit
EOT
# Get the user's input
my $menu_item = <>;
chomp($menu_item);
# Take action based on the user's choice
if (defined $action_to_take{$menu_item}) {
$action_to_take{$menu_item}->();
} else {
print "Invalid option. ";
do_exit();
}
exit 0;
#-------------------------------------------------------------
#sub functions for each menu option
sub list() {
print " Displaying list of directories in long format ";
system('ls -l');
return;
}
sub list_with_file_size() {
print " Displaying list of directories with size ";
system('ls -ls');
return;
}
sub system_time() {
print " Displaying System time ";
system('date');
return;
}
sub do_exit() {
print " Exiting... ";
return;
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.