how can a module tell if its caller is using some other module

R

russ.jones2

Is there a way of telling if a module is already "used?" I'm working on
a module that might be called by a CGI script, or by a non-CGI script.

I'd like to be able to use 'croak," etc. instead of die, but if it's
already used by some module or program that might be using my module, I
want to use the one that's already defined.

For example, if the main program says

use CGI::Carp;

then I would want to call "main::carp"

I'd also "use Carp" in my module, and if I couldn't find any other
"carp" to use, I'd call "mymodule::carp"

So does anyone know how to tell if there's already a "carp" available?
 
J

Jeremy Nixon

[...]
I'd also "use Carp" in my module, and if I couldn't find any other
"carp" to use, I'd call "mymodule::carp"

So does anyone know how to tell if there's already a "carp" available?

This should work:

my $have_carp = defined(*main::carp{CODE}) ? 1 : 0;
 
A

attn.steven.kuo

Is there a way of telling if a module is already "used?" I'm working on
a module that might be called by a CGI script, or by a non-CGI script.

I'd like to be able to use 'croak," etc. instead of die, but if it's
already used by some module or program that might be using my module, I
want to use the one that's already defined.

For example, if the main program says

use CGI::Carp;

then I would want to call "main::carp"

I'd also "use Carp" in my module, and if I couldn't find any other
"carp" to use, I'd call "mymodule::carp"

So does anyone know how to tell if there's already a "carp" available?


You can check %INC to see if a particular module
is used; you can check to see if "carp" is
a defined subroutine; you can check to see if
a "carp" method is available (if you're writing
object-oriented code):

if (grep $_ eq 'CGI/Carp.pm', keys %INC)
{
carp( "CGI::Carp alreay in use" );
}
elsif (defined &carp)
{
carp( "This package has a defined carp subroutine" )
}
elsif (__PACKAGE__->can('carp'))
{
__PACKAGE__->carp( "This package can carp ... somehow" );
# see perldoc UNIVERSAL
}
else
{
eval "require MyOwn::Carp";
warn $@ if $@;
}
 
G

Gunnar Hjalmarsson

Is there a way of telling if a module is already "used?" I'm working on
a module that might be called by a CGI script, or by a non-CGI script.

I'd like to be able to use 'croak," etc. instead of die, but if it's
already used by some module or program that might be using my module, I
want to use the one that's already defined.

For example, if the main program says

use CGI::Carp;

then I would want to call "main::carp"

Why would main::carp have anything to do with CGI::Carp? They are
different packages, and case matters in most cases.
I'd also "use Carp" in my module, and if I couldn't find any other
"carp" to use, I'd call "mymodule::carp"

CGI::Carp requires Carp, which is a standard module... Leaving that
aside, do you possibly want to do something like:

eval "use Carp; 1" or require mymodule::carp;

I'd advise you to study "perldoc perlmod" more carefully.
 
J

John W. Krahn

You can check %INC to see if a particular module
is used; you can check to see if "carp" is
a defined subroutine; you can check to see if
a "carp" method is available (if you're writing
object-oriented code):

if (grep $_ eq 'CGI/Carp.pm', keys %INC)

That would be better written as:

if ( exists $INC{ 'CGI/Carp.pm' } )



John
 

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,013
Latest member
KatriceSwa

Latest Threads

Top