Section A.1. Answers to Chapter 2 Exercises

A.1. Answers to Chapter 2 Exercises

  1. Here's one way to do it:

         #!/usr/bin/perl -w     $pi = 3.141592654;     $circ = 2 * $pi * 12.5;     print "The circumference of a circle of radius 12.5 is $circ.\n"; 

    We started this program with a typical #! line; your path to Perl may vary. We also turned on warnings.

    The first real line of code sets the value of $pi to our value of p. There are several reasons a good programmer will prefer to use a constant[*] value like this: it takes time to type 3.141592654 into your program if you ever need it more than once. It may be a mathematical bug if you accidentally used 3.141592654 in one place and 3.14159 in another. There's only one line to check to ensure you didn't accidentally type 3.141952654 and send your space probe to the wrong planet. It's easier to type $pi than p, especially if you don't have Unicode. Maintaining the program also will be easier in case the value of p changes.[]. Next, we calculate the circumference, storing it into $circ, and we print it out in a message. The message ends with a newline character because every line of a good programs output should end with a newline. Without it, you might end up with output looking something like this, depending on your shell's prompt:

    [*] If you'd prefer a more formal sort of constants, the constant pragma may be what you're looking for.

    [] It nearly did change more than a century ago by a legislative act in the state of Indiana. See House Bill No. 246, Indiana State Legislature, 1897,

         The circumference of a circle of radius 12.5 is     78.53981635.bash-2.01$[  ] 

    The box represents the input cursor, blinking at the end of the line, and that's the shell's prompt at the end of the message.[*] Since the circumference isn't 78.53981635.bash-2.01$, this should probably be construed as a bug. So, use \n at the end of each line of output.

    [*] We asked O'Reilly to spend the extra money to print the input cursor with blinking ink, but they wouldn't do it for us.

    Here's one way to do it:

         #!/usr/bin/perl -w     $pi = 3.141592654;     print "What is the radius? ";     chomp($radius = <STDIN>);     $circ = 2 * $pi * $radius;     print "The circumference of a circle of radius $radius is $circ.\n"; 

    This is like the last one, except now we ask the user for the radius, and then we use $radius in every place where we previously used the hardcoded value 12.5. If we had written the first program with more foresight, we would have had a variable named $radius in that one as well. Note that we chomped the line of input. If we hadn't, the mathematical formula would still have worked because a string like "12.5\n" is converted to the number 12.5 without any problem. But when we print out the message, it would look like this:

         The circumference of a circle of radius 12.5      is 78.53981635. 

    The newline character is still in $radius, even though we've used that variable as a number. Since we had a space between $radius and the word "is" in the print statement, there's a space at the beginning of the second line of output. The moral of the story is: chomp your input unless you have a reason not to do it.

  2. Here's one way to do it:

         #!/usr/bin/perl -w     $pi = 3.141592654;     print "What is the radius? ";     chomp($radius = <STDIN>);     $circ = 2 * $pi * $radius;     if ($radius < 0) {       $circ = 0;     }     print "The circumference of a circle of radius $radius is $circ.\n"; 

    Here we added the check for a bogus radius. Even if the given radius was impossible, the returned circumference will at least be nonnegative. You could have changed the given radius to be zero and calculated the circumference. There's more than one way to do it. In fact, that's the Perl motto: There Is More Than One Way To Do It, and that's why each exercise answer starts with "Here's one way to do it."

    Here's one way to do it:

         print "Enter first number: ";     chomp($one = <STDIN>);     print "Enter second number: ";     chomp($two = <STDIN>);     $result = $one * $two;     print "The result is $result.\n"; 

    Notice that we've left off the #! line for this answer. From here on, we'll assume that you know it's there, so you don't need to read it each time.

    Perhaps those are poor choices for variable names. In a large program, a maintenance programmer might think $two should have the value of 2. In this short program, it probably doesn't matter; in a large one, we could have called them something more descriptive, with names like $first_response.

    In this program, it wouldn't make any difference if we forgot to chomp the two variables $one and $two since we never use them as strings once they've been set. But if next week our maintenance programmer edits the program to print a message like The result of multiplying one by $two is $result.\n, those pesky newlines will come back to haunt us. Once again, chomp unless you have a reason not to chomp[*]like in the next exercise.

    [*] Chomping is like chewing: not always needed, but most of the time it doesn't hurt.

  3. Here's one way to do it:

         print "Enter a string: ";     $str = <STDIN>;     print "Enter a number of times: ";     chomp($num = <STDIN>);     $result = $str x $num;     print "The result is:\n$result"; 

    This program is almost the same as the last one. We're "multiplying" a string by a number of times. So, we've kept the structure of the previous exercise. In this case, we didn't want to chomp the first input itemthe stringbecause the exercise asked for the strings to appear on separate lines. So, if the user entered fred and a newline for the string and 3 for the number, we'd get a newline after each fred as we wanted.

    In the print statement at the end, we put the newline before $result because we wanted to have the first fred printed on a line of its own. That is, we didn't want output like this, with only two of the three freds aligned in a column:

         The result is: fred     fred     fred 

    At the same time, we didn't need to put another newline at the end of the print output because $result should already end with a newline.

    In most cases, Perl won't mind where you put spaces in your program; you can put in spaces or leave them out. But it's important not to spell the wrong thing accidentally. If the x runs up against the preceding variable name $str, Perl will see $strx, which won't work.

Learning Perl
Learning Perl, 5th Edition
ISBN: 0596520107
EAN: 2147483647
Year: 2003
Pages: 232

Similar book on Amazon © 2008-2017.
If you may any questions please contact us: