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;
| [reply] [d/l] [select] |
> 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.
| [reply] |
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.
| [reply] |
Actually, what might serve the OP's purpose is Named backreferences, a new feature of 5.10. For example, to primitively rearrange a date:
$date = '8/1/2008';
$date =~ s!(?<month>\d{1,2})/(?<day>\d{1,2})/(?<year>\d{4})!$+{year}-$
++{month}-$+{day}!;
This allows you to attach a name to a backreference like (?<name>pattern) that can be used within the match as \g{name} and anywhere else (in the replace or outside of the regex) as part of the hash %+. Keep in mind though, just because its a shiny new feature doesn't mean you have to use it. First figure out if its the best tool for the job :)
Disclaimer: Of course, the example above is a quick and dirty example, and as such should never be used. It was only meant to illustrate the feature noted.
Update: Ah, perhaps I did misunderstand the original question after all.
__________
Systems development is like banging your head against a wall...
It's usually very painful, but if you're persistent, you'll get through it.
| [reply] [d/l] [select] |
I don't see how named references allow $sentenceCount++ to be executed in the replace expression.
| [reply] [d/l] |
I recall this was recently dicussed?! Have you already tried using the /e (e modifier) or even /ee (double e modifier)? You can also fiddle with the eval statement.
| [reply] |