| ACADEMIC COMPUTING and COMMUNICATIONS CENTER | ||||||||
| |||||||||||||||||||||
Packages / Modules | |||||||||||||||||||||
| Packages | |||||||||||||||||||||
|
What with all the third part libraries for perl, there needs to be a sane way to manage names. Suppose two different packages use the variable $count. Chaos! Unless, of course, these variables are somehow made local. The package statement allows different libraries to keep their own space. Perl allows any statement to use any package. So privacy is not enforced, but don't go violating it unless you know what you are doing. Every global variable lives in a package. If you don't explicitly declare the package, then the default is main:: or just :: for short. You can refer to a variable in a package explicitly, such as $mypack::count. (This is the $count variable in the mypack package, and is not written mypack::$count). Or you can use the package statement to reset the default package from that point on. (Well, at least until the next package statement, or end of file.) One caveat. The special variables, such as $_ or $/, are always in main::, no matter what. Also note that package variables cannot be my() variables. $count = 0; package mypack; $count = 1; print $main::count; # 0 print $count; # 1 package main; print $count; # 0 |
|||||||||||||||||||||
| Modules | |||||||||||||||||||||
|
A perl module is just a way to arrange a library to make it easy to use. It has:
Exmample:
use lib '.'; # If module lives in current directory
use Date::Calc ('check_date'); # load module, import symbol
|
|||||||||||||||||||||
| Perl II | Previous: 3. References | Next: 5. Classes |
| 1999-3-3 BobG |
|