[PREV - E_WITH_POS] [TOP]
NEXT
CODE_EXAMPLE
Some simple code to do this trick:
# Givens: $text, $find_pat, $replace
my @changes; # save the change metadata
$text =~
s{$find_pat}
{
my $new = eval "return qq{$replace}";
my $l1 = length( $& );
my $l2 = length( $new );
my $delta = $l2 - $l1;
# pos points at the *start* of the match (inside of a s///eg)
# Note: char numbering fixed at the start of the s///ge run
my $p = pos( $text ) + 1;
my $beg = $p;
my $end = $p + $l2; # the end *after* the substitution
push @changes, {
beg => $beg,
end => $end, # endpoint *after* change
delta => $delta,
orig => $&,
rep => $new,
};
$new
}eg;
--------
[NEXT - FRAMES_OF_REFERENCE]