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":
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.
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?
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:
Cheers, New address of my CGI Course. In reply to Pure Perl Internals (with Pure Perl Segfaults) by Ovid
|
|