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

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.