Perl Module Basics
Packages may not seem immediately useful, but they form the basis for 
modular programming in Perl by providing encapsulation. Since each package 
forms a separate namespace for variables and subroutines, a package is free to 
implement its functionality without concern for the rest of the program. For 
example, let's say I'd like to override Perl's logarithmic function, log(),
3
 inside 
my package Acme::PotatoPeeler:
package Acme::PotatoPeeler;
sub log {
   print STDERR "PotatoPeeler Says: $_[0]\n";
}
If packages didn't provide encapsulation, I would have just overridden the loga 
rithm function for the entire program, and the stock simulation algorithms in 
Acme::StockPicker wouldn't work so well! Of course, if that's what you really want, 
you can do that too. I'll explain how to use packages to  redefine the world  later.
Symbol Tables
Packages work through the magic of symbol tables. Each package has a hash asso 
ciated with it called a symbol table. For every symbol in the package, there is a key 
in the hash. The value stored in the hash is a typeglob
4
 containing the value for 
the symbol.
Why doesn't the hash directly store the value of the variable? Perl supports, for 
better or worse, variables of different types with the same name you can have a 
scalar named $foo, an array named @foo, and a hash named %foo in the same package. 
The typeglob provides the level of indirection necessary to make this work.
Most compilers for other languages use symbol tables to keep track of variables 
declared in a program. Perl is unique in that it exposes its symbol tables to the pro 
grammer for examination and even manipulation at runtime. You can refer to a 
symbol table hash by using the package name plus a trailing package specifier, 
:: (double colon). Here's an example that prints out a sorted list of symbols for the 
File::Find package:
use File::Find;
print "$_\n" for sort keys %File::Find::;
3. Didn't know you could do that? I'll explain in more depth later in the  Exporting  section.
4. There isn't room here to dip into the arcane world of typeglobs. Suffice it to say that, outside 
of some useful idioms that I'll cover later, you can use them to do some really odd things that 
are probably best left undone.
25
25






footer




 

 

 

 

 Home | About Us | Network | Services | Support | FAQ | Control Panel | Order Online | Sitemap | Contact

web hosting perl

 

Our partners: PHP: Hypertext Preprocessor Best Web Hosting Java Web Hosting Inexpensive Web Hosting  Jsp Web Hosting

Cheapest Web Hosting Jsp Hosting Cheap Hosting

Visionwebhosting.net Business web hosting division of Web Design Plus. All rights reserved