Loading module in another module

V

Vito Corleone

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
 
G

Gunnar Hjalmarsson

Vito said:
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.
 
S

Shawn Corey

Vito said:
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
 

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. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,755
Messages
2,569,534
Members
45,008
Latest member
Rahul737

Latest Threads

Top