Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Perl > Perl Misc > Loading module in another module

Reply
Thread Tools

Loading module in another module

 
 
Vito Corleone
Guest
Posts: n/a
 
      01-10-2005
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
 
Reply With Quote
 
 
 
 
Gunnar Hjalmarsson
Guest
Posts: n/a
 
      01-10-2005
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
 
Reply With Quote
 
 
 
 
Vito Corleone
Guest
Posts: n/a
 
      01-10-2005
Hi Gunnar,
Thank your for your fast reply.
 
Reply With Quote
 
Shawn Corey
Guest
Posts: n/a
 
      01-10-2005
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
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
order of iframe loading with document loading ofir Javascript 0 12-03-2007 12:06 PM
loading image -> detect when image is done loading edfialk Javascript 0 05-10-2007 07:28 PM
help - Module needs access to another module abcd Python 4 03-20-2007 10:10 PM
[OT] Is loading the second Java application faster than loading the first? David Segall Java 2 01-02-2007 04:41 PM
Image loading using javascript. Handling timeouts and parrallel loading under IE zborisau@gmail.com Javascript 4 08-28-2005 02:02 PM



Advertisments
 



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57