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"

In reply to Referencifying an address or IV by diotalevi

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.