in reply to Challenge: Transforming markups

s/($named)|($http)|($camel)/$1?tansform_named():$2?transform_http():transform_camel()/gxe

But that starts to get awkward if you have a lot of patterns to deal with.

Putting your patterns in a hash might be a bad idea, because it randomizes the order, and you might end up with the unknown pattern first.

Replies are listed 'Best First'.
Re^2: Challenge: Transforming markups
by Anonymous Monk on Dec 06, 2013 at 22:19 UTC

    Argh

    s/($named)|($http)|($camel)/$1?tansform_named($1):$2?transform_http($2):transform_camel($3)/gxe
      OK, but the problem here are nested capture groups.

      you can't rely on $2 or $3 to be correct if $named is something like [[ ($http) ][ ($title) ]]

      Cheers Rolf

      ( addicted to the Perl Programming Language)

        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 } }

        This can be easily fixed by using named backreferences. \k<named> would refer to a match of the pattern (?<named>...).

Re^2: Challenge: Transforming markups
by LanX (Saint) on Dec 06, 2013 at 22:31 UTC
    > Putting your patterns in a hash might be a bad idea,

    look at the code again, it's an array that preserves the order.

    Cheers Rolf

    ( addicted to the Perl Programming Language)

      In that case, it seems the hash isn't used.
        yep, not yet. I've spared this part for simplicity of the posting.

        But the coderef to the transformation is planed to be put in there.

        Perl doesn't have ordered hashes thats why I'm improvising here.

        Cheers Rolf

        ( addicted to the Perl Programming Language)