To understand the opcodes, you can pick up a copy of Advanced Perl Programming (the panther book) and read chapter 20. If you want to generate opcode listing of a Perl program, you'll need to recompile it with the <nobr>-DDEBUGGING</nobr> option. Then, to dump the syntax tree (with the opcodes), you'd do something like the following:
perl -Dx somescript.pl
On japhy's posts, I have generally noticed that he is listing the regex opcodes. You can get these with the re pragma.
use strict; use Data::Dumper; use re 'debug'; 'abc123def456' =~ /(?<=f)(\d+)/; print "$1\n";
The above code will output how the regular expression compiled and will print out the exact steps the regex engine takes to match. Here's the output from 5.6.1 (ActiveState):
Compiling REx `(?<=f)(\d+)' size 13 first at 1 synthetic stclass `ANYOF[0-9]'. 1: IFMATCH[-1](7) 3: EXACT <f>(5) 5: SUCCEED(0) 6: TAIL(7) 7: OPEN1(9) 9: PLUS(11) 10: DIGIT(0) 11: CLOSE1(13) 13: END(0) stclass `ANYOF[0-9]' minlen 1 Matching REx `(?<=f)(\d+)' against `abc123def456' Setting an EVAL scope, savestack=3 3 <abc> <123def456> | 1: IFMATCH[-1] 2 <ab> <c123def456> | 3: EXACT <f> failed... failed... Setting an EVAL scope, savestack=3 4 <abc1> <23def456> | 1: IFMATCH[-1] 3 <abc> <123def456> | 3: EXACT <f> failed... failed... Setting an EVAL scope, savestack=3 5 <abc12> <3def456> | 1: IFMATCH[-1] 4 <abc1> <23def456> | 3: EXACT <f> failed... failed... Setting an EVAL scope, savestack=3 9 <abc123def> <456> | 1: IFMATCH[-1] 8 <abc123de> <f456> | 3: EXACT <f> 9 <abc123def> <456> | 5: SUCCEED could match... 9 <abc123def> <456> | 7: OPEN1 9 <abc123def> <456> | 9: PLUS DIGIT can match 3 times out of 32767... Setting an EVAL scope, savestack=3 12 <abc123def456> <> | 11: CLOSE1 12 <abc123def456> <> | 13: END Match successful! 456 Freeing REx: `(?<=f)(\d+)'
The first part (lines numbered 1 to 13) are a breakdown of the regex.
Cheers,
Ovid
Vote for paco!
Join the Perlmonks Setiathome Group or just click on the the link and check out our stats.
In reply to (Ovid) Re: Opcodes explained ... ?
by Ovid
in thread Opcodes explained ... ?
by dragonchild
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |