CGI/Perl Guide | Learning Center | Forums | Advertise | Login
Site Search: in

  Main Index MAIN
INDEX
Search Posts SEARCH
POSTS
Who's Online WHO'S
ONLINE
Log in LOG
IN

Home: Perl Programming Help: Intermediate:
How to know if a module is allready in use?

 



Rivotti
User

Feb 28, 2002, 11:02 AM

Post #1 of 6 (643 views)
How to know if a module is allready in use? Can't Post

Hi All,

Is it possible to know if a module was loaded and, if it was, don't load it again?
This is how I normaly do code:

#!/perl_path

use SomeModule;
$var = SomeModule->new();

sub function {
my $param = $_[0];
$var->param( DOWHAT => $param );
}

$cgi_param = param("cgi");

if ( $cgi_param eq "doit" ) {
&function("die");
&function("kill");
} elsif ( $cgi_param eq "dont" ) {
&function("forget");
.....
} else {
print "forgive";
}

You can see the problem here; if the cgi param isn't equal to "doit" or "dont" I dont need to load the module anyway. This was what I thought:

#!/perl_path

sub function {
use SomeModule;
$var = SomeModule->new() unless $var; #is this correct?
my $param = $_[0];
$var->param( DOWHAT => $param );
}

$cgi_param = param("cgi");

if ( $cgi_param eq "doit" ) {
&function("die");
&function("kill");
} elsif ( $cgi_param eq "dont" ) {
&function("forget");
.....
} else {
print "forgive";
}

This is ok but if i call &function more tha one time, will it start the module again? Doesn't this make the program slower?

Thanks in advance
(NICE NEW FORUM, KEEP THE GOOD WORK)
André Rivotti Casimiro
Web Designer/Programmer
MyGamesPlayer.com
http://www.mygamesplayer.com


mhx
Enthusiast / Moderator

Feb 28, 2002, 11:32 AM

Post #2 of 6 (640 views)
Re: [Rivotti] How to know if a module is allready in use? [In reply to] Can't Post


In Reply To
sub function {
use SomeModule;
$var = SomeModule->new() unless $var; #is this correct?
my $param = $_[0];
$var->param( DOWHAT => $param );
}


use won't work, since the module is loaded at compile time anyway. However, multiple use's won't load the module multiple times. You should have a look at the [url=]use manpage.

One of my scripts has a plugin interface that allows me to load Perl module plugins at runtime. The interesting section is as follows:

[perl]
sub UsePlugins
{
my @invalid_plugins = ();

foreach my $plug ( keys %plugin ) {
print STDERR "Loading plugin `$plug' ... ";

eval "require $plug";
if( $@ ) {
print STDERR "cannot load (require failed)\n$@";
push @invalid_plugins, $plug;
next;
}

my $PLUG = \%::;
$PLUG = $PLUG->{"${_}::"} for split /::/, $plug;

...
}
}
[/perl]

What I basically do is an eval'ed require. Normally, you don't need to eval it, but in case the plugin dies while I'm loading it, it won't crash my program and I just don't load it.

So, I guess [url=http://www.perldoc.com/perl5.6.1/pod/func/require.html]require could be what you're looking for.

-- mhx

At last with an effort he spoke, and wondered to hear his own words, as if some other will was using his small voice. "I will take the Ring," he said, "though I do not know the way."

-- Frodo



Paul
Enthusiast

Feb 28, 2002, 11:36 AM

Post #3 of 6 (637 views)
Re: [mhx] How to know if a module is allready in use? [In reply to] Can't Post

require will only reload the module if it has changed since it was compiled last.


mhx
Enthusiast / Moderator

Feb 28, 2002, 12:01 PM

Post #4 of 6 (634 views)
Re: [RedRum] How to know if a module is allready in use? [In reply to] Can't Post


In Reply To
require will only reload the module if it has changed since it was compiled last.


That's what we expect it to do, don't we? Wink

-- mhx

At last with an effort he spoke, and wondered to hear his own words, as if some other will was using his small voice. "I will take the Ring," he said, "though I do not know the way."

-- Frodo



Paul
Enthusiast

Feb 28, 2002, 12:04 PM

Post #5 of 6 (632 views)
Re: [mhx] How to know if a module is allready in use? [In reply to] Can't Post

I guess so.

Was just adding to what you said.


Rivotti
User

Mar 1, 2002, 4:28 AM

Post #6 of 6 (623 views)
Re: [mhx] How to know if a module is allready in use? [In reply to] Can't Post

Hummm... nice Smile! Anyway if a Module only loads once, with use, i can call it more than once. I'm soooo happpy Cool.....

Tanks m8's
André Rivotti Casimiro
Web Designer/Programmer
MyGamesPlayer.com
http://www.mygamesplayer.com

 
 


Search for (options) Powered by Gossamer Forum v.1.2.0

Web Applications & Managed Hosting Powered by Gossamer Threads
Visit our Mailing List Archives