|
Packages in packages in Perl
In perl, all your variables (except "my" ones, which we'll leave for another day) are arranged into packages, and by default that's a package called "main". So if you write about a scalar called $jeremy, that's really $main::jeremy and if you write about a named block of code called &mary, that's really &main::mary.
Why?
So that, as your programs grow, you can arrange all the variables and pieces of code into their own uniquely named modules. That then allows you to use the same name several times over without a conflict.
Good news - but it doesn't go far enough so far; what I've described only goes one level further and module files (for example) would all end up in a single massive "pancake" directory. So the :: notation can be nested, and package namespaces can be nested within package namespaces. As far as file organisation is concerned, you'll be working with a directory structure. This sort of thing is very much used on the CPAN, from where you can download modules such as Net::FTP - to be found once installed in a file called FTP.pm in the Net directory.
Here's an example program and some modules that show this in action.
$ perl pk2lev
tutor
designer
beauty therapist
$
And the pk2lev program:
use ellis::graham;
use ellis::lisa;
use whelan::lisa;
print ellis::graham::job();
print ellis::lisa::job();
print whelan::lisa::job();
The directory structure:
$ ls ellis whelan pk2lev
pk2lev
ellis:
graham.pm lisa.pm
whelan:
lisa.pm
$
And a sample module file:
$ cat ellis/lisa.pm
package ellis::lisa;
sub job {
return "designer\n";
}
1;
$ (written 2005-12-16 20:40:31)
Associated topics are indexed under P209 - Subroutines in PerlP218 - Perl - More ObjectsP616 - Perl - FTP and Telnet Modules
Some other Articles
MySQL permissions and privilegesDesign - one name, one actionBigger Box CampaignCopyright - how much can I legally copy?Packages in packages in PerlPerl course during the week, getting married at the weekendHalal in MelkshamGetting favicon to work - avoiding common pitfallsGreater Western Franchise AwardedApache httpd - serving web documents from different directories
|
1892 posts, page by page
Link to page ... 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38 at 50 posts per page
This is a page archived from The Horse's Mouth at
http://www.wellho.net/horse/ -
the diary and writings of Graham Ellis.
Every attempt was made to provide current information at the time the
page was written, but things do move forward in our business - new software
releases, price changes, new techniques. Please check back via
our main site for current courses,
prices, versions, etc - any mention of a price in "The Horse's Mouth"
cannot be taken as an offer to supply at that price.
Link to Ezine home page (for reading).
Link to Blogging home page (to add comments).
|
|