Introduction to Perl
Turning Your Pseudocode into Something the Computer Understands


Prep readings:
- Text: Chapters 1-3; Appendix B; Class notes
- Files:

In this assignment, we wish to:
- learn how to execute an existing Perl program
- learn how to edit and run a Perl program derived from pseudocode
Testing Your Perl Installation
You are free to work on your Perl programming assignments from the
computer laboratories in Buildings 58/58A, or from your home computer using a remote
login to eDesktop.
Once you are sitting at a machine with Perl, your first goal is
to get comfortable with testing a simple program, such as a program
which prints a message or two to the screen. Here are the steps for
doing this with the test.pl file provided above:
- First, save the test.pl file to your computer.
Working in UWF computer lab using H: or I: drives
If you are sitting at one of the computers on campus where Perl has been installed for
you, then you will need to create a subdirectory on your H: or I: drives to store your
Perl programs. The H: drive is used for files you don't want others to see, and the I:
drive is the opposite, allowing anyone on the Web to view your files. Let's assume
you are going to develop and store your files on your I: drive, since ultimately
you need to post all of them on your course Webpage for grading. In the bioinformatics
folder you created in the last assignment, create a new folder (using Windows Explorer)
called MyProgs. Next, click on the link above for the file test.pl and save it into the
I:\bioinformatics\MyProgs directory.
Working from home using eDesktop and the C: drive
Once you login, the directions will be the same as "Working from home".
Working from home using C: drive
If you followed the default prompts when installing Perl on your home computer, you will
already have a directory called C:\Perl on your C: drive. In an MS-DOS prompt window,
issue these commands to create a subdirectory for your Perl programs:
cd C:\Perl
mkdir MyProgs
Now, click on the link above for the file test.pl and save it into the C:\Perl\MyProgs directory.
- Now, open the file in Notepad and carefully read through it.
Notice that in Notepad, you can go to a particular line number using
the Edit-Go To menu. Try this option now.
(This will be important later when you need to fix syntax
errors that occur on a particular line number.)
- Next, open an MS-DOS prompt (also known as a Command Prompt) window.
There are several methods for doing
this. In earlier versions of Windows, you can usually locate the
MS-DOS prompt application off of the Start-Programs menus. In
newer versions of Windows, you might try the sequence
Start-Programs-Accessories-Command Prompt. If all else fails,
search your computer for files/folders with the name
"MS-DOS" or "Command Prompt". (If you are a Mac user, please e-mail me
with the name/location of the corresponding tool on that platform.)
- In the MS-DOS/Command prompt window, change into the directory where you
stored the test.pl file.
Working in UWF computer lab using H: or I: drives
The command
cd I:\bioinformatics\MyProgs
will place you in the "MyProgs" subdirectory
off of the "bioinformatics" subdirectory on the I: drive.
Working from home using eDesktop and the C: drive
Once you login, the directions will be the same as "Working from home".
Working from home using C: drive
The command
cd C:\Perl\MyProgs
will place you in the "MyProgs" subdirectory
off of the "Perl" subdirectory on the C: drive.
- Once you are in the subdirectory
where you stored the test.pl file,
type the
dir
command to confirm that the
test.pl file is there.
- Now, issue the command
perl test.pl
in the MS-DOS/Command prompt
window to interpret/compile and run the Perl
program in the file test.pl. If you are successful, you should see two messages displayed
on the screen, as described in the comments of the test.pl file.
- Next, edit the test.pl file and remove one of the semicolons in the print
statements. What happens when you now try to run the program? Note the line
numbers indicated in any error messages. Use Notepad to find these line
numbers and fix the indicated error. Run the program again to make sure it still works.
- Finally, edit the test.pl file with Notepad so that the welcome message
includes your first and last names. Save the file and run the new program
to make sure it still works with the modified message.
Turning Pseudocode into Perl Source Code
In class we developed pseudocode and a rough flowchart for the
design of a program to count the number of vowels in a word
provided by the user. Locate and review your class notes
on this. Our next step is to start thinking about how to turn
our pseudocode into Perl code that we can execute. Let's begin
by considering
the major steps of the algorithm we developed in class:
- Obtain input word from the user
- Count the number of vowels in input word
- Display final vowel count results
Steps 1 and 3 are the easiest to implement, so I suggest you start with
a program to do just these steps. This goes along with the idea of
coding around five lines of code at a time, in an
Edit-Run-Revise (and Save) cycle. This helps you to make progress
on an assignment, and build your confidence. So let's begin:
- Save the file vowels1.pl to your directory of Perl programs. Open
the file in Notepad and read through it. Carefully consider these
questions:
- How easy/difficult is it to understand the overall meaning of
the text/code in this file?
- How easy/difficult is it to understand which parts of the code
in this file correspond to your pseudocode for your vowel counting
algorithm?
- Which pages in Appendix B appear relevant to understanding the Perl
code in this file?
- How easy is it to understand the variables that are used in this
program? How are the variables listed and initialized in the program?
Why is this important?
- Now, open an MS-DOS/Command Prompt window and run the program. What results
do you get? How informative is the program in telling you what the program
does, what input to enter, what output to expect, etc.?
- Now, try uncommenting some of the lines of code in the section of the program
labeled with a NOTE comment. For example, uncomment the
line:
$vowel_count = 0;
Rerun the program. What happens?
- Uncomment the remainder of the code in this section (from the start to the
end of the for loop). Rerun the program. What happens now?
Think, think, think
If all goes well in the steps above, you just executed a non-trivial Perl
program. Congrats! Now, think carefully:
- Readability and
Reuse, Reuse, Reuse -
Are there any other changes you should make to the comments in the code to
improve its readability? How you use indentation in your code and
meaningful variable names are major
factors in this area. Imagine that you are going to put this code aside
and return to it a year from now. Would you be able to understand what
the programs do by reading the comments? Would you be able to easily
cut-and-paste pieces from these files into another Perl program to do
something else? Make any changes to the code now that you feel improves
issues involving readability and
reuse. You can overwrite your old version
of vowels1.pl with your new version. If needed, you can always
get the older version off the Web. Make only a couple of changes at
a time, and always rerun your the new and improved
vowels1.pl file to make sure it still works.
- User interface -
Are there any additional changes you should make to the print statements
in the code so it is easier to understand what happens when the programs
run? Again, think about revisiting these programs a year from now. If you
couldn't read the source code and had to run the programs to figure
out what they do, would the output make sense?
Make any changes to the code now that you feel improves
the user interface. After making any
changes, rerun the program to make sure it still works.
- Finally, open up an additional Command-Prompt window and position it next
to your current window. Run your new and improved vowels1.pl program in
one window, and my vowels2.pl program in the other. What is different
about the two programs in terms of interface? What is different if you
look at the source code, side-by-side? Which one do you prefer and why? What additional
changes would you suggest for either version?
The sequence of steps you have just followed mimics the way most good programmers develop
a computer program. They typically begin with pseudocode (and possibly a flowchart),
and start creating Perl code from that, one or two lines of code at a time. Some
programmers polish their code up as they go along, while others get chunks of the
functionality working, and then tidy it up before moving on to the next chunk. Either
method works, as long as you aren't saving the clean up/tidy up to the end - that's
essentially wasted energy and makes debugging your code very time-consuming. You will develop your own style of programming
as you proceed through this class, but I encourage you to develop good habits now
(good readability,
reuse, reuse, reuse, and a great
user interface)
- you will
reap many rewards along the way if you do!
Possible Problems and Solutions
Check back later for any additions to this section.
- In previous semesters, for security reasons on on-campus machines,
you were not able to execute programs located on the H: or I: drives,
because file-sharing between these drives was disabled.
If you encounter this problem, you will need to copy your Perl files to
a subdirectory on the
C:\ drive to execute them. I recommend you create a subdirectory called MyProgs
under C:\Perl for this purpose.
After making any changes to the files in the C:\Perl\MyProgs directory, be
sure to copy
them back into your ArgoNet account before you log out, or your changes will
be lost. Use the I:\ (Web) drive if you want to post
the file on the Web, or the H:\ (Home) drive for files you want to save, but not post.
Getting Credit for Your Progress
To get credit for this assignment, you need to complete three additional steps:
- Create a Perl program called nucleotide-counting1.pl which counts and displays the number of instances
of the letters: A, C, G, and T in a user-provided sequence of letters. Provide
output counts of each of these letters. Your program should be insensitive
to case (i.e., it should be able to process uppercase and lowercase letters in
the same manner). Your program should be well-written and user friendly, using
the guidelines provided here and in class.
- Include a link to your new Perl program on your course Webpage, under
a heading titled, "Lab: Counting Nucleotides."
Congratulations!
You just made it through your second assignment!
Feel free to check out how the other students
in the class did on this assignment.
These pages are optimized for viewing
under Netscape.
© Copyright 2003.
Melanie A. Sutton, Ph.D.
(msutton@uwf.edu)
All rights reserved.