Where was a function defined?

Do you have a question? Post it now! No Registration Necessary.  Now with pictures!

Threaded View

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!

Re: Where was a function defined?

Daniel Pfeiffer wrote:

Quoted text here. Click to load it

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,"

Quoted text here. Click to load it

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 & ?

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.

Re: Where was a function defined?

la 03.09.2006 16:37 Brian McCauley skribis:
Quoted text here. Click to load it

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!

Quoted text here. Click to load it

Yes, you're right.

Thank you very much

Site Timeline