What is the difference between module and package?

Answer Posted / savitha sridhar

Modules and packages are usually used interchangeably. But
there is a difference.

a. Packages are perl files with .pm extn and is considered
a separate namespace. So a package is nothing but group of
related scalars,arrays,hashes and subroutines for a
specific purpose.Once a package is included in a .plx file
(using "use") and you want to call one of the subroutines
of the package, you may have to use the scope resolution
operator &package::subroutine1 ( as the subroutine of the
package is in a separate name space).

b. Modules are packages but which has the capabilities of
exporting selective subroutines/scalars/arrays/hashes of
the package to the namespace of the main package itself. So
for the interpreter these look as though the subroutines
are part of the main package itself and so there is no need
to use the scope resolution operator while calling them.

This is usually done like:

use Exporter;
our @ISA=('Exporter');
our @EXPORT=('$x','@arr',subroutine)

(you are exporting a scalar, array and a sub-routine from
the package). So if some .plx is using the above package
they need not use the scope resolution to call these.
A direct access like "print $x" would work even without
using the scope resolution.

Is This Answer Correct ?    22 Yes 8 No



Post New Answer       View All Answers


Please Help Members By Posting Answers For Below Questions

How to prevent file truncation in perl?

506


What are the arguements we normally use for perl interpreter?

515


You want to read command-line arguements with perl. How would you do that?

502


What are some of the key features of objects in perl?

519


Comment on array slicing and range operator

527






Explain perl. When do you use perl for programming?

451


How to add elements in a hash in perl?

517


What is perl scripting?

518


What does the q{ } operator do?

519


What is perl programming?

563


What $! In perl?

531


what is Polymorphism in Perl?

542


How to open a directory in perl?

548


What is the tk module?

588


What is the difference between perl array and perl hash?

524