Beefy Boxes and Bandwidth Generously Provided by pair Networks
Come for the quick hacks, stay for the epiphanies.
 
PerlMonks  

Using a module more than once

by kwaping (Priest)
on Aug 02, 2005 at 15:12 UTC ( [id://480222]=perlquestion: print w/replies, xml ) Need Help??

kwaping has asked for the wisdom of the Perl Monks concerning the following question:

Hi Monks,

I wrote a module for use in-house that uses methods from various CPAN modules. I don't want to rely on other developers who use my module to include the "use" statements for the required CPAN modules in their programs, so I put a "use" statement for each CPAN module inside my methods as needed.

Is this bad practice? Will it hurt anything? (I assume "no" to both questions, but I could be wrong.)

Also, will any problems (or benefits) arise from developers adding "use" statements for the same CPAN modules in their own programs?

I've tried a SuperSearch with no luck, just to head that off.

Replies are listed 'Best First'.
Re: Using a module more than once
by blazar (Canon) on Aug 02, 2005 at 15:28 UTC
    Is this bad practice? Will it hurt anything? (I assume "no" to both questions, but I could be wrong.)
    I don't think so, unless these modules do something really strange. In which case you would probably already know from their documentation. See e.g.:
    #! /usr/bin/perl -l use strict; use warnings; use lib sub { print pop; undef }; use File::Find; use File::Basename; use File::Find; __END__
    which gives me
    File/Find.pm warnings/register.pm Cwd.pm vars.pm XSLoader.pm File/Basename.pm re.pm File/Spec.pm File/Spec/Unix.pm Scalar/Util.pm List/Util.pm DynaLoader.pm AutoLoader.pm
        perldoc -f require will explain it better than I ever could. It was a big discovery for me too. Of course you can exploit this feature to do more {bizarre,uselful} things.
      Thanks! That's the kind of factual confirmation I was hoping to get.
Re: Using a module more than once
by holli (Abbot) on Aug 02, 2005 at 15:19 UTC
    so I put a "use" statement for each CPAN module inside my methods as needed.
    Why don't you just put them on the top of your modules code?


    holli, /regexed monk/
      I guess I could do that - thanks for the suggestion! I just wanted to load them situationally, as not all of them are needed for every method. Or am I misunderstanding how that mechanism works?
        Or am I misunderstanding how that mechanism works?
        You are. Any module that gets used, will be loaded at compile time, thus, when your module is loaded. So there's no use in putting a use statement inside a method.

        Sometimes, when some modules are only needed in exceptional circumstances, people can use a require statement in a method or sub, for example to load Carp in an error trapping routine.

        You have to be aware of the differences between use and require: require doesn't call import in the loaded modules, and it won't change the allowable syntax for your code either. Thus, subs defined in that module won't be easily recognized as such in the loading script. It's something to look out for.

        For OO modules, it makes no difference, whether you choose use or require.

        You're misunderstanding. use happens at compile time. Consider:
        #file: /perl/site/lib/Foo.pm package Foo; use strict; use warnings; sub bar { return "BAR"; } 1; #file: test.pl use strict; use warnings; my $i = 0; if ( $i ) { use Foo; } print Foo::bar; #prints "BAR"


        holli, /regexed monk/
Re: Using a module more than once
by jeteve (Pilgrim) on Aug 02, 2005 at 15:24 UTC
    Hi. I don't think putting several
    use Foo ;
    on your code will load many times the module in the memory. IMHO, once the module is loaded in memory by the first use, the following use has no effect. So if you need to explicitely use the module Foo in your module, just write use Foo; at the head. If other developpers use the same modules as you in their code, that will cause no problem at all. Hope it helps !
    Nice photos of naked perl sources here !
      Correct. Perl maintains a global %INC hash, listing all modules that have been required (use is just a special case of require). If a reference to the file already exists in %INC, it will not be loaded a second time.

      If you wanted to force Perl to reload the module, you could delete the reference to it in %INC (the key is the file name, which can be absolute, or relative to one of the directories in @INC). There aren't many cases where you'd want to do this, of course. (One legitimate case is Apache::Reload).
      IMHO, once the module is loaded in memory by the first use, the following use has no effect
      Not entirely true. Subseqent use's don't load the file again, but they still call the import method of the use'd class.

      Dave.

Re: Using a module more than once
by davidrw (Prior) on Aug 02, 2005 at 15:31 UTC
    Also, will any problems (or benefits) arise from developers adding "use" statements for the same CPAN modules in their own programs?

    I've tried a SuperSearch with no luck, just to head that off.
    No, it's fine -- they only get included once. perldoc -f use says that use Module LIST is exactly equivalent to BEGIN { require Module; import Module LIST; } and perldoc -f require says that 'the file will not be included twice under the same specified name'.
Re: Using a module more than once
by cowboy (Friar) on Aug 02, 2005 at 21:04 UTC

    I think everyone else has covered the use vs require.

    One benefit to them adding use statements is in an environment such as mod_perl or another environment where alot of processes are forked from a parent. In this case, if you load the modules in the parent process process, the memory can be shared among the children using the OS's copy-on-write abilities.

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://480222]
Approved by holli
Front-paged by GrandFather
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others chanting in the Monastery: (2)
As of 2024-04-26 01:27 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found