diotalevi has asked for the wisdom of the Perl Monks concerning the following question:

I'm using B to walk all over perl's internals and now I'd like to get some real references back out. I noticed that the only difference between the ref I pass into svref_2object and the /B::.V/ object I get back out is that the RV is turned into a IV. The key is that it's otherwise the exact same reference I passed in just slightly different.

The goal is to get an ordinary reference back and I'm thinking of two ways to make that happen: Turn the IV back into an RV somehow (I suppose just mangle flags) or somehow take the address and create a new reference. If there is something else that works then I'm all ears as well. I'm hoping to make this happen in core perl but if that's not possible ... oh well. I imagine this is a trivial exercise of XS or Inline::C but I'm just trying to see if it's possible to make a round trip through B.

This is all part of an idea where I was thinking of tying a hash that would make all the various lexical padlists available n' stuff.

Sample code

use strict; use warnings; use Devel::Peek; use B qw(svref_2object); my $string = "Invasion of the Hive!"; my $ref = \$string; my $obj = svref_2object( $ref ); Dump($ref); Dump($obj);
Prints:
>>> This is for $ref
SV = RV(0x19818) at 0x1073c
  REFCNT = 1
  FLAGS = (PADBUSY,PADMY,ROK)
>>> This RV corresponds to the next bit's IV <<<
  RV = 0x107b4
  SV = PV(0x743c) at 0x107b4
    REFCNT = 2
    FLAGS = (PADBUSY,PADMY,POK,pPOK)
    PV = 0x5c60 "Invasion of the Hive!"\0
    CUR = 21
    LEN = 22

>>> This is for $obj which is a B::PV
SV = RV(0x1981c) at 0x10760
  REFCNT = 1
  FLAGS = (PADBUSY,PADMY,ROK)
  RV = 0x7270
  SV = PVMG(0x3a060) at 0x7270
    REFCNT = 1
    FLAGS = (OBJECT,IOK,pIOK)
>>> This IV corresponds to the next bit's RV <<<
    IV = 67508
    NV = 0
    PV = 0
    STASH = 0x1279c     "B::PV"

Replies are listed 'Best First'.
Re: Referencifying an address or IV
by Zaxo (Archbishop) on Oct 01, 2002 at 02:26 UTC

    Devel::Pointer can rereferencify a numified reference. It uses XS to construct the reference from int cast to pointer, and adjusts the reference count.

    After Compline,
    Zaxo

Re: Referencifying an address or IV
by diotalevi (Canon) on Sep 29, 2003 at 22:40 UTC

    This problem is now solved in pure perl using Devel::Pointer::PP which relys on a new method introduced in perl 5.8.1's B module.