jakeeboy has asked for the wisdom of the Perl Monks concerning the following question:

I've been reading about modules in the Camel book (3rd edition) and in it states that different packages can be put into a module like so:
package a; { #some code } package b; { #some other code }
The question I have is how do I specify that I want to restrict usage of the module to package b since the name of the module is a.pm I would use a; but if I write use a::b; it will look for b.pm in directory a?

But I want to use package b's variables and subs how do I get to them?

Thanks for the info....

jakeeboy

Title edit by tye as one-word titles complicate simple searches

Replies are listed 'Best First'.
Re: Modules
by chromatic (Archbishop) on Feb 08, 2002 at 17:26 UTC
    There's another bit in the camel that talks about privacy, saying that nothing prevents you from walking into someone else's living room except politeness. There are very few window bars and shotguns in Perl.

    Package names aren't required to have anything to do with file names -- and when you use or require a file, all Perl cares about is translating the name you give into a file path, finding the file, and compiling it. (If it can't find an import() in the associated package, it doesn't really care.)

    It's good form to keep package names in sync with their file names. If you need to have separate packages in the same file, they ought to be helper packages. In this case, you're stuck using a. If you run into this often, break out b into its own file.

Re: Modules
by AidanLee (Chaplain) on Feb 08, 2002 at 17:24 UTC

    There are a couple ways to do this (suprise, suprise).

    consider that your file is called 'ab.pm'

    BEGIN { require ab; } $a::variable; &b::subroutine;

    now, if both packages inherit from Exporter to export variables:

    BEGIN { require ab; import a qw( $var sub ); import b qw( sub2 sub3); }

    If you only have one inheriting from Exporter, name the file after it. assuming it's called 'a.pm' and package 'a' inherits from Exporter:

    use a qw( $var2 &sub ); BEGIN { import b qw( $foo %bar ); }