Tinkering with Perl

(Search & Sitemap) > Writing > Miscellaneous Nonfiction > Tinkering with Perl
Skip Back  Previous  44  45  46  47  48  49  50  51  52  53  Next  Skip Forward
Printer-Friendly Version

Friends and pets

Here, we will have a program that asks you about you friends and pets, and remembers them for as long as it's running. Let me give a listing. I am trying to document it well by putting in comments. Can you tell what it does? If you can't, look up the parts you can't understand.

#!/usr/bin/perl -w
#
# Friends and pets -- a program to keep track of your friends and their pets.
#
# Last modified 12-29-01, by Jonathan Hayward
#
# While you say you want to continue, it asks you for another friend's name,
# and then asks for the pet's name.  Then, it tells you all of the friends it's
# been told, and what their pets are.
#
# This is done with a while loop.
# The friends are stored in a list, and
# the pets are stored in a hash.
#
# We also use a couple of functions.
#

#
# The stuff it does in the beginning, called initialization.
#

# List the subroutines and functions.
sub Confirm();

#
# Confirm is a function that gets a yes or no answer from the user.
#
# Note that this function uses another function -- ReadLine.  It is
# possible, and indeed very useful, to have one subroutine or function use
# other subroutines or functions.
#

sub Confirm()
    {
    $UserAnswer = &ReadLine();
    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 = &ReadLine();
            }
        }
    # If we've gotten here, the user has given a yes or a no answer.
    return (($UserAnswer eq "Y") or ($UserAnswer eq "y"));
    }

# Make the list of friends and hash of pets empty, so that they don't contain
# anything.
@Friends = ();
%Pets = ();

# Have scalars that we can use for true and false conditional clauses.
$True = 1;
$False = 0;

# Welcome the user to the program.
print "Welcome to the friends and pets program.\n";

$ShouldContinue = $True;

#
# The main loop.  This is where the meat of the program is.
#

while ($ShouldContinue)
    {

    # Read in the friend's name.
    print "Please enter the name of your friend:\n";
    $NewFriend = &ReadLine();

    # Add the new friend to the list.
    @Friends = (@Friends, $NewFriend);

    # Read in the friend's pet.
    print "Please enter the kind of pet $NewFriend has:\n";
    $NewPet = &ReadLine();

    # Add the new friend's pet to the list of hashes.
    $Pets{$NewFriend} = $NewPet;

    # Print a blank line, so that the output doesn't look too crowded:
    print "\n";

    # Now, recite the friends and their pets.
    foreach $CurrentFriend (@Friends)
	{
	$CurrentPet = $Pets{$CurrentFriend};
	print $CurrentFriend ."'s pet is a $CurrentPet.\n";
	}

    # Finally, ask the user if he wants to continue.
    print "Do you want to continue (y/n)?\n";
    $ShouldContinue = &Confirm();

    # And we reach the end of the loop.
    }


# If we get here in the program, the user does not want to continue.
# So, we say "Bye!", and leave.

print "Bye!\n";
exit 0;

#
# The program will never get here by itself, because it is after the exit
# statement.  But we can still put procedures and functions here.  We will put
# two functions here:
#

#
# ReadLine is a function that reads a line in, and gets rid of the trailing
# newline.  This does input exactly as specified earlier.
#

sub ReadLine()
    {
    $UserInput = <>;
    chomp $UserInput;
    return $UserInput;
    }

A sample output for this program might be:

Welcome to the friends and pets program.
Please enter the name of your friend:
Fred
Please enter the kind of pet Fred has:
furball

Fred's pet is a furball.
Do you want to continue (y/n)?
y
Please enter the name of your friend:
David
Please enter the kind of pet David has:
dog

Fred's pet is a furball.
David's pet is a dog.
Do you want to continue (y/n)?
n
Bye!

See also:

Sample programs - Running average

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.)

Read more...

Top

(Search & Sitemap) > Writing > Miscellaneous Nonfiction > Tinkering with Perl
Skip Back  Previous  44  45  46  47  48  49  50  51  52  53  Next  Skip Forward
Printer-Friendly Version