Chapter 2
This comprises practically all of the universally applicable directions that can
be given about using modules. In practice, you'll have to at least skim the docu
mentation for each module you want to use in order to find out how it's meant to
be used. As you'll see in the following text, there are many, many ways to do it!
Packages
Perl supports modular programming through packages. Packages provide a sep
arate namespace for variables and subroutines declared inside. This means that
two packages can have subroutines and variables with the same names without
inadvertently stepping on each other's toes. You declare a package with a package
statement:
package CGI::SimplerThanThou;
After that, any subroutines or global variables you declare will be in the package.
For example, this code creates a subroutine param() in the CGI::SimplerThanThou
package:
package CGI::SimplerThanThou;
sub param {
return ( fake , params );
}
Now if you want to call this subroutine from some other package, you'll need to
prefix it with the package name:
my @params = CGI::SimplerThanThou::param();
Variables can also be created in a package. Here's an example that creates a
hash called %params in the CGI::SimplerThanThou package:
package CGI::SimplerThanThou;
%params = ( ten => 10 );
To refer to this variable from another package, you again use the package prefix:
print "Ten: $CGI::SimplerThanThou::params{ten}\n";
24
footer
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