|Do you know where your variables are?|
So, you know everything there is to know about Perl. You stifle a yawn as you flick through the latest obfuscations – how obvious they are! It's gratifying to be an expert; and your patient, only slightly condescending, help is appreciated by the less experienced. Life is good. But sometimes, in a reflective moment, you miss your younger days as an intrepid explorer in unfamiliar terrain, where there was always a new mystery to unravel, a new landscape to be discovered.
You could go and learn ruby or something, but that feels vaguely disloyal after so many years nestled happily in the bosom of Perl. And besides, you don't really like the idea of being patronised by experts on rubymonks or whatever the hell they have.
This is the node you've been waiting for. There is a way! You can plunge into an unfamiliar world of mystery and at the same time enhance your reputation as a Perl guru. I'm talking about perl: the guts, the source, the motherlode.Update: Fixed a few typos; added a ďTestingĒ section, as suggested by hossman.
You've probably heard the stories, of strong men who went there and never returned, or returned mumbling and broken and wonít tell what they saw. One explorer survived long enough to describe ďan interconnected mass of livers and pancreas and lungs and little sharp pointy things and the occasional exploding kidneyĒ.
Donít worry about it. Youíll be fine. The mistake that people often make is they try to understand it. (Some people make the same mistake with life, or the state of the world, or The Prisoner.) If you can avoid that, you'll be all right. Just crack it open and get stuck in. The slogan of the hour is ďHACK FIRST, THINK LATERĒ.
The trick is just to mess with things. Donít waste too much time worrying about what's going to happen: try it and see what happens.
People often say that a good way to get started is to try and fix a bug that somebodyís reported. Thereís some truth in that, but debugging isnít most peopleís idea of a good time. So that's not what we'll do; instead weíll add a new feature: lexical typeglobs. Itís always bothered me a little that you canít say my *foo. Why canít you? Thereís no good reason. Itís not very useful, I admit, but it would be kind of cool.
I did it using the HACK FIRST methodology, and wrote down what I was doing as I went along. I was really surprised at how easy it turned out to be: the final patch only changes 38 lines of code.
The first thing to do is to get a copy of the source. You can hack on whichever version you like, but I decided to use the latest ďbleadperlĒ. If you want to follow along, you should get it too. This is an interactive tutorial: itís not designed for reading in the bath, and it probably wonít make so much sense if youíre not actually tinkering with the source while you read.
So find a disk with a reasonable amount of free space, make a directory for the source to go in, and grab it. I did it like this:
and you should do something similar. Now build it, to make sure itís working before you start.
You can vary this as you like, but the -Doptimize='-g' -Dusedevel is essential. The -Dusedevel tells it that, yes, you really want to build a development version; and -Doptimize='-g' turns on debugging mode, which weíre going to make good use of later on.
In case you havenít looked at perlhack, Iíll quickly explain the rough structure of the source. Perl code is tokenised by a rather hairy routine called yylex that lives in toke.c, then itís parsed using the bison grammar that lives in perly.y. The grammar uses the routines in op.c to build an optree.
The optree is then executed by a one-liner that lives in run.c, which dispatches each op to the appropriate routine. The ops themselves are implemented by functions in the files pp*.c.
Back to the problem at hand! Our first task is to persuade perl to recognise the new construct. A quick
which shows that the parser doesnít even recognise the syntax. So we crack open the grammar (in perly.y, remember), and start grepping for Ďmyí. Soon we find this:
and you don't need a degree in rocket science to see that this is the bit weíre interested in. Down at the end of the file, the symbols Ďscalarí, Ďhshí and Ďaryí are defined, like this:
and thereís a similar entry for globs, though it seems to be called ďstarĒ rather than ďglobĒ. (The perl source is full of little things that donít quite make sense – thatís part of its charm.) Here it is:
So letís add another clause to the definition of Ďmytermí, like this:
Time to check it out! First we have to rebuild the parser using the new grammar, then rebuild perl itself:
Try the one-liner again:
Great! Itís been parsed okay, and itís now being rejected during compilation. The compiler is housed in op.c, and weíll need to write some code to compile our new construct. But before we can do that, we need to decide what weíre going to compile it to. Letís have a quick peek at what perl does with other Ďmyí declarations:
Okay, so they get compiled to special ops called /pad.v/. (In case you donít know, thereís a special perl guts shorthand for different types of value. The most important ones are: a scalar is an SV, an array is an AV, a hash is an HV and a glob is a GV. Oh yeah, and a reference is an RV. Pretty simple really.)
Looks like we ought to make a padgv op! The ops are all defined in a file called opcode.pl, which auto-generates the relevant header files. If we were worried about backwards compatibility, weíd add the new op at the end; but this is just for fun so weíre not really fussed about compatibility, and weíll add it at the logical place in the file:
Now run opcode.pl, which updates opcode.h, opnames.h and pp_proto.h on your behalf.
Weíve got a new op, but the compiler isnít going to use it unless we tell it how. So crack open op.c, and squint at the Perl_newGVREF() function. Looking at Perl_new[SAH]VREF for comparison, itís fairly obvious what we have to do:
Weíre going to need to implement the new op at some point, but for now letís just whack in a placeholder:
Wahey! Letís check out what weíve got so far. Run Ďmakeí to rebuild it, then:
Oh dear. :-(
The error is to be expected – we still havenít looked into that – but we have OP_RV2GV instead of our shiny new OP_PADGV. Whatís that about? Time to wheel out the old debugger.
Hmm, so weíve got an OP_CONST instead of the OP_PADANY we were expecting. Itís time to find out where those PADANYs are coming from:
Ah! Itís tokeniser magic. (You didnít think the tokeniser just tokenised, did you? Oh no.) That means itís time to dive into toke.c and see if we can grok whatís happening there. These newOP() commands are both in a function called S_pending_indent(), which gets called right from the top of the main lexer routine Perl_yylex():
That means that PL_pending_ident must be getting set for $foo, @foo and %foo, but not for *foo. A quick grep through the file reveals that weíre quite right – when a Ď*í is encountered, something called force_ident() gets called instead. Letís try changing it:
We try rebuilding perl. It builds miniperl okay, but then it dies with a load of syntax errors during the build process. The most telling-looking one is the second one:
Hmm, letís see what the tokeniser is up to:
I canít tell whatís wrong just looking at that, so letís try comparing it to something similar that does work:
Thatís interesting! Thereís definitely something different there. The second block of the Ď*í run doesnít appear at all in this one.
Maybe we got something wrong in the tokeniser change? Sure enough, another look at toke.c shows that the code for Ď%í is setting PL_tokenbuf = '%', which we werenít doing. So letís try a slightly improved change, copying the structure of the Ď%í code a bit more faithfully:
Right, letís try another make. This one fails too, but in a much more interesting way:
Looking at line 1158 of Config_heavy.pl, we find:
It looks like "use strict" is now affecting globs! Indeed:
Also, it seems to be confusing globs with hashes. What's this "%foo" all about? Let's see where the error is coming from:
A quick look in gv.c tells us that it's coming from Perl_gv_fetchpvn_flags(). Who's calling that? It's debugger time again!
Aha! So S_pending_ident() is calling gv_fetchpv(). There it is, right at the end, like this:
Ah! It's assuming that anything that's not a scalar or an array must be a hash. But we've added a new possibility, so letís tell it about that:
Now we try another Ďmake perlí, and everything builds as normal. Phew! Even better, the new op is being used in the right place:
Still got that pesky error though... I wonder where that one's coming from:
opmini.c is just an autogenerated copy of op.c thatís used to build miniperl. I donít know why thereís a separate file for this – probably those hysterical raisins again. Anyway, this looks like the right bit, here:
I guess we need to tell it about our new op.
Rebuild once again, and:
Itís executing our new op! Now weíre talking! I guess that means that we ought to implement the thing. We donít really know what weíre doing here, so letís just try something really simple, a kind of ultra stripped-down version of pp_padsv, and see what happens.
It builds okay again, so letís try and do something useful with it:
Hmm, another error. Where's it coming from?
Aha! This is the default clause in a big switch statement, in a function called Perl_mod(). I guess we need to tell this about our new op too:
Right, now letís try to use it again:
Hot damn! It seems to be working. Letís try some more experiments:
No way! This is great. Hmm, I wonder what a new glob is called:
Oh dear, not so good. I guess we need to initialise the new glob somehow. It looks like we can make a new glob using newGVgen(), but we have to pass a package name, and of course a lexical glob doesnít live in a package. Let's use the bogus package ďlexicalĒ, so lexical globs are easy to spot. The other problem is that newGVgen() returns a pointer to the GV, and there doesn't seem to be any sensible way to copy this GV into the pad entry.
(Confession: I made a false start here. I tried copying the GV into the pad sv using sv_setsv(), but it doesn't seem to copy all the relevant fields. So then I tried the following.) This calls for a slight change of strategy. We'll use the pad entry as a reference to the glob. Like this:
If youíre wondering what all these oddly-named functions are doing, have another squint at perlguts.
(Okay, I admit it! I didnít get this right first time either. I forgot the SvROK_on(), which caused segfaults during global destruction (of all the bizarre places), and it took a lengthy session in the debugger before I figured out what Iíd done wrong.)
Anyhow, letís give it a whirl:
Great! Let's try a closure:
That prints ďHmm23Ē, which is cool!
Look at this though:
Oh dear! Our supposedly lexical globs are being created as real package variables. That leads to stupendous memory leaks – for example, the loop:
will keep on growing until the computer runs out of memory. We'd better do something about that. How about deleting the entry from the %lexical:: stash as soon as it's been created? It's only a one-line addition:
Okay... letís try it out. The examples above still seem to work. What about this?
How cool is that? You can see the memory being reused – the same addresses keep coming back again and again. It looks like we have a working Perl interpreter with a shiny new feature!
Every time you add a new feature, or fix a bug, you ought to add some regression tests to make sure that it keeps on working in the future. Even though we donít expect this particular patch to be maintained in the future, itís good practice. Perlís test suite lives in (the subdirectories of) the directory t; the my operation is tested in t/op/my.t. We need to decide whether to add our tests to the existing file, or make a new one. If you look at the op tests, you'll notice that the fundamental ones are all coded by hand – they donít use Test::More. That's because Test::More is complicated enough that, if a really fundamental feature gets broken, it will almost certainly stop working. It might even stop working in such a way that all the tests appear to have passed, which would be very bad!On the other hand, we can be pretty sure that Test::More doesnít make use of lexical typeglobs, so thereís no reason we canít use it in our tests. That settles it then: weíll make a new file, say t/op/my_glob.t:
It could certainly be more thorough, but this covers the essentials.
If you run Ďmake testí at this point, there are a handful of test failures. That's not a real problem – they all come, in one way or another, from the fact that we've added a new opcode. The Opcode module whines that it doesnít know about this crazy padgv thingy, but itís easy to make it happy:
In similar vein, the test for Safe uses a list of tests that has to match up with the list of ops.
One of the tests for B::Concise fails because it's looking for a specific opcode, which we've changed by inserting a new op into the middle of the list. That's easy to fix too:
Now the tests all pass!
Thereís one more thing. If youíre making a non-standard change to perl, like we've just done, you're supposed to register it as a local patch. You do that by adding a line to patchlevel.h, like so:
Now when we run ./perl -V, we get:
The whole patch is here. It only changes 38 lines of code.