Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Perl > Perl Misc > using multiple "use Module" statements

Reply
Thread Tools

using multiple "use Module" statements

 
 
dszostek@gmail.com
Guest
Posts: n/a
 
      10-01-2005
Hi,

If I have "use Module;" in a subroutine that may be called more than
once, does it re-load anything from the module? Is there any
performance issue with doing this or does it just ignore the statement
if the module has already been loaded.

Thanks,
Dave

 
Reply With Quote
 
 
 
 
Sherm Pendley
Guest
Posts: n/a
 
      10-01-2005
writes:

> If I have "use Module;" in a subroutine that may be called more than
> once, does it re-load anything from the module?


It's only loaded once, for two reasons:

First, "use Module" is executed at *compile* time, and the subroutine is
compiled only once no matter how many times you call it at run time.

And second, when a module is included via do, use, or require, Perl checks
first for an entry for that module in %INC. If an entry is found, nothing
happens. If none is found, the module is loaded and an entry is created.

Have a look at:

perldoc -f use
perldoc -f do
perldoc -f require
perldoc perlvar # Look for %INC

sherm--

--
Cocoa programming in Perl: http://camelbones.sourceforge.net
Hire me! My resume: http://www.dot-app.org
 
Reply With Quote
 
 
 
 
Bob Walton
Guest
Posts: n/a
 
      10-01-2005
wrote:
....
> If I have "use Module;" in a subroutine that may be called more than
> once, does it re-load anything from the module? Is there any
> performance issue with doing this or does it just ignore the statement
> if the module has already been loaded.

....
> Dave
>

Per
perldoc -f use
one sees that require() is the underlying implementation of
use(). Then from
perldoc -f require
one sees that require() "demands that a library file be included
if it hasn't already been included.". So the module is loaded
only once. %INC keeps track of this. Note that the module's
->import() method will be called for each use() call even if the
module has already been loaded.

--
Bob Walton
Email: http://bwalton.com/cgi-bin/emailbob.pl
 
Reply With Quote
 
Sherm Pendley
Guest
Posts: n/a
 
      10-01-2005
Bob Walton <> writes:

> wrote:
> ...
>> If I have "use Module;" in a subroutine that may be called more than
>> once, does it re-load anything from the module? Is there any
>> performance issue with doing this or does it just ignore the statement
>> if the module has already been loaded.

> ...
>> Dave
>>

> Per
> perldoc -f use
> one sees that require() is the underlying implementation of use().


One also sees that use() is executed at compile time.

> %INC keeps track of this. Note that the module's ->import() method
> will be called for each use() call even if the module has already been
> loaded.


That will still only be once, because a subroutine is compiled only once
no matter how many times it might be called. That includes zero times -
a subroutine that's never called is still compiled.

sherm--

--
Cocoa programming in Perl: http://camelbones.sourceforge.net
Hire me! My resume: http://www.dot-app.org
 
Reply With Quote
 
Bob Walton
Guest
Posts: n/a
 
      10-01-2005
Sherm Pendley wrote:

> Bob Walton <> writes:

....
>> wrote:
>>

....
>>>Dave

....
>>%INC keeps track of this. Note that the module's ->import() method
>>will be called for each use() call even if the module has already been
>>loaded.

>
>
> That will still only be once, because a subroutine is compiled only once
> no matter how many times it might be called. That includes zero times -
> a subroutine that's never called is still compiled.


Counterexample:

#File junk552.pm:
package junk552;
sub import{
print "import called:@_\n";
}
1;

#File junk552.pl:
use junk552(a);
use junk552(b);

#Results of run:
d:\junk>perl junk552.pl
import called:junk552 a
import called:junk552 b

d:\junk>

>
> sherm--


--
Bob Walton
Email: http://bwalton.com/cgi-bin/emailbob.pl
 
Reply With Quote
 
Sherm Pendley
Guest
Posts: n/a
 
      10-01-2005
Bob Walton <> writes:

> Sherm Pendley wrote:
>
>> That will still only be once, because a subroutine is compiled only
>> once
>> no matter how many times it might be called. That includes zero times -
>> a subroutine that's never called is still compiled.

>
> Counterexample:
>
> #File junk552.pm:
> package junk552;
> sub import{
> print "import called:@_\n";
> }
> 1;
>
> #File junk552.pl:
> use junk552(a);
> use junk552(b);
>
> #Results of run:
> d:\junk>perl junk552.pl
> import called:junk552 a
> import called:junk552 b


I thought I'd left enough quotage to make the context clear, but I may have
oversnipped - sorry 'bout that.

Anyhow, we were discussing a single use() statement appearing in a subroutine
that's called many times. For instance:

sub foo {
use junk552(a);
}

The "use junk552(a);" is evaluated at compile-time, so it's called only once
no matter how many times you call foo() at run-time.

Your counter example doesn't negate that point, it simply illustrates something
entirely different - although a given module is only loaded and compiled once,
its import() subroutine may be called any number of times. If you really think
about it, it's obvious why this happens - how else could a package's symbols be
exported into more than one name space?

But as your example shows, that happens as a result of multiple use() state-
ments. It would not happen as a result of additional invocations of foo() in
the example above.

If you want to have a use() that's evaluated at run-time, you have to wrap it
in a string eval().

sub foo {
eval 'use junk552(a)';
}

One common use of this is to make the use of a module optional by checking for
its existence at run-time:

our $junk_exists;

sub check_junk {
$junk_exists = eval 'use junk552';
}

# ...later...

if ($junk_exists) {
my $foo = new junk552();
}

sherm--

--
Cocoa programming in Perl: http://camelbones.sourceforge.net
Hire me! My resume: http://www.dot-app.org
 
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
if statements and case statements questions John Crichton Ruby 6 07-12-2010 06:17 PM
Prepare Statements VS Statements Vince Java 12 01-21-2008 01:18 PM
component statements within architecture statements Neil Zanella VHDL 8 10-20-2006 09:05 AM
Multiple Select Statements in resultset Joe via DotNetMonster.com ASP .Net 3 03-27-2005 12:43 AM
if statements with or w/o else statements Harry George Python 6 02-23-2004 06:48 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