I, like many careful programmers, shy away from globals. Do some searching if you don't already know why.
I seem to be in a small minority in disliking global package names. I wish perl supported use strict 'packages'; which would make it a fatal error to use a symbolic reference to a package.
In the full implementation, this requires defining a new type of reference, a (non-symbolic) ref to a package. But you can get many of the benefits without waiting for the full implementation.
Now, modules on CPAN need nice, long, specific, unique names. And the location where they are installed (locally) usually matches those long names (and for good reason). So I'm just fine with mentioning the long name once in order to tell Perl where to find the local copy. But you should never mention it again.
The best way to prevent name conflicts is to use lexicals instead of globals. That is one reason people suggest open my $log, ... over open LOG, .... And the best way to deal with modules is the same, except that Perl doesn't yet support making this easy all of the time.
So Perl code should look more like:
my $Customer= load Acme::Business::Manufacturor::Customer; ... my $cust= $Customer->new( ... );
No more global package names being thrown all over your code. No need to make hackish (global) copies of symbol tables to do it, either.
So, for the scenario presented, the better interface would be:
my %BusMod= loadAll "Acme::Business::Manufacturor::*"; ... my $cust= $BusMod{Customer}->new( ... );
So then you don't have globals, can be introspective and discover exactly which modules were loaded, etc.
Now patch Perl (probably Perl 6) so __PACKAGE__ returns a non-symbolic reference to the current package (which stringifies to the package name), so require returns that (by default but with a way to override that), and then the real power comes in. It allows me to have a slightly hacked copy of a module in a different location and I can use both copies of the same module at the same time. I wrote more about that in some node long ago.
Ah, well, that probably doesn't convey the idea all that well, but I thought it worth throwing out again in the context of this feature idea.
- tye
In reply to Re: A Perl Version of Java's import (lexicals > globals)
by tye
in thread A Perl Version of Java's import
by Ovid
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |