Anonymous Monk has asked for the wisdom of the Perl Monks concerning the following question:

What I want to be able to do:
$x = '123'; $y = $x; $y =~ s/(2)/?{$1+1}/x; print $y
with the result:
133
But instead I get the following:
1?{2+1}3
Is there a regex way to that? The best non-regex way?

Replies are listed 'Best First'.
Re: perl code in regex replacement
by toolic (Bishop) on Mar 12, 2011 at 18:16 UTC
    The best non-regex way?
    The best way to add 10 to a number is to use +
    use warnings; use strict; my $x = 123; my $y = $x + 10; print $y;
    But, I suspect your example is a much-reduced version of a larger problem you are trying to solve.
Re: perl code in regex replacement
by ww (Archbishop) on Mar 12, 2011 at 17:29 UTC
    Assuming (dangerous) that I've interpreted your problem statement as you intended, one way of doing the job (in the problem you specified; adding 10 to a 3 digit decimal value) and without resort to executing code inside a regex is:

    #/usr/bin/perl use strict; use warnings; # 892837 my $x = 123; # no quotes my $y = $x; $y =~ /(\d{2})(\d)/; # regex, but see also perlretut re executing c +ode in a regex $y = ($1+1).$2; print $y;
Re: perl code in regex replacement
by Anonymous Monk on Mar 12, 2011 at 17:17 UTC
Re: perl code in regex replacement
by wind (Priest) on Mar 12, 2011 at 18:15 UTC
    my $x = '123'; (my $y = $x) =~ s/(2)/$1+1/e; print $y;
    Or
    my $x = '123'; (my $y = $x) =~ tr/2/3/; print $y
    Or even ;)
    my $x = '123'; my $y = $x + 10; print $y