Chapter 6. Subroutines and Bugs

In this chapter you’ll extend your basic knowledge in two directions:

  • Subroutines

  • Using the Perl debugger

Subroutines are an important way to structure programs. You’ll use them in Chapter 7, where you’ll learn how to use randomization to simulate the mutation of DNA, and in all the following chapters. The Perl debugger examines a program’s behavior in “slow motion” and helps you find those pesky bugs.

Subroutines

Subroutines are an important way to organize a program and are used in all major programming languages.

A subroutine wraps up a bit of code, gives the code a name, and provides a way to pass in some values for its calculations and then report back the results. The rest of the program can then use the subroutine’s code just by calling its name, giving the needed values to pass in to the subroutine code and then collecting the results. This use or “invocation” of a subroutine is commonly referred to as calling the subroutine. You can think of a subroutine as a program within a program; just as you run programs to get results, so your programs call subroutines to get results. Once you have a subroutine, you can use it in a program simply by knowing which values to pass in and what kind of values to expect it to pass out.

Advantages of Subroutines

Subroutines provide several benefits. They endow programs with abstraction, modularization, and the ability to create large programs by organizing the code into manageable chunks with defined inputs and ...

Get Beginning Perl for Bioinformatics now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.