Beefy Boxes and Bandwidth Generously Provided by pair Networks
Keep It Simple, Stupid

Re: To module or not?

by tlm (Prior)
on Apr 26, 2005 at 14:06 UTC ( #451576=note: print w/ replies, xml ) Need Help??

in reply to To module or not?

Good question++. Your second pared-down version of the module just adds one more sub to the main package. Try adding the line

package MyModule;
at the top of the second version, and see what happens.

Your question begins to tease out three Perl facilities that are so often used together that it's easy to miss that they are distinct. One is the ability to define namespaces using the package keyword. The other is the ability to load source from a different file (e.g. with use). The third one is the ability to export/import names to/from one namespace to another (facilitated by Exporter).

the lowliest monk

Comment on Re: To module or not?
Download Code
Replies are listed 'Best First'.
Re^2: To module or not?
by splinky (Hermit) on Apr 26, 2005 at 14:14 UTC
    Just to comment a little further...

    The whole purpose of a module is to give you your own namespace, so you don't have to compete with the main program or other modules for subroutine names. So that's why you put "package MyModule;" at the top... to get your own namespace.

    The Exporter stuff allows users of your module to selectively import your subs into their namespace, so they don't have to type "MyModule::addit" if they don't want to.

      I wouldn't say that is the whole purpose of a module... the purpose of package is to give you a namespace, the purpose of modules is reusable code :)

                      - Ant
                      - Some of my best work - (1 2 3)

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://451576]
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others romping around the Monastery: (6)
As of 2016-04-29 17:34 GMT
Find Nodes?
    Voting Booth?
    :nehw tseb si esrever ni gnitirW

    Results (441 votes). Check out past polls.