Multiple choice technology programming languages

Suppose the variable $var has the value abc123abc . What is the value of $var after the following substitution? $var=~ s/(\d+)/ $1*2 /e ;

  1. “abc”

  2. It will produce error

  3. “ABC123ABC”

  4. ”abc246abc”

Reveal answer Fill a bubble to check yourself
D Correct answer
Explanation

The regex /e modifier evaluates the replacement as Perl code. It matches 123, captures it in \$1, then evaluates 123*2=246. The substitution replaces 123 with 246, resulting in abc246abc. Option D is correct. Without /e, it would treat \$1*2 as literal text. The /e modifier enables dynamic computation in regex replacements.

AI explanation

"abc246abc" is correct. The /e modifier on a Perl substitution tells Perl to evaluate the replacement text as executable Perl code rather than treating it as a literal string. Here the pattern (\d+) captures "123" from "abc123abc" into \$1; the replacement code " \$1*2 " — surrounding whitespace is insignificant in Perl code — evaluates the expression \$1*2, i.e. 123*2 = 246, and that numeric result (246) is what gets substituted in place of the matched "123", yielding "abc246abc". Without /e, the literal string " \$1*2 " (with $1 interpolated but *2 left as text) would have been inserted verbatim, which is what makes this a genuine /e-modifier test rather than a plain substitution.