(Search & Sitemap)
> Writing >
Miscellaneous Nonfiction >
Tinkering with Perl
Skip Back
Previous
42
43
44
45
46
47
48
49
50
51
Next
Skip Forward
Printer-Friendly Version
One kind of useful subroutine is one that does some calculations, or something like that, and gives a value as an answer. For example, suppose we wanted to be able to assign the total price to a variable in our fruit market example. This is what functions are for.
To use a function, we would do something like this:
$TotalPrice = &CalculatePrice($NumberOfApples, $NumberOfOranges, $NumberOfBananas);
And the CalculatePrice function is almost exactly the same as the TellPrice procedure. Here is its listing, with all of the changes in bold:
sub CalculatePrice()
{
$NumberOfApples = shift(@_);
$NumberOfOranges = shift(@_);
$NumberOfBananas = shift(@_);
@RemainingArguments = @_;
$PriceForApples = $NumberOfApples * $PricePerApple;
$PriceForOranges = $NumberOfOranges * $PricePerOrange;
$PriceForBananas = $NumberOfBananas * $PricePerBanana;
$TotalPrice = $PriceForApples + $PriceForOranges + $PriceForBananas;
return $TotalPrice;
}
Now, instead of printing out the result, the subroutine returns it. Now the part of the program that called it (above) will have the result plugged in where it was given.
Another very useful function can be used to get a yes or no answer from the user. This subroutine doesn't itself tell the user what he's supposed to answer yes or no about; that would be done just before calling this subroutine.
sub confirm()
{
$UserAnswer = <>; # Read in the user's answer.
chomp $UserAnswer;
if ((not($UserAnswer eq "n")) and # If it's not a yes or a no,
(not($UserAnswer eq "y")) and
(not($UserAnswer eq "N")) and
(not($UserAnswer eq "Y")))
{
while ((not($UserAnswer eq "n")) and # Keep going until we get a yes
(not($UserAnswer eq "y")) and # or a no.
(not($UserAnswer eq "N")) and
(not($UserAnswer eq "Y")))
{
print "Please answer \"y\", for yes, or \"n\", for no.\n";
$UserAnswer = <>;
chomp $UserAnswer;
}
}
# If we've gotten here, the user has given a yes or a no answer.
return (($UserAnswer eq "Y") or ($UserAnswer eq "y"));
}
This procedure makes sure that the user types a y or n, then returns a conditional that says that the user said yes. So, for example, one could have the following segment of code:
print "Do you wish to continue (y/n)? ";
if (&confirm())
{
print "Continuing...\n";
}
else
{
print "Bye!\n";
exit 0;
}
That segment of code asks the user if he wants to continue. If he says yes, then the program says "Continuing...", and continues. If he says no, the program says "Bye!", and exits.
That's all of the bare bones rudiments of programming. Now we can move onward to looking at a few sample programs, and then begin tinkering!
Tinkering with Perl is a free book that provides an introduction to programming in Perl, as well as a basic reference for things like foreach in Perl, if-then, and if-then-else, in addition to providing a glossary where you can find definitions for concatenate and other terms.
Tinkering with Perl may be one of the most popular offerings on this site, but it's not the only attraction. You can read a tongue-in-cheek Game Review: Meatspace, read an even more offbeat customer service survey (whether or not you actually fill it out), and spend a few minutes wishing your boss would read, The Administrator Who Cried, "Important!" (Not to mention that there are other things you can read here besides tech stuff, from Janra Ball: The Headache to The Spectacles.)
(Search & Sitemap)
> Writing >
Miscellaneous Nonfiction >
Tinkering with Perl
Skip Back
Previous
42
43
44
45
46
47
48
49
50
51
Next
Skip Forward
Printer-Friendly Version