In Perl, what is the difference between a .pm (Perl module) and .pl (Perl script) file?

What is the difference between a .pm (Perl module) and a .pl (Perl script) file?

Please also tell me why we are returning 1 from the file. If return 2 or something else, it does not raise any errors, so why do we return 1 from the Perl module?

+75
perl perl-module
Aug 04 '10 at 5:20
source share
2 answers

In the kernel itself, the file extension you use does not matter how perl interprets these files.

However, placing modules in .pm files following a specific directory structure that follows the package name provides convenience. So, if you have a module Example::Plot::FourD , and you put it in the directory Example/Plot/FourD.pm on the path to @INC , then use and require will be correct when considering the package name, as in use Example::Plot::FourD .

The file should return true as the last statement to indicate the successful execution of any initialization code, so usually end the file with 1; if you are not sure that it will return true otherwise. But it’s better to just put 1; if you add more operators.

If EXPR is a simple word, require implies the extension β€œ.pm” and replaces β€œ::” with β€œ/” in the file name for you to make loading standard modules easier. This form of loading modules is not subject to change your namespace.

All use is to determine the file name from the provided package name, require in the BEGIN block and call import in the package. Nothing prevents you from using use , but performing these steps manually.

For example, below I put the package Example::Plot::FourD into a file named t.pl , loaded it into a script file in the s.pl file.

 C:\Temp> cat t.pl package Example::Plot::FourD; use strict; use warnings; sub new { bless {} => shift } sub something { print "something\n" } "Example::Plot::FourD" C:\Temp> cat s.pl #!/usr/bin/perl use strict; use warnings; BEGIN { require 't.pl'; } my $p = Example::Plot::FourD->new; $p->something; C:\Temp> s something 

This example shows that the module files should not end with 1 , any true value will do.

+63
Aug 04 '10 at 12:41
source share

A.pl is the only script.

In .pm ( Perl Module ) you have functions that you can use from other Perl scripts:

A Perl module is a self-contained piece of Perl code that can be used by Perl or other Perl modules. It conceptually looks like a C-link library or a C ++ class.

-2
Aug 04 2018-10-10T00:
source share



All Articles