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

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.