in reply to Re^3: Challenge: Transforming markups
in thread Challenge: Transforming markups

If you just don't like referring to the global %-, maybe something like:

s/$regex/transform(%-)/ge sub transform { my %matches = @_; ... }

Or you can do a second match.

s/($regex)/transform($1)/ge sub transform { local $_ = shift; if (/^(\[\[($http)]\[($title)]])/) { return transform_named($2,$3) +} elsif (/^($http)/) { return transform_http($1) } ... }

Or you might use something like this.

while (1) { if (/\G($named)/gc) { print transform_named($1) } elsif (/\G($http)/gc) { print transform_http($1) } ... elsif (/\G(.)/gcs) { print $1 } else { last } }

Replies are listed 'Best First'.
Re^5: Challenge: Transforming markups
by LanX (Saint) on Dec 07, 2013 at 16:43 UTC
    > Or you might use something like this.
    while (1) { if (/\G($named)/gc) { print transform_named($1) } elsif (/\G($http)/gc) { print transform_http($1) } ... elsif (/\G(.)/gcs) { print $1 } else { last } }

    Thanks I ended up using this approach, it's the most practical in my case! ¹

    Actually I knew and tried it before w/o luck, turned up that the /\G(.)/gcs was crucial.

    I remember \G to be tricky (or even buggy) in edge cases,

    (Don't really know why I need /s since there are no line breaks in single lines? ...have still to debug whats going on with pos here...)

    But it works now, thanks! =)

    Cheers Rolf

    ( addicted to the Perl Programming Language)

    ¹) The code in the OP might be a good way to design a parser generator but I dont need the complexity and the extra speed of one big regex.