Matisse Enzer wrote:
I'm discussing some potential refactorings at $work at wanted to give an articulate explanation of the benefits of having package declarations match file names, so that:

   # file is Foo/bar.pm
   package Foo::Bar;

That was probably a typo, but I hope you mean Foo/Bar.pm. Getting the cases wrong will bite you even on case insensitive filesystems. Here's a classic:

$ perl -wle 'use Strict;  $foo = 42;  print $foo'
42

Look ma, I'm using strict! No, it's not. "use Strict" is the equivalent of C<< require Strict; Strict->import if Strict->can("import") >>. This will load Strict.pm, and since the filesystem is case insensitive it will work, and then call Strict->import. Class methods are not case insensitive so it will not exist and do nothing.


One reason is so that when you see a "package" statement, you know what the corresponding "use" statement would be, and when you see a "use" statement, you know what the corresponding package is, and have a good clue about the path path of the file you are importing.

What are other good reasons to have package declarations match file paths?

Eric already covered the import() issue.

There's also the principle of least surprise. How do you load class Foo::Bar? "use Foo::Bar". How do you get the docs? "perldoc Foo::Bar". Where do I find the code for Foo::Bar? In Foo/Bar.pm. How do you inherit from it? "use base qw(Foo::Bar)". Simple, no investigations or local knowledge necessary.

As a counter example, consider Tie::StdHandle. It makes a tied handle work like a regular file handle. Very handy. How do you use it? C<< use base qw(Tie::StdHandle) >> right? Wrong, it's in Tie/Handle.pm, of course. [1] So now you have to invoke it manually.

        BEGIN {
                require Tie::Handle;
                our @ISA = qw(Tie::StdHandle);
        }

As is often the case with irregular code, Tie::StdHandle has no documentation.


[1] 5.10.0 finally moved it to its own file.

--
Robrt:   People can't win
Schwern: No, but they can riot after the game.

Reply via email to