 # Scalar Data and Operators in Perl

• Print
This chapter is from the book

## Another Example: Stocks

To cement what you've learned today, let's work through another simple example that uses assignment, while loops, if conditionals, pattern matching, input, and both print and printf statements.

This example is a stock performance tracker. All you do is enter the purchase price of your stock and its current price, and it tells you if your investment has lost money, made money, or broken even, and by what percentage:

```% stock.pl
Enter the purchase price: 45
Enter the current price: 48
Your return on investment is 6.7%
% stock.pl
Enter the purchase price: 45
Enter the current price: 40
Your return on investment is -11.1%
% stock.pl
Enter the purchase price: 45
Enter the current price: 45
Your return on investment is 0.0%```

Stock prices must be entered as decimals (not fractions such as 14 5/8), and must be digits. We'll check for both of these things in the script.

Listing 3.2 shows the code for our simple stock tracker. Before reading the following description see if you can go through the code and understand what's going on here.

#### Listing 3.2 The stock.pl Script

```1: #!/usr/local/bin/perl -w
2:
3: \$start = 0;
4: \$end = 0;
5: \$return = 0;
6:
7: while () {
8:   print "Enter the purchase price: ";
9:   chomp (\$start = <STDIN>);
10:
11:   print "Enter the current price: ";
12:   chomp (\$end = <STDIN>);
13:
14:   if (\$start eq '' or \$end eq '' ) {
15:     print "Either the purchase or current price is missing.\n";
16:     next;
17:   }
18:
19:   if (\$start =~ /\D/ or \$end =~ /\D/ ) {
20:     if (\$start =~ /\// or \$end =~ /\//) {
21:       print "Please enter prices as decimal numbers.\n";
22:       next;
23:     } else {
25:       next;
26:     }
27:   }
28:
29:   last;
30: }
31:
32: \$return = (\$end - \$start) / \$start * 100;
33:
34: if (\$start > \$end) {
35:   print "Your investment has lost money.\n";
36: } elsif (\$start < \$end ) {
38: } else {
39:   print "Your investment has broken even.\n";
40: }
41:
42: print "Your return on investment is ";
43: printf("%.1f%%\n", \$return);```

This example has three main sections: an initialization section, a section for getting and verifying input, and a section for calculating and printing the result. I'm going to skip the initialization because you've already seen a bunch of them and you know what they look like by now.

### Getting and Verifying the Input

This while loop, in lines 7 to 30, is for getting the initial input from the user and is the most complex part of this script:

```7: while () {
8:   print "Enter the purchase price: ";
9:   chomp (\$start = <STDIN>);
10:
11:   print "Enter the current price: ";
12:   chomp (\$end = <STDIN>);
13:
14:   if (\$start eq '' or \$end eq '' ) {
15:     print "Either the purchase or current price is missing.\n";
16:     next;
17:   }
18:
19:   if (\$start =~ /\D/ or \$end =~ /\D/ ) {
20:     if (\$start =~ /\// or \$end =~ /\//) {
21:       print "Please enter prices as decimal numbers.\n";
22:       next;
23:     } else {
25:       next;
26:     }
27:   }
28:
29:   last;
30: }```

Initially this will look very similar to the while loop in the stats.pl script: same infinite loop, same if test with pattern matching. But there are some significant differences here.

The most important difference to note is that stats.pl repeats over and over again until the user is done inputting data, whereas this script only needs two pieces of correct data and then it's done. In fact, blank input here is an error, and should be tested for. The other two errors we are checking for are nondigit input, and input made in fractional format (14 5/8, for example). We could lump the latter two together because the slash character / is a nondigit character, but printing a more specific error in that specific case makes for a more user-friendly script.

Our infinite loop, then, will continue looping until we get two acceptable pieces of numeric data. Then we can break out of the loop and move on.

Lines 8 through 12 enable you to look at things line by line. They prompt for the data, and store that data in the scalar variables \$start and \$end. Note that we prompt for both values before testing for validity, rather than doing one at a time. (One at a time would make more sense, usability-wise, but given what you know so far about Perl we would have to duplicate a lot of code, so this way is shorter).

In lines 14 through 17 we test both \$start and \$end to see if they are empty, that is, if the user pressed Return without entering any data. If they did, we call next, which skips to the end of the loop and restarts again from the beginning, prompting again for the data.

In the if statement starting in line 19 things start getting weird. Line 19 is the same test for nondigitness you saw in the stats program; this pattern checks for any input that contains data that isn't a number. This test will trap both data that is completely nonsensical for the script, along with data that might make conceptual sense but that we can't handle—the fractional numbers that I mentioned earlier (14 5/8, 101 15/16, and so on, as stock prices are sometimes listed).

If the test in line 19 is true, the code in line 20 and onward begins executing. That code in-cludes the test in line 20. This test is a character pattern for a single slash—we have to backslash it here because its inside the pattern matching operator. This test, then, is here to trap those fractional numbers. If this test returns true, lines 21 and 22 are executed; we print an error (a hint, actually, and next is called to skip to the end of the loop and restart again).

If the test in line 20 is not true—that is, if we have data that contains something other that a digit, but that doesn't contain a slash—then the code in lines 23 through 26 gets executed. The else clause is part of the if conditional. In an if conditional, if the test is true, the first block of code inside the curly braces is executed. If the test is false, Perl looks for the else clause, and if there is one, then that code gets executed instead. Depending on the state of the test, you'll get either one block or another—never both.

In this case, if we have nonnumeric data that doesn't contain a slash, say, "rasberry", that data will return true for the test in line 19, so the block going from lines 19 to 27 will get executed. The test in line 20, however, will be false—no slash—so Perl will skip lines 20 through 22 and instead execute the else block in lines 23 through 26; print an error, and call next to again run through the same prompting-and-testing loop.

The else part of the if conditional is optional—if there isn't one and the test is false, Perl just goes on executing. So if the user does indeed enter the right data, then the tests in lines 14 and 19 will be false. None of the code in lines 14 through 17 or lines 19 through 27 gets executed if the data is correct. Perl just goes straight down to line 29; last, which breaks us out of the while loop to stop asking for data.

### Calculating and Printing the Result

With correct data in hand, we can go ahead and perform some arithmetic without worrying that what we've actually been given is a string or some weird combination of numbers and strings. All that is behind us now. Line 32 computes the percentage return on investment and stores it in the variable \$return.

`32: \$return = (\$end - \$start) / \$start * 100;`

Note that we need the parentheses around the subtraction here to change the operator precedence. Without the parentheses, the division and multiplication would have been performed first, and then the subtraction, which would not have been correct.

Lines 34 through 40 print our little report on how your investment is doing, and here you can see more ifs and elses. The one in the middle—elsif—is just a shorthand notation for nesting if conditionals inside else statements. You'll learn more about it on Day 6.

Nested ifs, as this structure is called, is a way of cascading different tests and results. You'll see a lot of these sorts of structures. The tests start at the top, and if a test is true, that result is printed, and the nested if is over. Perl skips to the bottom and doesn't do any more tests. If the test is false, another test is tried until one matches or the final else at the end is the default result.

In this case, there are only three possible results: \$start can be less than \$end, start can be greater than \$end, or \$start and \$end can be equal. In the first two tests we check for the first two cases. The last case doesn't need a test because it's the only possible remaining outcome, so it gets caught in the final else in lines 38 through 40.

The final lines, 42 and 43, print out the percentage return on investment we calculated in line 32. Here we use a printf instead of a print, so that we can format the floating-point number to have a single digit of precision.

`43: printf("%.1f%%\n", \$return);`

In this line, %.1f is the formatting code—.1 for the single digit after the decimal point, f for a floating-point number. The two percents after the formatting code (%%) are used to print out an actual percent character in the output. \n, as you know, is a newline, therefore, the result looks something like this:

`Your return on investment is 110.9%`