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. Advertising

  2. Vito Corleone wrote:
    > Sorry if this is newbie question.


    No need to apologize for asking a newbie question. :) (Besides, your
    question appears to me a little more than that.)

    > 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?


    It's at least nothing wrong with it. It's how it's done all the time.

    > Will it takes double memory (because Module1 is used twice)?


    No. If Module1 exports symbols by default, they will be imported to both
    package main and package Module2. That's all that happens.

    > 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)?


    It's merely a matter of taste.

    --
    Gunnar Hjalmarsson
    Email: http://www.gunnar.cc/cgi-bin/contact.pl
     
    Gunnar Hjalmarsson, Jan 10, 2005
    #2
    1. Advertising

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

    Shawn Corey Guest

    Vito Corleone wrote:
    > 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


    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. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. David Segall
    Replies:
    2
    Views:
    455
    Thomas Kellerer
    Jan 2, 2007
  2. abcd
    Replies:
    4
    Views:
    258
    Ben Finney
    Mar 20, 2007
  3. Replies:
    4
    Views:
    329
  4. edfialk
    Replies:
    0
    Views:
    205
    edfialk
    May 10, 2007
  5. ofir
    Replies:
    0
    Views:
    193
Loading...

Share This Page