Loading module in another module

Discussion in 'Perl Misc' started by Vito Corleone, Jan 10, 2005.

  1. Hi,

    Sorry if this is newbie question. I am a bit confused about loading
    module in another module. Let's say I have a script that look like this.

    ## script.pl
    use strict;
    use Module1;
    use Module2;
    my $m1 = Module1::->new();
    print $m1->func1();
    my $m2 = Module2::->new(); ## Module2 is also using Module1
    print $m2->func2();

    ## Module2.pm
    use strict;
    use Module1;
    ....

    The problem is I "use Module1" 2 times, in script.pl and in Module2. I
    just wonder is it the right way to do it? Will it takes double memory
    (because Module1 is used twice)? Is it better if I only "use Module1" in
    script.pl and then pass the reference to Module2 (so I don't need to
    "use Module1" in Module2)?

    Please enlight me. Thank you in advance.

    --Vito
     
    Vito Corleone, Jan 10, 2005
    #1
    1. Advertisements

  2. No need to apologize for asking a newbie question. :) (Besides, your
    question appears to me a little more than that.)
    It's at least nothing wrong with it. It's how it's done all the time.
    No. If Module1 exports symbols by default, they will be imported to both
    package main and package Module2. That's all that happens.
    It's merely a matter of taste.
     
    Gunnar Hjalmarsson, Jan 10, 2005
    #2
    1. Advertisements

  3. Hi Gunnar,
    Thank your for your fast reply.
     
    Vito Corleone, Jan 10, 2005
    #3
  4. Vito Corleone

    Shawn Corey Guest

    Perl stores all modules it loads in the hash %INC. It uses this to
    determine if a module is already loaded and does not reload it. The
    second 'use Module1;' does not do anything since you did not change
    package; you are using modules as libraries, as oppose modules as
    packages (different name spaces) or modules as objects.

    --- Shawn
     
    Shawn Corey, Jan 10, 2005
    #4
    1. Advertisements

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments (here). After that, you can post your question and our members will help you out.