[pm-h] Deparse
G. Wade Johnson
gwadej at anomaly.org
Sat Dec 20 08:15:15 PST 2008
I've recently been reminded of a module that can help in understanding
terse Perl code: B::Deparse.
It's a compiler backend that converts Perl into Perl. This doesn't
sound too useful until you see it used with the -M switch and the O
module. The latest example came from Perl Monks.
perl -MO=Deparse -lpe "s/^\s*\d+://;"
The original questioner wanted to remove some stuff from the front of
every line. The person who answered gave a one-liner without the -M
switch and then suggested this for understanding.
Running this on the command line gives the following:
-------------------------------------------------------
BEGIN { $/ = "\n"; $\ = "\n"; }
LINE: while (defined($_ = <ARGV>)) {
chomp $_;
s/^\s*\d+://;
}
continue {
print $_;
}
-e syntax OK
-------------------------------------------------------
Which shows what perl does, given the one-liner presented. You can also
do this with whole scripts to convert some "overly terse" code into
something more verbose that may be easier to understand.
G. Wade
--
You should never hand someone a gun unless you're sure where they'll
point it. -- Jeffrey Sinclair in "By Any Means Necessary"
More information about the Houston
mailing list