Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Old .pl modules versus new .pm modules

I'm a beginner in Perl and I'm trying to build in my head the best ways of structuring a Perl program. I'm proficient in Python and I'm used to the python from foo import bar way of importing functions and classes from python modules. As I understood in Perl there are many ways of doing this, .pm and .pl modules, EXPORTs and @ISAs, use and require, etc. and it is not easy for a beginner to get a clear idea of which are the differences, advantages and drawbacks of each (even after reading Beginning Perl and Intermediate Perl).

The problem stated, my current question is related to a sentence from perldoc perlmod:

Perl module files have the extension .pm. The use operator assumes this so you don't have to spell out "Module.pm" in quotes. This also helps to differentiate new modules from old .pl and .ph files.

Which are the differences between old .pl way of preparing modules and the new .pm way?

Are they really the old and the modern way? (I assume they are because Perlmod says that but I would like to get some input about this).

like image 887
joaquin Avatar asked Dec 04 '22 11:12

joaquin


2 Answers

The use function and .pm-type modules were introduced in Perl 5, released 16 years ago next month. The "old .pl and .ph files" perlmod is referring to were used with Perl 4 (and earlier). At this point, they're only interesting to computer historians. For your purposes, just forget about .pl libraries.

like image 59
cjm Avatar answered Jan 26 '23 00:01

cjm


Which are the differences between old .pl way of preparing modules and the new .pm way?

You can find few old modules inside the Perl's own standard library (pointed to by @INC, the paths can be seen in perl -V output).

In older times, there were no packages. One was doing e.g. require "open2.pl"; which is analogous to essentially including the content of file as it is in the calling script. All functions declared, all global variables were becoming part of the script's context. Or in other words: polluting your context. Including several files might have lead to all possible conflicts.

New modules use package keyword to define their own context and name of the namespace. When use-ed by a script, new modules have possibility to not import/add anything to the immediate context of the script thus prevent namespace pollution and potential conflicts.

@EXPORT/@EXPORT_OK lists are used by standard utility module Exporter which helps to import the module functions into the calling context: so that one doesn't have to write all the time full name of the functions. The lists are generally customized by the module depending on the parameter list passed to the use like in use POSIX qw/:errno_h/;. See perldoc Exporter for more details.

@ISA is a Perl's inheritance mechanism. It tells Perl that if it can't find a function inside of the current package, to scan for the function inside all the packages mentioned in the @ISA. Simple modules often have there only the Exporter mentioned to use its import() method (what is also well described in the same perldoc Exporter).

like image 33
Dummy00001 Avatar answered Jan 25 '23 22:01

Dummy00001