What is the difference between module and package?

Answers were Sorted based on User's Feedback



What is the difference between module and package?..

Answer / 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

What is the difference between module and package?..

Answer / kiruthikau

- A module is a .pm file containing some Perl code.
- A package is a declaration of the name of the current
scope, e.g.:
package Math::Complex
- Which can occur in the code of a module or a script.
- A module can be used, because it is a physical
'thing', but a package cannot.
- A module may contain several packages:

Is This Answer Correct ?    11 Yes 3 No

What is the difference between module and package?..

Answer / rameshravi

A package is a division of the global namespace; that means
you can have a global variable $foo and/or a sub named foo
in one package and a different global variable $foo and
different sub named foo in a different package.

A "module" is a file named according to the package it
contains, so module Foo::Bar would be in a file named
Foo/Bar.pm. There's no need for packages to be modules or
for a module to contain only that one package.

The term "module distribution" refers to a collection of one
or more modules that get built/installed together. Sometimes
the "distribution" part is left off and it's just called
"module", but it's really a different idea than a single module.

Is This Answer Correct ?    5 Yes 0 No

What is the difference between module and package?..

Answer / shankarreddy k

A module is basically a set of subroutines that are
designed to do a specific set of tasks, all with a common
goal/topic. You "use" modules in your Perl code and once
you do, their functions are available to you.

Now, say you have two modules that end up having two
functions with the same name, but do two completely
different things. You need to be able to tell them apart.
That is where the module, acts like a namespace.

For example, if modules "My::First::Module"
and "My::Second::Module" both have a function called "Count
()", but you need the version from "My::First::Module",
then you would specify that when you called the function,
like so:

My::First::Module::Count(options)
{
some code;
}

Ok, that is my take on this topic. Short and sweet.
Hopefully, if there is more, and I am sure there is, that
someone will enlighten not only you, but me as well.

Regards,
shankar

Is This Answer Correct ?    4 Yes 7 No

Post New Answer

More CGI Perl Interview Questions

How to get help for perl?

0 Answers  


Explain the meaning of closure in perl.

0 Answers  


How to start perl in interactive mode?

0 Answers  


What does `new $cur->{LINK}' do? (Assume the current package has no new() function of its own.)

0 Answers  


How to compare two strings in perl?

0 Answers  






What's the difference between /^Foo/s and /^Foo/?

0 Answers  


How do I replace every character in a file with a comma?

0 Answers  


How to add elements in a hash in perl?

0 Answers  


How do find the length of an array?

0 Answers  


Write syntax to use grep function?

0 Answers  


What are the various perl data types based on the context?

0 Answers  


What does read () command do?

0 Answers  


Categories