in reply to constants in an RE

Far fetched but you can execute perl code in BOTH a regex-match (e.g. /HERE/) and the match-side of a regex substitution (e.g. s/HERE/../) and use its return value to shape the final regex pattern (see (??{ code })). So, using (??{ code }), you can do:

use constant APOSTROPHE => "\xE2\x80\x99" ; my $string = "..."; $string =~ s/(?{{ APOSTROPHE }})/'/ ;

If you want to call perl code in the replacement side of a substitution (e.g. s/../HERE/) simply add the /e (for eval) regex modifier, i.e. $string =~ s/.../APOSTROPHE/e

Perl code executes within the baby-cart operator suggested by haukex and also in choroba's ref-deref trick.

Edit: OP's code my $string =~ s/...//; has been modified above, thanks choroba.

bw, bliako

Replies are listed 'Best First'.
Re^2: constants in an RE
by Anonymous Monk on May 16, 2023 at 14:15 UTC

    Note that (?{{ ... }}) turns off at least some regex optimizations, since the Perl compiler has (at least in the general case) no idea what the code will actually return. Whether this is a problem depends on what you are doing.