Perl Tutorial
Fundamentals
Input and Output
Control Flow
Arrays and Lists
Hash
Scalars
Strings
Object Oriented Programming in Perl
Subroutines
Regular Expressions
File Handling
Context Sensitivity
CGI Programming
Misc
The ee
modifier in Perl's regular expressions is an interesting and somewhat unique feature. It's used to evaluate the replacement side of a substitution (s///
) as code, and then, if there's another e
modifier, evaluate it again. Let's break it down step-by-step.
e
ModifierFirst, let's start with the single e
modifier. The e
modifier causes the replacement side to be evaluated as a Perl expression.
For example:
my $x = 10; my $string = "The result is: RESULT"; $string =~ s/RESULT/$x * 2/e; print $string; # Outputs: "The result is: 20"
In the above, s/RESULT/$x * 2/e
replaces RESULT
with the result of the expression $x * 2
.
ee
ModifierThe ee
modifier takes this one step further. After the first evaluation as a Perl expression, it's evaluated again.
Consider:
my $code = '2 * 3'; my $string = "The result is: RESULT"; $string =~ s/RESULT/$code/ee; print $string; # Outputs: "The result is: 6"
Here's what happens:
e
treats the replacement $code
as code, which translates the replacement to 2 * 3
.e
then evaluates 2 * 3
to get 6
.This can be very powerful, but it's also potentially dangerous. Evaluating strings as code can introduce security risks, especially when dealing with user input. This is similar to eval
in this sense, and similar precautions should be taken.
A potential use case for the ee
modifier is dynamic code execution based on pattern matches. However, it's essential to ensure that the executed code is safe and not derived from untrusted sources.
e
modifier: Evaluates the replacement part of a regex as Perl code.ee
modifier: Evaluates the replacement twice. The first evaluation treats the replacement as code, and the second evaluation runs the result of the first evaluation.Remember to be cautious when using the ee
modifier to avoid potential security issues.
Perl regex 'ee' modifier explanation:
my $string = "2 + 3"; $string =~ s/(\d+) \+ (\d+)/$1 + $2/ee; print "Result: $string\n"; # Output: 5
Perl regex eval modifier 'ee':
eval
function to evaluate the replacement part as code.my $string = "2 * 3"; $string =~ s/(\d+) \* (\d+)/eval("$1 * $2")/ee; print "Result: $string\n"; # Output: 6
Perl regex double eval modifier:
my $string = "2 + 3"; $string =~ s/(\d+) \+ (\d+)/eval("$1 + $2")/ee; print "Result: $string\n"; # Output: 5
Using 'ee' in Perl regular expressions:
s///
) to enable the double evaluation of the replacement part.my $expression = "2 * 3"; my $result = $expression =~ s/(\d+) \* (\d+)/eval("$1 * $2")/ee; print "Result: $result\n"; # Output: 6
Perl regex 'ee' flag examples:
my $expression = "2 + 3"; $expression =~ s/(\d+) \+ (\d+)/$1 + $2/ee; print "Result: $expression\n"; # Output: 5
Dynamic regex with 'ee' in Perl:
my $operation = "substitution"; my $string = "Hello, world!"; if ($operation eq "substitution") { $string =~ s/world/eval('uc("earth")')/ee; } print "Result: $string\n"; # Output: Hello, EARTH!
Perl regex evaluation with 'ee' modifier:
eval
function.my $string = "3 * 4"; $string =~ s/(\d+) \* (\d+)/$1 * $2/ee; print "Result: $string\n"; # Output: 12
Perl regex double evaluation 'ee' usage:
my $expression = "2 + 3"; $expression =~ s/(\d+) \+ (\d+)/eval("$1 + $2")/ee; print "Result: $expression\n"; # Output: 5
How to use 'ee' in Perl regex patterns:
s///
) in a regular expression, enabling the double evaluation of the replacement part.my $expression = "2 * 3"; $expression =~ s/(\d+) \* (\d+)/eval("$1 * $2")/ee; print "Result: $expression\n"; # Output: 6