Loginskip to content

October 31st, 2006

The specification of that function says that if

12/31/99 This is the first line 12/31/99 This is the second line 12/31/99 This is the third line Running under the debugger won’t reveal anything other than the fact that $t isn’t being increased in the do block. Wait a minute. It doesn’t say do, does it? It’s a typo: dp. Yes, we could fix the program right now. But curiosity demands that we wonder why Perl has no objections! What could it be thinking we mean? Enter the B::Deparsemodule. The B stands for “back-end,” which is to say, this module accepts input from the Perl compiler after it has created a binary opcode tree from a program. The whole purpose in life of B::Deparse is to turn that opcode tree back into Perl. For instance, you can use it to turn a subroutine reference into program text in the middle of a program. Deparseisn’t quite perfect you may see some odd output, at times even incorrect output but it’s getting there.[2] Let’s see what we get when we run it on just a part of the line of code that’s bugging us. Notice that we don’t invoke it with a use statement but rather with a funny variant of the -Moption designed for back-end modules: [2] Larry Wall said that Perl 5 should be translatable into Perl 6 via a kind of Deparse module, so we can expect considerable progress. % perl -MO=Deparse -e ‘dp { $t += 86400 } while (localtime $t)[6] == 0′ do { $t += 86400 }->dp while (localtime $t)[6] == 0; -e syntax OK Aha! Perl is interpreting our typo as a use of the indirect object method syntax it thinks we want to call the dpmethod of whatever the block $t += 86400 evaluates to. So this is no longer a doBLOCKwhileEXPRESSION construct (which guarantees the block will be executed at least once). Instead it is a STATEMENTwhileEXPRESSIONconstruct (where the statement is do { $t += 86400 }->dp), which means that the statement will be executed only if the expression is true, which in this case, it isn’t to begin with. The Deparsemodule can often explain how perl is parsing your code. Note: Deparse only came into the core with version 5.005, and it’s documentation has some additional options worth checking out, like the ability to totally parenthesize expressions so you can see how precedence is determined. (And if you see ‘???’in the output, that stands for a constant in the input that was optimized by perl to the point where its value was not available to Deparse.) Here’s another example in which Deparse helps. Let’s say you’re scanning text to pull out URLs and want to clean them up a little by removing redundant default port numbers:

Hint: This post is supported by Gama besplatan domen provider

Comments are closed.