How to use the /e modifier for code evaluation in substitution?
Question
How to use the /e modifier for code evaluation in substitution?
The /e modifier in Perl’s substitution operator s/// is a powerful but sometimes confusing feature that allows you to execute Perl code during a substitution. Instead of replacing the matched text with a static string, /e tells Perl to treat the replacement part as a Perl expression, evaluate it, and replace the match with the result of that evaluation.
How the /e Modifier Works
Normally, a substitution looks like this:
s/pattern/replacement/;
This replaces pattern with the string replacement. But if you add the /e modifier, Perl evaluates replacement as if it were Perl code each time the pattern matches.
For example:
$text =~ s/(\d+)/$1 * 2/e;
When $text contains numbers, those numbers are doubled because Perl evaluates $1 * 2.
Why Use /e?
- Dynamic replacements: Compute replacements based on matched content.
- Complex transformations: You can run any code, including calling functions or evaluating expressions.
- Flexible conditional replacements: Execute different code depending on the match.
Multiple /e Modifiers
Since Perl 5.10, you can use /ee (double e), to evaluate the substitution result twice, which is helpful if the evaluated code returns another piece of code to be evaluated again.
Common Pitfalls
- Security risk: Running arbitrary code from untrusted input with
/eis dangerous and may allow code injection. - Context awareness: The code runs in scalar context, so be careful when returning lists or arrays.
- Quoting issues: Using quotes inside the replacement string can be tricky, so consider using single quotes around the pattern/replacement to avoid interpolation surprises.
Runnable Example
The following example doubles every number in a string using the /e modifier:
use strict;
use warnings;
my $text = "I have 3 apples and 7 oranges.";
print "Original text: $text\n";
# Double each number found
$text =~ s/(\d+)/$1 * 2/e;
print "Modified text: $text\n";
Output:
Original text: I have 3 apples and 7 oranges.
Modified text: I have 6 apples and 14 oranges.
Explanation of the Code
s/(\d+)/$1 * 2/e: Finds one or more digits (\d+), captures them in$1.- The replacement part
$1 * 2is evaluated as Perl code because of/e. - The resulting number from the multiplication replaces the matched digits.
The use of the /e modifier lets you leverage Perl’s "There's more than one way to do it" (TMTOWTDI) philosophy by enabling code execution right within regex substitutions, offering flexibility beyond static replacements.
Verified Code
Executed in a sandbox to capture real output. • v5.34.1 • 4ms
(empty)(empty)Was this helpful?
Related Questions
- How to validate email addresses using Perl regex?
- How to split a string by regex pattern in Perl?
- How to use greedy vs non-greedy quantifiers in Perl regex?
- How to match start and end of string with ^ and $ in Perl?
- How to use alternation | in Perl regex?
- How to use the qr// operator to compile regex in Perl?