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

This is a discussion on How to write a "caller-inspecting" module? - Perl ; 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 ...

+ Reply to Thread
Results 1 to 3 of 3

How to write a "caller-inspecting" module?

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




    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
    --
    NOTE: In my address everything before the first period is backwards;
    and the last period, and everything after it, should be discarded.

  2. Default Re: How to write a "caller-inspecting" module?

    On Dec 13, 5:31 pm, kj <so...@987jk.com.invalid> wrote:
    > 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.

    --
    Charles DeRykus

  3. Default Re: How to write a "caller-inspecting" module?

    In article <fjsmd4$3do$1@reader1.panix.com>, kj
    <socyl@987jk.com.invalid> wrote:

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

+ Reply to Thread

Similar Threads

  1. Replies: 1
    Last Post: 09-19-2007, 09:45 PM
  2. Replies: 9
    Last Post: 09-26-2006, 06:01 AM
  3. "diameter", "has_a_cycle" problems in Graph-0.76 module
    By Application Development in forum Perl
    Replies: 1
    Last Post: 07-12-2006, 03:58 AM
  4. """""""""""""""""""""Visual C++ 2005 Express"""""""""""""""""
    By Application Development in forum DOTNET
    Replies: 0
    Last Post: 03-12-2006, 03:55 AM
  5. Replies: 0
    Last Post: 12-15-2004, 08:35 AM