How to write a "caller-inspecting" module?

K

kj

Is it possible to write a module Foo such that if one wrote

package Bar;
use Foo;

# rest of Bar defined here

sub last_sub_in_Bar { do_something() }

last_top_level_statement_in_Bar();

# suitable moment!

1; # end of Bar

then, at some *suitable moment* Foo would automatically inspect
the Bar package (the "caller") and do something (e.g. add some
methods to Bar) on the basis of this inspection. The "suitable
moment" I'm referring to is right after last_sub_in_Bar has been
defined and "last_top_level_statement_in_Bar();" has been executed.

After playing around endlessly with CHECKs and INITs, I still have
not come up with a solution to this problem. The closest to a
solution I can come up with is to move the "use Foo" statement to
right after last_top_level_statement_in_Bar().

Is what I'm trying to do possible?

TIA!

kj
 
C

comp.llang.perl.moderated

Is it possible to write a module Foo such that if one wrote

package Bar;
use Foo;

# rest of Bar defined here

sub last_sub_in_Bar { do_something() }

last_top_level_statement_in_Bar();

# suitable moment!

1; # end of Bar

then, at some *suitable moment* Foo would automatically inspect
the Bar package (the "caller") and do something (e.g. add some
methods to Bar) on the basis of this inspection. The "suitable
moment" I'm referring to is right after last_sub_in_Bar has been
defined and "last_top_level_statement_in_Bar();" has been executed.

After playing around endlessly with CHECKs and INITs, I still have
not come up with a solution to this problem. The closest to a
solution I can come up with is to move the "use Foo" statement to
right after last_top_level_statement_in_Bar().

Is what I'm trying to do possible?

Carp's 'longmess' provides some stack introspection, eg,

package Foo;
use Carp;
my $trace = Carp::longmess;
...

$trace might then contain, eg,
at Bar.pm line 2
Bar::BEGIN() called at Foo.pm line 0
eval {...} called at Foo.pm line 0
require Bar.pm called at -e line 1
main::BEGIN() called at Foo.pm line 0
eval {...} called at Foo.pm line 0

You'd then have to pry 'Bar' from this 'mess'.
Kludgy and fragile though...so there may well
be a better way.
 
B

brian d foy

kj said:
Is it possible to write a module Foo such that if one wrote

package Bar;
use Foo;

# rest of Bar defined here

sub last_sub_in_Bar { do_something() }

last_top_level_statement_in_Bar();

# suitable moment!

1; # end of Bar

then, at some *suitable moment* Foo would automatically inspect
the Bar package (the "caller")

Well, you can always use the caller() built-in to inspect the call
stack and decide what to do. You might also want to look at things such
as Hook::LexWrap for ideas.

If you need more help, give a concrete example about what you are doing
and what you are trying to accomplish.

Good luck,
 

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

Forum statistics

Threads
474,208
Messages
2,571,080
Members
47,682
Latest member
TrudiConna

Latest Threads

Top