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

by russ.jones2@boeing.com » Tue, 14 Feb 2006 22:25:49 GMT

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?

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

by Jeremy Nixon » Wed, 15 Feb 2006 13:15:24 GMT



[...]

This should work:

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

--
Jeremy | XXXX@XXXXX.COM

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

by attn.steven.kuo@gmail.com » Wed, 15 Feb 2006 13:28:04 GMT


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 $@;
}




--
Hope this helps,
Steven

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

by Gunnar Hjalmarsson » Wed, 15 Feb 2006 15:37:55 GMT


Why would main::carp have anything to do with CGI::Carp? They are
different packages, and case matters in most cases.


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.

--
Gunnar Hjalmarsson
Email: http://www.gunnar.cc/cgi-bin/contact.pl

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

by Christian Winter » Thu, 16 Feb 2006 00:26:56 GMT


Yes, you can walk the symbol table of the main program, that is
the %main:: hash. If CGI.pm is loaded by the program, a hash entry
$main::{"CGI::"} exists (note the "::" after the package names).


In this case, you would also have to look if $main::{"Carp"} exists
as well.

Or just use the can() function from the UNIVERSAL base class.
print "Carp already defined! Will use main::carp().$/"
if( main->can("carp") );

So your module could be something like
------------------------------------------------
package mycarper;

sub mycarp {
if( main->can("carp") ) {
main::carp(@_);
}
print STDERR "I am carping myself! @_$/";
}
------------------------------------------------

HTH
-Chris

(FollowUp2 set to c.l.p.moderated)

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

by John W. Krahn » Thu, 16 Feb 2006 06:45:36 GMT


That would be better written as:

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



John
--
use Perl;
program
fulfillment

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

by attn.steven.kuo » Fri, 17 Feb 2006 04:45:16 GMT


Thanks for the reminder, John.

I can never remember whether the
keys of %INC contain the full path
to modules or not; nor do I remember
whether the name space delimiters
were '::' or '/', nor whether the '.pm'
file extension is added.

This leads me to test things with a
one-liner, like:

$ perl -MCGI::Carp -le 'print grep /Carp/, keys %INC;'

And from there, it's just a careless thought to
copy/paste/modify the 'grep' statement into
actual code.

*Sigh*

--
Regards,
Steven

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

by Yitzchak Scott-Thoennes » Fri, 17 Feb 2006 18:20:03 GMT


If you only wanted to switch between CGI::Carp and Carp, you could:

use Carp;
BEGIN { *carp = CGI::Carp->can("carp") || \&Carp::carp }

I don't think checking what main:: has is a good idea; it could be
directly calling CGI::Carp::carp without importing it.

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

by Charles DeRykus » Tue, 21 Feb 2006 13:07:55 GMT


If I undestand the OP, code in main such as 'use MyModule; use
CGI::Carp' would forestall re-use anyway since CGI::Carp::carp
wouldn't be seen inside MyModule.

--
Charles DeRykus

Similar Threads

1. How to write a "caller-inspecting" module? - Perl

2. More controlled Module loading - Faking output from caller

Hi

I'm trying to write my own function that works like use but outputs a
more user friendly error message if the module isn't present.

My current tactic is to have my function in it's own package, and it
uses File::Package to do the loading - however this doesn't import
things into the correct namespace (neither does calling import()
directly for some packages that have redefined it).

Is there any way I can change what caller() returns, so my function
does not appear and everything behaves as if it was called from the
namespace that called my function?

Alternatively, are there any other solutions?

Stuart Moore

3. ANNOUNCE: new Perl module to interface with Ingres utility netutil (and others: iinamu, iimonitor)

4. Best practice to patch a existing module written by others

Hi all,

I would like to ask what is the "official"/"best practice" to write
patch on a existing module which is original written by others?

The module I planned to patch is "Math::Amoeba".

Thanks in advance.


Tom

5. How to find Modules used by a Given Module - Perl

6. How do I tell if a module exists?

How can I tell whether or not a module exists, and what version it is? 
i.e. do something like if (defined CGI.pm).  I want to make sure all my
users are running off of the same modules.

Thanks in advance,

Dan

7. how to tell perl to look for where a new module is installed - Perl

8. How to tell what modules are installed?

Our SysAdmins have created a Solaris package of perl5.8.0 and
installed a number
of packages from CPAN. What's the best way to get a list of all the
installed modules?

Thanks,

jim