Where was a function defined?

  • Thread starter Daniel Pfeiffer
  • Start date
D

Daniel Pfeiffer

Hi,

with "caller" I can pinpoint where something comes from. Question is how can
I get at that same information _without_ calling the function?

Usage is in the "make" improvement makepp.sf.net, where a rule action can be a
self defined function as in

sub c_mycommand { ... } # c_ marks this as a command

targets: dependencies
&mycommand args

Now that function might be defined in the same makefile, in an include file,
or in a module which gets use`d. (In the first two cases it goes through an
eval with a #line nnn "file" directive so perl knows where it came from.)

Since makepp tries very hard to guarantee correct builds, it makes this
function an implicit dependency. But currently that dependency is the
makefile -- which is wrong if it came from include or use.

I hope there is an easier way than traversing %:: and hunting for a function
of the same name!

thanks in advance!
Daniel
 
B

Brian McCauley

Daniel said:
with "caller" I can pinpoint where something comes from. Question is how can
I get at that same information _without_ calling the function?

Well it's not really the same information but I think you mean
something like...

use B;
my $start_cop = B::svref_2object(\&foo)->START;
print $start_cop->file," ",$start_cop->line,"
",$start_cop->stashpv,"\n";
I hope there is an easier way than traversing %:: and hunting for a function
of the same name!

That wouldn't help anyhow. The "package" of a subroutine is perhaps a
rather ambiguous concept. Consider:

{
package XXX;
sub YYY::foo {1 };
}

*ZZZ::foo = \&YYY::foo;

my $coderef = \&ZZZ::foo;

What is the package of &{$coderef} ?

B::svref_2object($coderef)->START->stashpv will return XXX.
B::svref_2object($coderef)->GV->STASH->NAME will return YYY
There is AFIAK no way to get ZZZ from $coderef.
 
D

Daniel Pfeiffer

la 03.09.2006 16:37 Brian McCauley skribis:
Well it's not really the same information but I think you mean
something like...

use B;
my $start_cop = B::svref_2object(\&foo)->START;
print $start_cop->file," ",$start_cop->line,"
",$start_cop->stashpv,"\n";

The file method is just what I need here :) Alas it's not available in 5.6,
which we still support. Currently I built it in such as to be used only in
5.8, but in case you know how it used to work before, I'd be glad to hear!
That wouldn't help anyhow. The "package" of a subroutine is perhaps a
rather ambiguous concept.

Yes, you're right.

Thank you very much
Daniel
 

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,774
Messages
2,569,598
Members
45,158
Latest member
Vinay_Kumar Nevatia
Top