John M. Dlugosz has asked for the wisdom of the Perl Monks concerning the following question:

Withdrawn: Stupid mistake found.

OK, what did I do wrong? I thought I knew this stuff already...

my $re= qr/[^-_.~A-Za-z0-9!*'();:@&=+\$,\/?#\[\]]/; use bytes; $linkref =~ s/($linkref)/sprintf("%%%02X",ord($1))/ge;
Rather than replace every verboten character with the escape sequence, $linkref winds up being a string that contains only one escape sequence, even if the original string shouldn't have needed anything escaped at all.

I know I'm making one or more basic errors here, but I'm just not seeing it. I need a break, and maybe another pair of eyes.

Thanks,
—John

P.S. writing \$1 instead of $1 doesn't fix it, though I do seem to be getting different single codes. It's stuck on %53 now, rather than being different each time.

Replies are listed 'Best First'.
Re: Regex replacement goof
by wind (Priest) on May 07, 2011 at 01:08 UTC

    Your regex s/// contains a reference to the string your changing $linkref instead of to the cached regex $re.

    $linkref =~ s/($re)/sprintf "%%%02X", ord $1/ge;
      Thanks. I saw it just as you answered.