Beefy Boxes and Bandwidth Generously Provided by pair Networks
Syntactic Confectionery Delight
 
PerlMonks  

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:

              sv
+---------------+
| ANY           |
+---------------+
| REFCOUNT      |
+---------------+
| FLAGS  | TYPE |
+---------------+

"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

Cheers,
Ovid

New address of my CGI Course.


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

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!
  • 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
  • Outside of code tags, you may need to use entities for some characters:
            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?
    Username:
    Password:

    What's my password?
    Create A New User
    Chatterbox?
    and the web crawler heard nothing...

    How do I use this? | Other CB clients
    Other Users?
    Others surveying the Monastery: (6)
    As of 2014-08-20 12:56 GMT
    Sections?
    Information?
    Find Nodes?
    Leftovers?
      Voting Booth?

      The best computer themed movie is:











      Results (113 votes), past polls