The tutor notices that, given it’s Aug 14, we need a little progress in our summer project of Perl programming. This rarely comes up in tutoring, but it’s a labour of love….
Back on June 22, I broached the idea of getting started with Perl. After all, I argued, academic pursuits can continue through the summer, the motivation being interest rather than preparing for an exam.
For those of you who actually took up the challenge and got started, you likely followed my blog through the summer. Not every article has been about Perl, but a good few have. The time has come for more.
Today’s article plugs an obvious hole in our knowledge base so far: getting user input. After all, it’s hard to interact with a program if you can’t give it different values to see how the output changes.
Perl has an array called @ARGV that is dedicated to storing inputs from the command line. (See my article on arrays here.) The following program serves as an example:
$ans=$firstnum + $secondnum;
print “\n\nHello, $name.”;
print ” You entered $firstnum and $secondnum.”;
print ” Their sum is $ans\n\n.”
The activity of this code is likely not mysterious. It is given the input array @ARGV with three values in it. $ARGV is expected to contain your name; $ARGV and $ARGV should each contain a number. The program fetches your name from $ARGV, then stores it in the variable $name. Next, the program fetches the numbers from $ARGV and $ARGV, stores them in $firstnumber and $secondnumber, adds the two, then stores the sum in $ans. On the command line, it gives you a friendly greeting by the name you gave, afterwards reminding you the numbers you gave and telling you their sum. Where you see \n, the terminal will start a new line. That’s just done to make space on the screen for the output.
At this point, the serious reader might have four questions in mind:
- How does your name get into $ARGV?
- How does the first number get into $ARGV?
- How does the second number get into $ARGV?
- How does @ARGV get handed to the program as input?
The answer is that you enter those inputs, in order, after the program name. Let’s imagine the program above is called lucy.txt,and that your own name is Edward. Furthermore, you are just now wondering what the sum of 329 and 1982 is. Without a pencil or calculator handy, you’d like to call on lucy.txt for the answer. First, you will go into the terminal (see my articles here, here, and/or here, depending on your operating system). Next, you will place yourself in the directory where lucy.txt resides. Finally, you will call her and give her the three inputs by entering the following line:
perl lucy.txt Edward 329 1982
If all goes well, Lucy will answer with
Hello, Edward. You entered 329 and 1982. Their sum is 2311.
The very first line of lucy.txt, specifically
is called the shebang line. (You can read more about it here.) It is needed in non-Windows environments. While it’s not necessary in Windows, it doesn’t seem to hurt if it’s there.
Good luck with this!
Source: Robert Pepper’s Perl Tutorial
Jack of Oracle Tutoring by Jack and Diane, Campbell River, BC.