Perl: some explanation of yesterday’s command-line cube root program
The tutor offers a few points of clarification.
In yesterday’s post I showed a Perl program, called from the command line with the input value, to find the cube root of a number. Here are some points to help a casual reader understand it:
$ARGV[0] refers to the first command-line argument. Let’s imagine the program is stored as cuberoot.txt. If the user wants the cube root of -64, the program will be called from the command line as follows:
perl cuberoot.txt -64
The if statement checks for negativity; my version of Perl isn’t happy to exponentiate a negative number by 1/3. I just make $input positive, take that cube root, then multiply the answer by negative 1. Later I change $input back to negative so it’s correct in the answer statement printed to the screen.
printout is a subroutine listed at the bottom of the program. It’s called by &printout. To my knowledge, a subroutine is called using an ampersand in front of its name.
HTH:)
Source:
McGrath, Mike. Perl in easy steps. Southam: Computer Step, 2004.
Jack of Oracle Tutoring by Jack and Diane, Campbell River, BC.
Leave a Reply
You must be logged in to post a comment.