Peng Yu said:
http://www.tutorialspoint.com/perl/perl_oo_perl.htm
I checked some perl modules, it seems that the filename is always the
same as the package name. I'm wondering if this is required. Or the
package name and the filename can be different?
In a non-OO module that exports functions, they have to be the same for
Exporter to do its thing. When you use() a module - let's call it "Foo",
the following steps are basically what happens:
BEGIN {
require Foo;
import Foo qw(args);
}
The second step is optional - if there's no import() function in module
Foo, the use() will still succeed.
Now, let's suppose that package Bar is in Foo.pm. Foo.pm is compiled by
the require() with no problems. Because its code is in package Bar, the
import() function, explicitly declared or inherited from Exporter, is
compiled into that package too.
But the use() still tries to call an import() function in package
Foo. Worse, because import() is optional it will *silently* fail. The
only clue the user will get that something has gone wrong, is that none
of the functions declared in package Bar will be usable without a fully-
specified package name, i.e. Bar::baz(), because the import() failed to
create aliases for them in the current package.
Other things can depend on the filename = package convention too. Take
perldoc, for example. If a user of your module runs "perldoc Foo" to get
your module's docs, it will read the docs found in Foo.pm. The user's
going to be confused if those docs describe a package named "Bar."
Even if your module is "pure" OO and doesn't export anything, following
the established convention helps avoid confusion - imagine a maintenance
programmer looking at your script a year from now, and seeing this:
use Foo;
my $bar = Bar->new();
It's not very intuitive to see a class Bar defined in Foo.pm.
So no, it's not required in theory. But the convention of naming them
the same is followed so widely, that in practice it's really more or
less required.
sherm--