Scripts & Modules

B

Bigus

Hello

I would just like some clarification on "use"ing modules. Say we have the
following files on our system:

script1.cgi
module1.pm [contains sub - mod1function1()]
module2.pm [contains subs - mod2function1() and mod2function2()]

sciprt1.cgi has "use module1;" in it because it needs to call
mod1function1().

However, mod1function1() needs to call mod2function1(), so "use module2;" is
placed inside mod1function1().

At a later date script1.cgi needs to use mod2function2(). in addition to
mod1function1(), so "use module2;" is placed directly into script1.cgi.

So we now have a scenario where script1.cgi is loading both modules but when
mod1function1() is called it loads module1.pm again (or does it?)

That is a simple case but in reality our system has a dozen modules and
scores of scripts so it can be difficult keeping track of what modules are
loaded at any one time.

Is there a "best practise" in this kind of situation, when it comes to
loading modules?

Regards
Bigus
 
J

Jochen Lehmeier

Is there a "best practise" in this kind of situation, when it comes to
loading modules?

http://www.google.de/search?q=perldoc+use

"The ... makes sure the module is loaded into memory if it hasn't been
yet."

So, don't worry, just use "use" as much as you want to.

And, the usual way is to put the "use" statements at the top of the file,
not inside a function. Not that it matters, it just makes it easier to
read.
 
B

Bigus

Jochen Lehmeier said:
http://www.google.de/search?q=perldoc+use

"The ... makes sure the module is loaded into memory if it hasn't been
yet."

So, don't worry, just use "use" as much as you want to.

And, the usual way is to put the "use" statements at the top of the file,
not inside a function. Not that it matters, it just makes it easier to
read.

Blimey, yes it's clicked now.. that perldoc doesn't make it all that clear
to me.

However, I created a test script with the lines:

use MyModule;
print time2str("%d %m %Y",time);

Then created MyModule.pm with these lines:

sub func1 {
use Date::Format;
}
1;

and ran the test script and it worked! So, basically modules loaded via
"use" have a kind of a global scope :)

Thanks
Bigus
 
J

Jürgen Exner

Bigus said:
sub func1 {
use Date::Format;

I consider this to be poor style.

'use' is executed at compile time while placing the 'use' statement
inside of a function erroniously indicates that the 'use' would be
executed at runtime of this function.

Why do you want to confuse people like that?

jue
 
X

Xho Jingleheimerschmidt

Jochen said:
http://www.google.de/search?q=perldoc+use

"The ... makes sure the module is loaded into memory if it hasn't been
yet."

So, don't worry, just use "use" as much as you want to.

And, the usual way is to put the "use" statements at the top of the
file, not inside a function. Not that it matters, it just makes it
easier to read.

If I am fairly confident that nothing in the main script other than this
specific function is ever going to want that module, then I put the
"use" of that module inside that function. It keeps the dependency
together.

Xho
 
X

Xho Jingleheimerschmidt

Bigus wrote:
....
However, mod1function1() needs to call mod2function1(), so "use module2;" is
placed inside mod1function1().

At a later date script1.cgi needs to use mod2function2(). in addition to
mod1function1(), so "use module2;" is placed directly into script1.cgi.

So we now have a scenario where script1.cgi is loading both modules but when
mod1function1() is called it loads module1.pm again (or does it?)

No, for two reasons. One is that the "use" inside the function happens
when the function is compiled, not when it is called.

And second, the module is not loaded twice because perl keeps track of
such things. It's "import" subroutine, if any, is executed twice, however.


Xho
 
S

sreservoir

Bigus said:
Hello

I would just like some clarification on "use"ing modules. Say we have the
following files on our system:

script1.cgi
module1.pm [contains sub - mod1function1()]
module2.pm [contains subs - mod2function1() and mod2function2()]

if this is actually happening, you should rewrite the thing so that it
has module1::function1, module2::function1, module2::function2. module
code being compiled in main:: is usually considered bad style.
 
B

Bigus

Tad McClellan said:
[ "use"ing a module multiple times ]
that perldoc doesn't make it all that clear
to me.


See also the description for %INC in

perldoc perlvar

which is how perl knows whether or not it has already loaded a module.

Ahh, thanks - a list of all the modules loaded at any one point :)

Regards
Bigus
 
X

Xho Jingleheimerschmidt

Ben said:
No, since the file is (normally) only compiled once. If you 'do' a file
that 'use's a module twice, import will get called twice ('do' updates
%INC but doesn't check it); similarly if you eval a 'use' twice.

I'm not sure what you are trying to correct here, but if you "use" a
module from more than one place, which is what the OP was talking about,
its import sub certainly does get called more than once.

Xho
 

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

No members online now.

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,009
Latest member
GidgetGamb

Latest Threads

Top