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
How many types of operators are used in the Perl?
Explain goto label, goto name, and goto expr?
What is the easiest way to download the contents of a URL with Perl?
Explain '->' in perl?
In Perl we can show the warnings using some options in order to reduce or avoid the errors. What are that options?
Explain join function in perl?
you are required to replace a char in a string and store the number of replacements. How would you do that?
What are the functions that can be performed using cgi program?
What are some common methods to all handles in perl?
What is the closure in PERL?
How to read a single line from a file in perl?
What is perl unshift array function?
You want to download the contents of a url with perl. How would you do that?
What is the difference between use and require in perl programming?
What rules must be followed by modules in perl.