Share
Explore BrainMass

Using $#ARGV in PERL, write a program that have two subroutines add() and multiply() that will add or multiply its arguments and print the result to screen.

Write a program in PERL that have two subroutines add() and multiply() that will add or multiply its arguments and print the result to screen.

The values must returned from the add and multiply functions, back to the main program before the value is printed. The body of the code might look something like the following:

if ($ARGV[0] =~ /add/) {
$rtn = add($ARGV[1], ...]);
print "The sum is: $rtn";
}
elsif ($ARGV[0] =~ /multiply/) {
$rtn = multiply($ARGV[1], ...]);
print "The product is: $rtn";
}

sub add {
...
}

sub multiply {
...
}

(Hint: $#ARGV can be used to determine how many values were specified on the command line. The length of an array is also retrieved by writing something like $len = @ARGV.)

Solution Preview

The add() and multiply() subroutines take the two parameters and
perform the correct operation. The main code checks the number of
arguments to make sure it is correct and then uses the first one to
decide what operation to ...

Solution Summary

The solution uses $#ARGV in PERL to write a program that has two subroutines add() and multiple() that will add or multiple its arguments.

$2.19