++ed by:
Author image Mark Nunberg
and 1 contributors


Devel::PtrTable - Interface to perl's old-new pointer mapping for cloning


This provides an interface to map memory addresses of duplicated/cloned perl variables between threads.

Perl duplicates each variable when a new thread is spawned, and thus the new underlying SV object has a new memory address.

Internally, perl maintains a struct ptr_tbl during cloning, which allows the interpreter to properly copy objects, as well as keep track of which objects were copied.


    use threads;
    use Devel::PtrTable;
    my $someref = \"somestring";
    my $refhash = {
        $someref + 0 => $someref
    my $thr = $thr->create(
        sub {
            while (my ($addr,$v) = each %$refhash) {
                my $newval = PtrTable_get($addr);
                die("oops!") unless $newval == $addr;



Returns a reference to whatever happened to have been stored under $memaddr in the parent thread, or undef if there is no such entry.


Free the copied pointer table


For those who might be interested, this module does the following

  1. Using CLONE_SKIP to initialize an opaque SV and magic structure with a MGVTBL including svt_dup

  2. Inside the svt_dup hook, the stash for this module is unshifted to the beginning of the stashes fields of the CLONE_PARAMS argument. This ensures that no other module will destroy the global PL_ptr_table before us. This also means that we won't be able to see any new pointers possibly added by other packages in their CLONE methods - though this is unlikely

  3. After the interpreter has been cloned, our CLONE method is the first thing called. In this method, we make a duplicate struct ptr_tbl object, which is available until the perl-level PtrTable_freecopied is called


This module cannot be too smart about which entries in the pointer table are valid SVs, which are PerlIO objects, and which are random junk. Users of this module are expected to have a list of valid addresses to use.

Additionally, modules which may insert other entries into the pointer table during their own CLONE methods will not have those entries available to us. See "GUTS" for the reason


perlapi and perl.h


Copyright (C) 2011 by M. Nunberg.

You may use and distribute this module under the same terms and license as Perl itself