in reply to Re: backreference replaced as literal or just fails
in thread backreference replaced as literal or just fails

Sorry for missing the input part. With respect to your binding point, I must not understand the usage. I understand
$rrow_test =~ s/$rex[0]/$rex[1]/ig;
to mean
"Replace the first occurrence of $rex[0] with $rex[1]."
I'm setting the $rex array values on the 'split' line of the code. Am I understanding your point/question correctly or have I missed the mark altogether? Thanks.

Replies are listed 'Best First'.
Re^3: backreference replaced as literal or just fails
by kennethk (Abbot) on Mar 10, 2009 at 19:43 UTC
    The "binding" issue is that you didn't give us any possible values for $rrow_test, thus making it harder to determine intent. In any case, the issue you are likely encountering is that you expect your strings to get interpolated twice. According to Regexp Quote-Like Operators, variables you pass get interpolated at run time. This means that "$rex[1]" gets interpolated to "$1", the literal value (Update: as GrandFather points out below). Your solution is to doubly interpolate the string, using the ee modifier. Note this is potentially a serious security risk.

    Also note your i modifier is useless in your current construct, as you already handle both upper- and lower-case letter.

Re^3: backreference replaced as literal or just fails
by zwon (Abbot) on Mar 10, 2009 at 19:45 UTC
    "Replace the first occurrence of $rex[0] with $rex1."

    Actually /g means replace all occurrences.