Finding if a module is already 'require'd

2002-09-30 Thread Ramprasad A Padmanabhan



How do I find in a function if a particular module is already loaded

for eg,

sub mysub {
my @vars = @_;
require Data::Dumper unless ( already_required('Data::Dumper'));
print Data::Dumper::Dumper(\@vars);
}

I want help writing the function already_required()


-- 
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]




Re: Finding if a module is already 'require'd

2002-09-30 Thread Janek Schleicher

Ramprasad A Padmanabhan wrote at Mon, 30 Sep 2002 09:36:50 +0200:

 How do I find in a function if a particular module is already loaded
 
 for eg,
 
 sub mysub {
   my @vars = @_;
   require Data::Dumper unless ( already_required('Data::Dumper'));
   print Data::Dumper::Dumper(\@vars);
 }
 
 I want help writing the function already_required()

You don't need it.
From perldoc -f require:

   Otherwise, demands that a library file be included
   if it hasn't already been included.  The file is
   included via the do-FILE mechanism, which is
   essentially just a variety of eval.  Has
   semantics similar to the following subroutine:

So a required module is only loaded when it hadn't been loaded yet.


Greetings,
Janek

-- 
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]




RE: Finding if a module is already 'require'd

2002-09-30 Thread NYIMI Jose (BMB)

 -Original Message-
 From: Janek Schleicher [mailto:[EMAIL PROTECTED]] 
 Sent: Monday, September 30, 2002 10:05 AM
 To: [EMAIL PROTECTED]
 Subject: Re: Finding if a module is already 'require'd
 
 
 Ramprasad A Padmanabhan wrote at Mon, 30 Sep 2002 09:36:50 +0200:
 
  How do I find in a function if a particular module is already loaded
  
  for eg,
  
  sub mysub {
  my @vars = @_;
  require Data::Dumper unless ( already_required('Data::Dumper'));
  print Data::Dumper::Dumper(\@vars);
  }
  
  I want help writing the function already_required()
 
 You don't need it.
 From perldoc -f require:
 
Otherwise, demands that a library file be included
if it hasn't already been included.  The file is
included via the do-FILE mechanism, which is
essentially just a variety of eval.  Has
semantics similar to the following subroutine:
 
 So a required module is only loaded when it hadn't been loaded yet.

As from which version of Perl this functionality has been included.
I'm asking this because, I have seen in the DBI code (DBI.pm file)
That the author is checking if a DBD::* module has been already installed (loaded) ...

Here an extract of DBI.pm file :


sub install_driver {# croaks on failure
my $class = shift;
my($driver, $attr) = @_;
my $drh;

$driver ||= $ENV{DBI_DRIVER} || '';

# allow driver to be specified as a 'dbi:driver:' string
$driver = $1 if $driver =~ s/^DBI:(.*?)://i;

Carp::croak(usage: $class-install_driver(\$driver [, \%attr]))
unless ($driver and @_=3);

# already installed
return $drh if $drh = $DBI::installed_drh{$driver};

[snip]


José.


 DISCLAIMER 

This e-mail and any attachment thereto may contain information which is confidential 
and/or protected by intellectual property rights and are intended for the sole use of 
the recipient(s) named above. 
Any use of the information contained herein (including, but not limited to, total or 
partial reproduction, communication or distribution in any form) by other persons than 
the designated recipient(s) is prohibited. 
If you have received this e-mail in error, please notify the sender either by 
telephone or by e-mail and delete the material from any computer.

Thank you for your cooperation.

For further information about Proximus mobile phone services please see our website at 
http://www.proximus.be or refer to any Proximus agent.


--
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]




Re: Finding if a module is already 'require'd

2002-09-30 Thread Jenda Krynicky

From:   Ramprasad A Padmanabhan [EMAIL PROTECTED]
 How do I find in a function if a particular module is already loaded

Look at the %INC hash.

Jenda
=== [EMAIL PROTECTED] == http://Jenda.Krynicky.cz ==
There is a reason for living. There must be. I've seen it somewhere.
It's just that in the mess on my table ... and in my brain
I can't find it.
--- me


-- 
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]




Re: Finding if a module is already 'require'd

2002-09-30 Thread david

Ramprasad A Padmanabhan wrote:

 
 
 How do I find in a function if a particular module is already loaded
 
 for eg,
 
 sub mysub {
 my @vars = @_;
 require Data::Dumper unless ( already_required('Data::Dumper'));
 print Data::Dumper::Dumper(\@vars);
 }
 
 I want help writing the function already_required()

if you use the compile time 'use Data::Dumper' then you don't have to worry 
abut this since Perl will complain and won't compile if the module is 
missing. if you try to do it in run time, here is one way:

sub already_required{

my $module = shift;

eval{
require $module;
};

if($@){
#-- something is wrong. $module could be missing
return 0;
}else{
#-- looks like $module is successfully loaded!
return 1;
}
}

david

-- 
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]