Lesson 2, Scalar Data
Here's one way to do it:
#!/usr/bin/perl -w
$pi = 3.141592654;
$circumference = 2 * $pi * 12.5;
print "The circumference of a circle of radius 12.5 is $circumference\n";
As you see 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
. There are several reasons a good programmer
will prefer to use a constant1 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 on to make sure you didn't accidentally type 3.141952654 and send your
space probe to the wrong planet. It's easier to type $pi than
especially if you don't have Unicode. And
it will be easy to maintain the program in case the value of
ever changes.2
Next we calculate the circumference, storing it into $circumference, and we print it out in a nice message. The message ends with a newline character, because every line of a good program's output should end with a newline. Without it, you might end up with output looking something like this, depending upon your shell's prompt:
The circumference of a circle of radius 12.5 is 78.53981635.bash-2.01$
This should probably be construed as a bug. So use \n at the end of
each line of output.
Here's one way to do it:
#!/usr/bin/perl -w
$pi = 3.141592654;
print "What is the radius? ";
chomp($radius = <STDIN>);
$circumference = 2 * $pi * $radius;
print "The circumference of a circle with radius $radius is $circumference.\n";
This is similar to the previous exercise, but here we've asked the person
running the program for a radius value, using a print statement
for a prompt, and then the <STDIN> operator to read a line
from the terminal. We store the radius value in the variable named $radius
and use that variable name wherever we used "12.5" in the previous example.
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.
Notice that 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 message 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 that.
Here's one way to do it:
#!/usr/bin/perl -w
$pi = 3.141592654;
print "What is the radius? ";
chomp($radius = <STDIN>);
$circumference = 2 * $pi * $radius;
if ($radius < 0) {
$circumference = 0;
}
print "The circumference of a circle with radius $radius is $circumference.\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 then calculated the circumference, too; there's more than one way to do it. That's why each exercise answer starts with "Here's one way to do it."
Here's one way to do it:
#!/usr/bin/perl -w
print "Enter first number: ";
chomp($first_response = <STDIN>);
print "Enter second number: ";
chomp($second_response = <STDIN>);
$result = $first_response * $second_response;
print "The result is $result.\n";
Note the newline at the end of the string in the seventh line, contrasted with its absence in the second and fourth lines. The first two messages are prompts, for which user input was desired on the same line. This last message is a complete statement; if we had left the newline out of the string, the shell prompt would appear immediately after the message. Not very cool!
Here's one way to do it:
#!/usr/bin/perl -w
print "Enter a string: ";
$string = <STDIN>;
print "Enger a number of times: ";
chomp($number = <STDIN>);
$result = $string x $number;
print "The result is:\n$result";
Like the previous exercise, the second through fifth lines ask for, and accept, values for the two variables. Unlike the previous exercise, we don't chomp the newline from the end of the string, because we need it! The sixth line takes the two entered values and performs a string repetition on them, then the result is displayed. Note that the interpolation of $result is not followed by a newline, because we believe that $resuLt will always end in a newline anyway. 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 just 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
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 accidentally spell the wrong thing! If the x runs up against the preceding variable name $string, Perl will see $stringx which won't work.
[1]
If you'd prefer a more formal sort of constants, the constant pragma may be
what you're looking for.
Return to the page from whence you came
[2]
It nearly did change by a legislative act in the state of Indiana.
http://www.urbanlegends.com/legal/pi_indiana.html.
Return to the page from whence you came