Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
Solved

# Per sub routine

Posted on 2011-09-15
Medium Priority
257 Views
I am working on a perl script that would print a value based on a menu option. Below is the menu option.
Select one of:
1. Print the Current Value
2. Add a number to current value
3. Subtract a number from the current value
4. Multiply the current value by a number
5. Divide the current value by a number
6. Raise the current value to a power
7. clear the current value to 0
8. Exit";

the problem i am having is with the loop. I want it loop until the user enter 8 to exit.
``````#!/usr/bin/perl
#use strict;
use warnings;

# Subroutine prototypes
sub do_exit();
sub current_value();
sub subtract_number();
sub multiply_number();
sub divide_number();
sub raise_number();
sub clear_number();
\$value=0;

print "
Select one of:
1. Print the Current Value
2. Add a number to current value
3. Subtract a number from the current value
4. Multiply the current value by a number
5. Divide the current value by a number
6. Raise the current value to a power
7. clear the current value to 0
8. Exit";
print "\n";

# Get the user's input
# Define the actions to take
{
current_value();
}
{
}
{
subtract_number();
}
{
multiply_number();
}
{
divide_number();
}
{
raise_number();
}
{
clear_number();
}
do_exit();
}

else {
print "I didn't understand the command.\n";
do_exit();
}

exit 0;

}

#-------------------------------------------------------------
sub current_value() {

print \$value;
print "\n";
return;
}

\$number = <STDIN>;
\$value = (\$number +\$value);
print \$value,"\n";
return;
}

sub subtract_number() {
print "enter number to subtract,\n";
\$number = <STDIN>;
\$value = (\$value-\$number);
print \$value,"\n";
return;
}

sub multiply_number() {
print "enter number to multiply,\n";
\$number = <STDIN>;
\$value = (\$value*\$number);
print \$value,"\n";
return;
}

sub divide_number() {
print "enter number to divide,\n";
\$number = <STDIN>;
if (\$number ==0){
print "please choose number greater than 0, \n";
}
else {
\$value = (\$value/\$number);
print \$value,"\n";
}
return;
}
sub raise_number() {
print "enter number to multiply,\n";
\$number = <STDIN>;
\$value = (\$value**\$number);
print \$value,"\n";
return;
}
sub clear_number() {
\$value = (0);
print \$value,"\n";
return;
}
sub do_exit() {
print "Exiting...\n";
return;
}
``````
0
Question by:SheldonC
[X]
###### Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

• Help others & share knowledge
• Earn cash & points
• 2
• 2
• 2

LVL 10

Accepted Solution

jeromee earned 2000 total points
ID: 36545112
Good luck!
0

Author Comment

ID: 36545183
I am not quite sure I quite understand
0

LVL 10

Expert Comment

ID: 36545277
Replace the code at line138 from:
``````sub do_exit() {
print "Exiting...\n";
return;
}
``````
to
``````sub do_exit() {
print "Exiting...\n";
exit(1);
}
``````
0

Author Comment

ID: 36545292
sorry,thanks I got it.
0

LVL 28

Expert Comment

ID: 36545356
1) re-enable strict and declare all of your vars.  The combination of the strict and warnings pragmas will catch lots of problems that might otherwise be missed and could be difficult to track down.

2) get rid of the empty prototype definitions.

3) create a dispatch table.
http://en.wikipedia.org/wiki/Dispatch_table
http://stackoverflow.com/questions/1281456/how-do-i-implement-dispatch-tables-in-perl

4) use an infinate while loop which will exit when the user selects option 8 (that's when you call the do_exit() sub via the dispatch table.
0

LVL 28

Expert Comment

ID: 36545554
Maybe I should give a brief reason as to why I made those suggestions.

This is obviously a class homework assignment and as it's written you will probably at best get a 'C' grade.  The suggestions I made introduce better quality coding standards and if done correctly will be much easier to read and maintain and you will receive a much higher grade.

There are a couple improvements that I left out and can point them out if you wish.
0

## Featured Post

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Email validation in proper way is  very important validation required in any web pages. This code is self explainable except that Regular Expression which I used for pattern matching. I originally published as a thread on my website : http://www…
A year or so back I was asked to have a play with MongoDB; within half an hour I had downloaded (http://www.mongodb.org/downloads),  installed and started the daemon, and had a console window open. After an hour or two of playing at the command …
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
Six Sigma Control Plans
###### Suggested Courses
Course of the Month4 days, 10 hours left to enroll