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 disable the mod_perl from apache_server as i have used perlfect search on the site and its pagination is not working and the remedy is to disable the mod_perl.

1789


How to sort arrays in perl?

616


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

515


How to concatenate strings in perl?

588


Explain a tell function in perl?

535






What is a chomp() function in perl?

513


What is the use of now constructor in perl?

522


What do you mean by context of a subroutine?

560


Explain the difference between declarations of 'my' and 'local' variable scope in perl?

498


Explain the different types of data perl can handle.

490


What are scalars in perl?

534


What are the features of perl language?

589


Explain chomp, chop, cpan, tk.

530


Explain the meaning of subroutine?

512


What is perl scripting?

512