Question: Perl Assignment Objectives To get practice in using the Perl language by using common programming concepts: Program arguments, arrays and subroutines This script will translate
Perl Assignment
- Objectives
- To get practice in using the Perl language by using common programming concepts:
- Program arguments, arrays and subroutines
- This script will translate number scores entered as program arguments into their letter grade equivalents using a subroutine.
- To get practice in using the Perl language by using common programming concepts:
- Perl Program 02
- Create a perl script named LastnameFirstname02.pl.
- Include strict and warnings at the top of your script.
- The script expects at least 1 program argument.
- The arguments will be numbers.
- For now, you don't have to validate that it's in fact numbers, we will deal with that later.
- Terminate the script with an appropriate error and usage message if this requirement is not met.
- See the error and usage messages in the Example Outputs on the bottom. You may adapt the error and usage messages for your own scripts.
- The arguments will be numbers.
- Define a subroutine called graderator that uses arguments and returns a value.
- This subroutine builds a string containing the translation of all the numbers passed to it into letter grades. The letter grades are all on a single line separated by one space in the same way the corresponding numbers were entered as program arguments.
- Declare and initialize a scalar variable named $output that will be used to store all the letter grade translations.
- Initialize the variable to the empty string.
- Example: my $output = "";
- Loop through each element in the default array, determine the letter grade for each value, and concatenate the appropriate letter on to $output.
- If the number is greater than or equal to 90, concatenate "A".
- If the number is greater than or equal to 80, concatenate "B".
- If the number is greater than or equal to 70, concatenate "C".
- If the number is greater than or equal to 60, concatenate "D".
- If the number is less than 60, concatenate "F".
- Note: Do not print anything in this subroutine, this subroutine returns a value instead of printing.
- You may print to debug your script, but it should not be a part of your final submission.
- After the loop, return $output.
- You are NOT allowed to use or make any reference to ARGV in the subroutine. The purpose of calling a subroutine with arguments is to use the default array.
- Above the definition of the graderator subroutine, invoke/call it with the @ARGV as an argument and store the result in a scalar variable.
- Print the scalar variable to display the results of the graderator subroutine.
- Ensure that your code is sufficiently styled and documented.
- Styled meaning code is indented when applicable, variables are named properly, etc.
- Documentation includes a program description at the top and in-line comments.
- See posted examples in the Lecture Material page.
- It is okay when your program crashes/produces warnings if the user enter letters or sybols instead of a number, we will deal with those later.
- In the Hints section on the bottom, there is a starter outline you may use for your script.
- Example Outputs The following outputs are all from the same script but run with different program arguments.
> perl LastnameFirstname02.pl Error: LastnameFirstname02.pl expects at least 1 program argument. Usage: perl LastnameFirstname02.pl num1 num2 num3 ... > perl LastnameFirstname02.pl 1 F > perl LastnameFirstname02.pl 89.9 76.3 50 63 B C F D > perl MeyerEdward02.pl 90 80 1000 60 -16 A B A D F > perl LastnameFirstname02.pl baby shark du du dudu Argument "baby" isn't numeric in numeric ge (>=) at MeyerEdward02.pl line 16. Argument "shark" isn't numeric in numeric ge (>=) at MeyerEdward02.pl line 16. Argument "du" isn't numeric in numeric ge (>=) at MeyerEdward02.pl line 16. Argument "du" isn't numeric in numeric ge (>=) at MeyerEdward02.pl line 16. Argument "duu" isn't numeric in numeric ge (>=) at MeyerEdward02.pl line 16. F F F F F
- Hints
- The basic outline of the script looks like this:
use strict; use warnings; # Program Description # # Author: # Error checking for command line arguments goes here. # Call the subroutine with the program arguments and store the result in a variable my $gradeOutput = &graderator(@ARGV); # Output the result. print $gradeOutput; # Define the graderator subroutine here sub graderator { } - If you use the above outline, replace the comments with your own.
- There is only 1 print instruction in the entire script.
- The subroutine returns a string, therefore you should not have any print instructions inside the subroutine.
- To build a string, concatenate the variable onto itself - this is similar to incrementing a variable.
- As an example: my $output = ""; $output = $output . "pew"; # pew $output = $output . "pew"; # pewpew
- There is the shortcut .= to concatenate a variable onto itself.
- As an example: my $output = ""; $output .= "pew"; # pew $output .= "pew"; # pewpew
- Be sure to add a space to separate each letter.
- The basic outline of the script looks like this:
Step by Step Solution
There are 3 Steps involved in it
1 Expert Approved Answer
Step: 1 Unlock
Question Has Been Solved by an Expert!
Get step-by-step solutions from verified subject matter experts
Step: 2 Unlock
Step: 3 Unlock
