To make it even clearer, remember that the right side of the
s///ge
can be an arbitrary block. Here's the "Perl Bowling" style written for clarity:
{ my $count = 0;
$string =~ s{($pattern)}{
my $source = $1;
$count++;
if ($count == $target) {
$source = $replacement; # replace!
}
$source; # either original or replacement now
}ge;
};
And yes, you can actually write it like that, just as you see. If you're doing
anything odd in the right hand side in a
s///e, I recommend expanding
it out like that.
-- Randal L. Schwartz, Perl hacker