Indeed so. That would be just one of quite a few changes I would make to perlboy_emeritus's code if I were writing it myself. Instead, I tried to make the smallest number of changes to his original code to achieve what should be his desired outcome so that it is clearest that these are the changes needed to address the stated problem.
If writing it myself but still keeping s/// as the operator under test it would probably look like this:
use strict; use warnings; use 5.020; # for best efficiency on $& use Test::More tests => 4; my $orig = 'This is a real number, 123456.56'; my $want = 'This is a real number, 493824.56'; (my $have = $orig) =~ s/\d+/sprintf "%i", 4 * $&/e; my $intpart = $&; is $intpart, 123456, 'int part captured'; is $have, $want, 'Multiply int part by 4'; ($have = $intpart) =~ s/./3/g; is $have, 333333, 'Int part digits set to "3" trivially'; $want = 'This is a real number, 333333.56'; ($have = $orig) =~ s/\d+/(my $int = $&) =~ s#.#3#g; $int/e; is $have, $want, 'Replace int part in string with all 3s';
Look, Ma - no capture groups! :-)
🦛
In reply to Re^3: regex in REPLACEMENT in s///
by hippo
in thread regex in REPLACEMENT in s///
by perlboy_emeritus
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |