Beefy Boxes and Bandwidth Generously Provided by pair Networks
Keep It Simple, Stupid

Comment on

( #3333=superdoc: print w/replies, xml ) Need Help??

So you've always been curious about the internals of Perl, right? Maybe you're afraid of C or maybe you're on a Windows box and you don't have a C compiler handy, so you've not gotten around to it. Well, as it turns out, Brian Ingerson had an itch to scratch and he scratched it. Using his new Pointer module (not even remotely the same thing as Devel::Pointer), he's written (in pure Perl) a module that let's you explore the Perl internals.

Taking an incredibly brief tour of how the internals work, let's see how a string is stored. (If you want to learn more, see Perlguts Illustrated).

First of all, a basic, null scalar (my $x;) looks something like this:

| ANY           |
| REFCOUNT      |

"ANY" is a pointer to more data, but in the case of a an undefined scalar (SvNULL), it's not pointing to anything. The other fields hold various bits of information that are used for garbage collection, identifying the type of scalar, whether or not it's blessed, etc.

What if you want to store a string? In that case, you have "ANY" point to an SvPV struct, whose PVX pointer points to the actual array of characters representing the string (I can't remember what all of the shorthands mean, either).

              sv       svpv
+---------------+   +-----+
| ANY           |-->| PVX |-->|H|e|l|l|o|,| |w|o|r|l|d|\0| | |
+---------------+   +-----+
| REFCOUNT      |   | CUR |
+---------------+   +-----+
| FLAGS  | TYPE |   | LEN |
+---------------+   +-----+

Note that the first two structs each contain one pointer. How do we get to them? First, create a pointer of "Hello, world":

use Pointer; my $x = "Hello, world"; my $sv = pointer->of_scalar($x); my $svpv = $sv->get_pointer; my $pointer_to_string = $svpv->get_pointer; print $pointer_to_string->get_string; # or print pointer->of_scalar("Hello, world")->get_pointer->get_pointer->ge +t_string;

As you begin to learn more about Perl internals, one of the things that you discover is that Perl can store several representations of a variable internally. For instance, the following can still print "Hello, world", even though the apparent value of $x has been altered.

my $x = "Hello, world"; $x = 42; my $sv = pointer->of_scalar($x); my $svpv = $sv->get_pointer; my $pointer_to_string = $svpv->get_pointer; print $pointer_to_string->get_string;

How does that work? Well, now the scalar looks sort of like this:

              sv     svpviv
+---------------+   +-----+
| ANY           |-->| PVX |-->|H|e|l|l|o|,| |w|o|r|l|d|\0| | |
+---------------+   +-----+
| REFCOUNT      |   | CUR |
+---------------+   +-----+
| FLAGS  | TYPE |   | LEN |
+---------------+   +-----+
                    | IVX |

Now the second struct (svpviv) is similar to the SvPV struct, but it has an extra field that contains the integer value. However, it still has a pointer to the original string value.

Now what happens if we try to get the string value of a scalar which has never had a string assigned to it?

$ perl -MPointer -le 'print pointer->of_scalar(42)->get_pointer->get_p +ointer->get_string;' Segmentation fault

A segfault with pure Perl :)

Pointer is fairly new and the documentation (and tests!) could use some work. For example, I could not figure out, from the docs, how to get the integer out of an SpIV, or how to get at the refcount. Still, with a copy of Perlguts illustrated and this module, you can have fun exploring Perl internals. I'm looking forward to seeing this module developed further.

Update: A quick email exchange with Ingy revealed how to get an integer from an SpIV:

perl -MPointer -MPointer::int -le 'print ((pointer->of_scalar(42)->get +_pointer("int") + 3)->get)' 42


New address of my CGI Course.

In reply to Pure Perl Internals (with Pure Perl Segfaults) by Ovid

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, 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, 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.
  • Log In?

    What's my password?
    Create A New User
    and all is quiet...

    How do I use this? | Other CB clients
    Other Users?
    Others browsing the Monastery: (3)
    As of 2018-03-20 07:07 GMT
    Find Nodes?
      Voting Booth?
      When I think of a mole I think of:

      Results (248 votes). Check out past polls.