$regexp = "a"; $subst = "b"; $global = 1; if ($global) { $string =~ s/$regexp/$subst/g; } else { $string =~ s/$regexp/$subst/; } # (?i...) can be used in $regexp for /.../i # (?m...) can be used in $regexp for /.../m # (?s...) can be used in $regexp for /.../s # (?x...) can be used in $regexp for /.../x # /g cannot be stored in $regexp, thus the need for $global. # /e cannot be stored in $regexp.