http://qs1969.pair.com?node_id=1022397


in reply to Find pieces of text in a file enclosed by `@` and replace the inside

Step one is that you replace text between '@' delimiters. You can do that using
s/\@(.*?)\@/ ... /g;
Step two is to replace individual characters on the selected part.

Using the /e modifier you can use perl code in the substitution part, where you can use $1 as a normal variable. With a pair of "{}" delimiters on the right hand side, it can even look like normal code, as it looks like a block; you have to use similar paired delimiters on the left to make it work, for example using angle brackets "<>":

s<\@(.*?)\@>{ ... }ge;
So you might try to do the replacement using code directly in the substitution part. But, to be safe, you'd better call a sub to do the actual replacement, on the selected text. I'd change your code like this:
while(<>) { s/\@(.*?)\@/ subst($1) /ge; print; } sub subst { my $s = shift; my %r = ( 'a' => chr(0x430), 'b' => chr(0x431), 'c' => chr(0x446), 'd' => chr(0x434), 'e' => chr(0x435), 'A' => chr(0x410), 'B' => chr(0x411), 'C' => chr(0x426) ); $s =~ s/([a-eA-C])/$r{$1}/g; return $s; }

Caveat: untested.

update: Tested, and bug fixed, this line was wrong:

s/\@(.*)\@/ subst($1) /ge;