What is the difference between library files and modules in Perl?
3 Answers
It's all Perl code to perl
. All distinctions are purely idiomatic.
Perl code meant for inclusion that uses a package
directive:
- Called "module".
- Usually has the extension
.pm
. Must have this extension foruse
to find them. - Should always be loaded with
require
, possibly viause
.- Must therefore return a true value.
- More modular, better supported by CPAN.
Perl code meant for inclusion that doesn't use a package
directive:
- Called "library". (At least historically. These days, "library" might also be used to refer to a module or distribution.)
- Usually has the extension
.pl
. - Should always be loaded with
do
. - Pollutes the caller's namespace.
- Usually indicative of a substandard design. Avoid these!
Perl code meant for direct execution by interpreter:
- Called "script".
- Usually has the extension
.pl
, or none at all. - Will probably start with a shebang (
#!
) line so they can be started without specifyingperl
.

- 367,544
- 15
- 269
- 518
Library files (I'm assuming you mean require 'foo.pl'
stuff here) are an obsolete (pre-Perl 5) form of external module. For the most part, you shouldn't need to care any more, although there are still some Perl 4 installations around and therefore still some Perl code that remains backward compatible with them (and there's some code that's simply never been updated and still loads getcwd.pl
etc.).

- 59,309
- 11
- 123
- 114
Nothing. They are both files that contain Perl code. Here are some of the possible circumstantial differences, though.
- A perl executable is more likely to have a
#!/bin/perl
shbang. - Old
.pl
Perl libraries (hence the 'p' + 'l') are more likely to expect to be required than.pm
modules. - Perl 5 style (
.pm
) modules are more likely to useExporter
-- although even newer module eschew exporting anything.

- 29,660
- 2
- 47
- 102
-
3A module always defines a package (or else it's not a module). a `.pl` library usually doesn't. – hobbs Jun 16 '11 at 19:24
-
I don't know what you mean by #2. `.pm` (i.e. files a `package` directive) should always be loaded with [`require`](http://perldoc.perl.org/functions/require.html), possibly via [`use`](http://perldoc.perl.org/functions/use.html). `.pl` (i.e. files without a `package` directive) should always be loaded with [`do`](http://perldoc.perl.org/functions/do.html). – ikegami Jun 16 '11 at 21:44
-
@ikegami, 1) If someone gave you a .pm file, would you expect to need to require (or even `do`) it? Or is it more of a question if you had a .pl file. 2) Package directive doesn't enter into it when talking about the file types, but out of the two--I'd more likely expect that a .pm file defines a package than a .pl file. So it's also in the vein of a difference of practice in creating the two types of files. Again, suggested practice is different from *de facto*--or characteristic--practice--in my experience with .pl files--I have never seen somebody `do` them--advisability not withstanding. – Axeman Jun 24 '11 at 16:38
-
If someone gave me a .pm file, I would expect it to have a `package` directive, and thus, I would expect to be able to load it using `require`. I too would also more likely expect that a .pm file defines a package than a .pl file, which is why I said .pm should always be loaded with `require`, and why I said .pl should never be loaded using `require`. – ikegami Jun 24 '11 at 21:41