motivation

I was confronted to present a condensed explanation of the use cases and side-effects of matching-regexes in Perl

 LHS = ($string =~ /RE(G)EX/MOD)

which depend on:

for comparison, these use cases had to be implemented with multiple methods in JS
meditation

Wouldn't it be nice to create automatic tests for all cases to create a nice lookup-table/cheat-sheet?

This table would have a worst case of 4 dimensions ( context, result, capture, /g ), which means many possible projections into a 2D table, here an attempt how it might look like, if the result is coded into the cell's value

(NB: this is untested guess work and turned out to be wrong I leave it to you to spot the errors)

+------------+-----------+----------+----------+--------------+ | | SCALAR / VOID | LIST | | m/REGEX/ +-----------+----------+-----------+-------------+ | | | (groups) | | (groups) | +------------+-----------+----------+-----------+-------------+ | / | ->BOOLEAN | ->COUNT | ->BOOLEAN | ->CAPTURES | | /g | ->BOOLEAN | ->COUNT | ->BOOLEAN | ->CAPTURES | +------------+-----------+----------+-----------+-------------+

So in an outbreak of ADHD, hubris, and too much time b/c of Easter holidays I started to write - well hack - test code:

These are my results so far, only as data structures.

Imperfect because I wanna leave the house still during daylight and risks are high that the code will remain unfinished on my disk without being shared. And it even seems to be wrong. The task to condense it into a meaningful table which avoids unnecessary repetitions is even farther away...

hack

use v5.12.0; use warnings; use Data::Dump qw/pp dd/; use Data::Dumper; my $time = '"2$X:1$X:0$X "'; my $str = ""; for my $X (1..3) { $str .= eval($time); } say $str; my ($res,@res) ; my %CTX= ( VOID => { LHS => '' , RES => undef }, SCALAR => { LHS => '$res =', RES => \$res }, LIST => { LHS => '@res =', RES => \@res }, ); my %REGEX = ( "GROUPS_NO" => '2.:1.:0.', "GROUPS_YES" => '(2.):(1.):(0.)' ); my %got; my $ctxt_cnt = 0; for my $ctx (qw/SCALAR LIST VOID/) { for my $groups ( map "GROUPS_$_", qw/YES NO/ ) { for my $mod ("","g") { my $LHS = $CTX{$ctx}{LHS}; my $REGEX = $REGEX{$groups}; my $code = "$LHS ( \$str =~ /$REGEX/$mod );"; eval($code); my $ref = $CTX{$ctx}{RES} ; my $type = ref $ref; my $res = $type eq "SCALAR" ? $$ref : $type eq "ARRAY" ? [ @$ref ] : undef; pp { context => $ctx, code => $code, mod => $mod, result = +> $CTX{$ctx}{RES} }; $got{$ctx}{"MOD_$mod"}{$groups} = $res ; } } $ctxt_cnt++; } $Data::Dumper::Indent=1; $Data::Dumper::Terse =1; say Dumper \%got; #pp \%got;

-->

what's even worse, even my tests don't fit with what I see in the debugger
DB<36> @res = ( $str =~ /(2.):(1.):(0.)/g ) DB<37> x @res 0 21 1 11 2 01 3 22 4 12 5 02 6 23 7 13 8 03 DB<38>

Edit

Hmm, the bug may be related to pos not being properly reset. Must check when back home :)

update

yeah, that was it, resetting the string is fixing the issue. Next version must monitor the side effects.

Cheers Rolf
(addicted to the 𐍀𐌴𐍂𐌻 Programming Language :)
Wikisyntax for the Monastery


In reply to Automatic documentation of match operations? by LanX

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.