Beefy Boxes and Bandwidth Generously Provided by pair Networks
Come for the quick hacks, stay for the epiphanies.
 
PerlMonks  

comment on

( [id://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":



  • Are you posting in the right place? Check out Where do I post X? to know for sure.
  • Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
    <code> <a> <b> <big> <blockquote> <br /> <dd> <dl> <dt> <em> <font> <h1> <h2> <h3> <h4> <h5> <h6> <hr /> <i> <li> <nbsp> <ol> <p> <small> <strike> <strong> <sub> <sup> <table> <td> <th> <tr> <tt> <u> <ul>
  • Snippets of code should be wrapped in <code> tags not <pre> tags. In fact, <pre> tags should generally be avoided. If they must be used, extreme care should be taken to ensure that their contents do not have long lines (<70 chars), in order to prevent horizontal scrolling (and possible janitor intervention).
  • Want more info? How to link or How to display code and escape characters are good places to start.
Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others imbibing at the Monastery: (2)
As of 2024-04-25 07:30 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found