I'm a little stumped as to the funky matching that goes on when sub handleABC is embedded in the substitution. Within the function, my @c = split /\[%\s+ELSE\s+%\]/s, $b, 2; splits yoda [% ELSE %] JEFFA correctly into "yoda " and "jeffa ", while within the substitution, it gets split into "" and "yoda [% ELSE %] JEFFA".

I tried searching for threads on this but they're kind of hard to find.
P.S. If you're worried about wheels and such (you shouldn't), a bug in CGI::Kwiki spawned this.

use strict; use warnings; use constant DEBUG => @ARGV; my $v = { stuff => 0 }; my $template = '[% IF stuff %] yoda [% ELSE %] JEFFA [% END %]'; my $ot = $template; $template =~ s{ \[% \s+ IF \s+ (\w+) \s+ %\] # $1 var (.*?) # $2 then \[% \s+ END \s+ %\] } [ my( $a, $b, $c ) = ($1, $2, ''); # handleABC($v, $a,$b,$c); if($b =~ m/\[% \s+ ELSE \s+ %\]/gsx ){ my @c = split /\[%\s+ELSE\s+%\]/s, $b, 2; warn "c(",join('|',@c),")" if DEBUG; $b = $c[0]; $c = $c[1]; } warn "a($a) b($b) c($c)\n----\n" if DEBUG; defined $v->{$a} && $v->{$a} ? $b : $c; ]gsex; warn " \n\n ------------------ \n\n" if DEBUG; $ot =~ s{ \[% \s+ IF \s+ (\w+) \s+ %\] # $1 var (.*?) # $2 then \[% \s+ END \s+ %\] } [ handleABC($v, $1, $2, ''); ]gsex; print ">>>$template<<<\n"; print ">>>|$ot|<<<\n"; sub handleABC{ my( $v, $a, $b, $c ) = @_; warn "----\na($a) b($b) c($c)" if DEBUG; if($b =~ m/\[%\s+ELSE\s+%\]/s ){ my @c = split /\[%\s+ELSE\s+%\]/s, $b, 2; warn "c(",join('|',@c),")" if DEBUG; $b = $c[0]; $c = $c[1]; } warn "a($a) b($b) c($c)\n----\n" if DEBUG; defined $v->{$a} && $v->{$a} ? $b : $c; } __END__ >>>yoda [% ELSE %] JEFFA <<< >>>| JEFFA |<<<

MJD says "you can't just make shit up and expect the computer to know what you mean, retardo!"
I run a Win32 PPM repository for perl 5.6.x and 5.8.x -- I take requests (README).
** The third rule of perl club is a statement of fact: pod is sexy.


In reply to matching / splitting within a substitution by PodMaster

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.