logo
Free, unlimited AI code reviews that run on commit
git-lrc git-lrc GitHub Install Now We'd appreciate a star git-lrc - Free, unlimited AI code reviews that run on commit | Product Hunt git-lrc - Free, unlimited AI code reviews that run on commit | Product Hunt

Devel::Callsite - Get caller return OP address and Perl interpreter context

Authors

       Rocky Bernstein <rocky@cpan.org> (current maintainer) Ted Zlatanov <tzz@lifelogs.com> Ben Morrow ikegami

Description

callsite
           $callsite = callsite();
           $callsite = callsite($level);

       This function returns the the OP address of the caller, a number. It can take an optional integer
       specifying the number of levels back to get the OP address. If no parameter is given, a value of 0 is
       used which means to go up one level in the call chain. This behavior is like the built-in function
       "caller".

       This value is useful for functions that need to uniquely know where they were called, such as
       Every::every(); see Every. Or it can be used to pinpoint a location with finer granularity than a line
       number <http://www.perlmonks.com/?node_id=987268>. In conjunction with an OP tree disassembly you can
       know exactly where the caller is located in the Perl source.

       As of version 0.08, this function will return the expected call site for functions called via "DB::sub".
       (Previously it returned a call site inside the debugger.) If "callsite" is called from package "DB" in
       list context, it will return two numbers. The first is the ordinary return value; the second is the
       'true' call site of the function in question, which may be different if "DB::sub" is in use.

   addr_to_opFornowthisisonlyin5.026orgreater.

           $op = caller_nextop();
           $op = caller_nextop($level);

   caller_nextopFornowthisisonlyin5.026orgreater.

           $op = caller_nextop();
           $op = caller_nextop($level);

       This function returns the the "B::OP", not the address, of the next OP to get run after the call is made.
       It is equivalent to:

           addr_to_op(callsite($level));

   context
           $context = context()

       This function returns the interpreter context as a number. Using "callsite" alone to identify the call
       site is not reliable in programs which may include multiple Perl interpreters, such as when using
       ithreads. Combining "callsite" with "context" gives a unique location.

History

       Ben Morrow conceived this and posted it to perl5-porters. Ted Zlatanov then turned it into a CPAN module
       which he maintained for the first 3 revisions. Ben also added the level parameter to callsite.

       ikegami provided the function to turn the address into a real "B::OP".

       It is currently maintained (or not) by Rocky Bernstein.

Name

       Devel::Callsite - Get caller return OP address and Perl interpreter context

See Also

       B::Concise to disassemble the OP tree. Devel::Trepan optionally uses Devel::Callsite to show you exactly
       where you are stopped inside the debugger.

Synopsis

         use Devel::Callsite;
         my $site = sub { return callsite() };
         my $op_addr = $site->();
         printf "OP location: 0x%x\n", $op_addr;   # prints caller OP location
         printf "OP location: 0x%x\n", $site->(); # prints a different OP location

         sub foo { return callsite(1) };
         sub bar { foo() };
         # print this OP location even though it is 2 levels up the call chain.
         printf "OP location: 0x%x\n", bar();

         if ($] >= 5.025) {
           printf "OP is: %s\n", addr_to_op($addr);
           my $get_op = sub { return caller_nextop() };
           printf "OP is now: %s\n", $get_op->();
         }

         print context(), "\n"; # prints the interpreter context, an unsigned number

       Running the above gives:

         OP location: 0x5572e41f89f8
         OP location: 0x5572e421f5b0
         OP is: B::NULL=SCALAR(0x5572e41d0578)
         OP location: 0x5572e421f010
         OP is now: B::LISTOP=SCALAR(0x5572e41d0578)
         93951941730912

See Also