in reply to Using variables in RegEx

First of all, there are no "predefined" variables. All variables can be interpolated. The replace expression in a string literal like any other.

s/.../...$foo.../g;

But you want to have Perl code in the replace expression. That's actually possible too. Using /e, the replace expression is treated as a Perl expression.

s/.../$sentenceCount++; foo()/eg;

$sentenceCount doesn't seem to make much sense with s///, though.

$sentenceCount++ while /.../g;

Replies are listed 'Best First'.
Re^2: Using variables in RegEx
by hsfrey (Beadle) on Aug 01, 2008 at 16:48 UTC
    > First of all, there are no "predefined" variables. < The NAMES are predefined, eg $1, $', etc. > $sentenceCount doesn't seem to make much sense with s///, though. < I want to insert a unique html anchor for each sentence, so I need to increment the number to construct a unique name.

      The NAMES are predefined, eg $1, $', etc

      What I meant is that it's not a meaningful distinction. Since all variable should be declared, it's like saying "electronic television". Until they use laser circuitry for TVs, there's no such thing as electronic televisions. There are just televisions.