Callsite(3pm)
Callsite(3pm) is here, bestie β and before you ask, yes, it's one of those User Contributed Perl Documentation pages. It's short, it's spicy, it's giving "I do one weird thing and I do it extremely well." Let's get into it π
β¨ NAME β¨
Devel::Callsite β get the caller's return OP address, plus the Perl interpreter context. That's the whole gig. That's the brand. π
π«Ά SYNOPSIS π«Ά
Okay so here's the vibe in code, no cap, copy it exactly:
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
And if you actually run that, this is what comes out πΈ:
OP location: 0x5572e41f89f8
OP location: 0x5572e421f5b0
OP is: B::NULL=SCALAR(0x5572e41d0578)
OP location: 0x5572e421f010
OP is now: B::LISTOP=SCALAR(0x5572e41d0578)
93951941730912
π DESCRIPTION π
π callsite π
$callsite = callsite();
$callsite = callsite($level);
This function hands you back the OP address of the caller, as a number. It can take an optional integer that says how many levels back to go grab that OP address from. If you don't pass a parameter, a value of 0 is used, which means go up one level in the call chain. This behavior is exactly like the built-in function caller.
So why do you care? Because this value is so useful for functions that need to uniquely know where they were called from β think Every::every();, go look at Every. Or you can use it to pinpoint a location with finer granularity than a line number (http://www.perlmonks.com/?node_id=987268). And if you pair it with an OP tree disassembly, you can know exactly where the caller is located in the Perl source. Iconic behavior, honestly π
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 β awkward.) And 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. Two numbers, two vibes. π¦
π addr_to_op π
For now this is only in 5.026 or greater.
$op = caller_nextop();
$op = caller_nextop($level);
Not me pointing out that this is literally what the page says under this heading, snippet and all β I'm keeping it exactly as written, bestie, I do not edit the source material π
π caller_nextop π
For now this is only in 5.026 or greater.
$op = caller_nextop();
$op = caller_nextop($level);
This one returns 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. Here's the tea: using callsite alone to identify the call site is not reliable in programs which may include multiple Perl interpreters, like when you're using ithreads. So you combine callsite with context and boom β unique location. Slay. π₯
π§ HISTORY π§
Ben Morrow conceived this whole thing 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. π the "(or not)" is sending me
π SEE ALSO π
B::Conciseβ to disassemble the OP tree.Devel::Trepanβ optionally usesDevel::Callsiteto show you exactly where you are stopped inside the debugger.
π AUTHORS π
- Rocky Bernstein
rocky@cpan.org(current maintainer) - Ted Zlatanov
tzz@lifelogs.com - Ben Morrow
- ikegami
π LICENSE AND COPYRIGHT π
Copyright (C) 2013, 2018 Rocky Bernstein rocky@cpan.org, Ted Zlatanov, tzz@lifelogs.com, Ben Morrow
This program is distributed WITHOUT ANY WARRANTY, including but not limited to the implied warranties of merchantability or fitness for a particular purpose. Yes bestie, zero warranty, you are on your own, godspeed π«Ά
The program is free software. You may distribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation (either version 2 or any later version) and the Perl Artistic License as published by O'Reilly Media, Inc. Please open the files named gpl-2.0.txt and Artistic for a copy of these licenses. π
And that's the whole page, periodt. This one ships as perl v5.42.3, dated 2026-08-19, and the page tag is Callsite(3pm) β go forth and know exactly where you were called from, main character π
β¨
π¦ where this page comes from
- ubuntu stonking: libdevel-callsite-perl 1.0.1-2build6 Β· Artistic-1.0-Perl OR GPL-2.0-or-later
- π girlified by deepseek-ai/DeepSeek-V4.1-Flash on : a language model's rewrite of the original, under its terms