Beefy Boxes and Bandwidth Generously Provided by pair Networks
No such thing as a small change
 
PerlMonks  

What's the right way to import a module

by sriharsha.sm (Initiate)
on Apr 26, 2012 at 07:11 UTC ( #967254=perlquestion: print w/ replies, xml ) Need Help??
sriharsha.sm has asked for the wisdom of the Perl Monks concerning the following question:

Is there any difference between the following two codes which is used to import the modules? If so which is the recommended practice?
use Foo; use Foo::Bar;
and
use Foo::Bar;

Comment on What's the right way to import a module
Select or Download Code
Re: What's the right way to import a module
by Anonymous Monk on Apr 26, 2012 at 07:14 UTC
Re: What's the right way to import a module
by moritz (Cardinal) on Apr 26, 2012 at 07:33 UTC
Re: What's the right way to import a module
by tobyink (Abbot) on Apr 26, 2012 at 07:56 UTC

    The difference is:

    • The first one uses the Foo module.
    • The second one does not use the Foo module.
    perl -E'sub Monkey::do{say$_,for@_,do{($monkey=[caller(0)]->[3])=~s{::}{ }and$monkey}}"Monkey say"->Monkey::do'
      Foo package has only two modules: Bar and Car.

      Directory structure

      - Foo |-Car.pm |-Bar.pm

      I intend to use only Foo::Bar module, but one of my colleague said its "best practice" to include 'use Foo' and then include 'use Foo::Bar'. Is this the right way of importing?

        No. If there is no Foo.pm, and you don't use any functionality of Foo.pm then use Foo; will not work and is not considered best practice.

        There is no general hierarchical relationship between Foo and Foo::Bar and Foo::Car. Maybe if you wrote the modules, there is some relationship between Foo::Bar and Foo::Car, but in general, this is not true.

        one of my colleague said its "best practice" to include 'use Foo' and then include 'use Foo::Bar'
        Either there's more to it than you're telling us (perhaps things are set up in your work environment that it does make sense), or it's time to replace your colleague with someone with a basic understanding of Perl.

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://967254]
Approved by Corion
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others exploiting the Monastery: (8)
As of 2014-08-01 08:04 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    My favorite superfluous repetitious redundant duplicative phrase is:









    Results (257 votes), past polls