Hour 17. Writing Modules

 <  Day Day Up  >  

Hour 17. Writing Modules

What You'll Learn in This Hour:

  • Building a module

  • Scoping

  • The our Statement

In Hour 8, "Functions," you learned about subroutines. Subroutines allow you to take a frequently used pieces of code and assign it a name to create a small self-contained "mini-program" with its own variables .

After you've written enough programs, you'll find yourself creating the same functions over and over again for tasks . If you write a lot of file-manipulation programs, you'll probably have code to lock files and unlock them in several places; programs with user interaction (prompts and input) will have functions to display prompts, wait for answers, and reject bad input; and programs that work with a particular database might have functions for reading, writing, and editing data.

If every program contained copies of the required functions, cut-and-pasted into the program, your programs would soon become unwieldy to maintain because

  • Every enhancement or bug fix would require hunting down and changing each version of each function in every program that used it.

  • If you weren't perfect in your cutting-and-pasting or updating, you've now got several versions of that function in your programs.

  • The programs themselves would become huge

Instead programmers have settled on the concept of function libraries . Every programming language and system has them. Sometimes they're called libraries, modules, or archives. For example, with operating systems, Microsoft Windows systems have dynamically linked libraries (DLLs) and Linux systems have shared libraries. Perl's method of storing collections of functions is called a module .

Entire books have been written on creating modules and function libraries. In this hour, you're going to explore writing modules to accomplish a few simple goals, namely making your code easier to maintain, share, and debug.

 <  Day Day Up  >  


SAMS Teach Yourself Perl in 24 Hours
Sams Teach Yourself Perl in 24 Hours (3rd Edition)
ISBN: 0672327937
EAN: 2147483647
Year: 2005
Pages: 241

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net