in reply to Multiple classes in modules

If the file name is MyTest.pm, just do use MyTest or BEGIN { require "MyTest.pm"; } (subbtle differences left to you to lookup). use doesn't care what packages are used inside the file. For example,
use Bah looks for Bah.pm, and
use Foo::Bar looks for Foo/Bar.pm,
even if they contain packages
Cow::Moo,
Cow::Eat and
Milkmaid.
perl looks for the modules in the the paths listed in @INC. See use lib for a method to edit @INC.

Replies are listed 'Best First'.
Re^2: Multiple classes in modules
by johnnywang (Priest) on Oct 05, 2004 at 20:06 UTC
    That's indeed true. I didn't know that, but tried the following, it works:
    # file: MyDir/MyTest.pm package MyDir::Foo; sub new{ my $class = shift; my $self = {}; bless $self, $class; return $self; } sub foo{ print "Foo\n"; } package MyDir::Bar; sub new{ my $class = shift; my $self = {}; bless $self, $class; return $self; } sub bar{ print "Bar\n"; } 1;
    With test code:
    use strict; use MyDir::MyTest; my $foo = MyDir::Foo->new(); $foo->foo(); my $bar = MyDir::Bar->new(); $bar->bar();
    Updated. In fact, one can simply name the package as "Foo" and "Bar" instead of "MyDir::Foo" and "MyDir::Bar". In other words, "use" is tied to the directory layout, but not to the contents of the files. Am I too general here?

      The catch is that use MyDir::MyTest; will attempt to call MyDir::MyTest::import. That can be avoided by using either
      use MyDir::MyTest ();
      or
      BEGIN { require MyDir::MyTest; }
      or
      BEGIN { require "MyDir/MyTest.pm"; }