A Gentle Introduction

 <  Day Day Up  >  

The use directive allows you to use a module in your Perl program. For example, to include the Cwd module in your program, simply place the following somewhere in your code:

 use Cwd; 

It doesn't matter where in your code you place the use Cwd , although for clarity and maintainability, it should probably be placed near the beginning of the program.

You may recall that we used the following module in Hour 10, "Files and Directories." At that time, however, you didn't know how it worked. When you run a program with use Cwd, what actually happens is this:

  1. The Perl interpreter opens your program and reads in all the code until the use Cwd statement is found.

  2. When your Perl interpreter was installed, it was informed which directory it should search for modules (which is usually Perl's installation directory). That directory is searched for a module called Cwd . This module is a file containing Perl code.

  3. Perl reads the module, and all the functions and variables that are required for the module to work are initialized .

  4. The Perl interpreter continues reading and compiling your program where it left off.

That's all there is to it. Once Perl has read your entire program, it's ready to run, using all the functionality that the module provides.

By the Way

You might notice that use strict looks a lot like use Cwd . Without confusing the issue too much, let's just say that the use statement is a general-purpose instructionto the Perl interpreter to do something (this is called a pragma ). In the case of use strict , it changes the behavior of the interpreter to be strict about references and bare words; there is no module called s

 <  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