Main Content
Perl substitute - the e modifier Archive - Originally posted on "The Horse's Mouth" - 2008-12-16 07:58:51 - Graham Ellis
Here's a graphic illustration of the use of the "e" for "execute" modifier used on the end of substitute operation in Perl.
The "s" for substitute allows you to replace a matched pattern with a STRING in which you can use special references like or for the first matched substring. If you want it to replace the matched pattern with the RESULT OF RUNNING CODE , you should add that extra e on the end.
In this example, the $emma variable ends up containing a corrupted email address which actually includes some code, whereas (with the "e"), the $emily variable results in a correct email address with the section before the "@" pushed to lower case.
$emma = 'Graham@wellho.net';
$emily = 'Graham@wellho.net';
$emma =~ s/(\w+)@/lc()."@"/;
$emily =~ s/(\w+)@/lc()."@"/e;
print ("$emma\n$emily\n");
__END__
Dorothy:plpw grahamellis$ perl swithe
lc(Graham)."@"wellho.net
graham@wellho.net
Dorothy:plpw grahamellis$
Some other articles
P669 - Data Munging Perl 6 - a Practical Extraction and Reporting example! Shell, Awk, Perl of Python? Converting codons via Amino Acids to Proteins in Perl Practical Extraction and Reporting First and last match with Regular Expressions Nothing beats Perl to solve a data manipulation requirement quickly This article Extracting information from a file of records Filtering and altering Perl lists with grep and map Storing a regular expression in a perl variable P212 - More on Character Strings Binary data handling - Python and Perl First match or all matches? Perl Regular Expressions Converting codons via Amino Acids to Proteins in Perl Possessive Regular Expression Matching - Perl, Objective C and some other languages Serialsing and unserialising data for storage and transfer in Perl The difference between dot (a.k.a. full stop, period) and comma in Perl Single and double quotes strings in Perl - what is the difference? DNA to Amino Acid - a sample Perl script How much has Perl (and other languages) changed? Looking ahead and behind in Regular Expressions - double matching Object Orientation in an hour and other Perl Lectures Arrays v Lists - what is the difference, why use one or the other Further more advanced Perl examples Unpacking a Perl string into a list Teaching examples in Perl - third and final part Binary data handling with unpack in Perl Want to do a big batch edit? Nothing beats Perl! Making variables persistant, pretending a database is a variable and other Perl tricks Running a piece of code is like drinking a pint of beer This article Finding words and work boundaries (MySQL, Perl, PHP) Equality and looks like tests - Perl Handling Binary data (.gif file example) in Perl Ignore case in Regular Expression Regular expressions made easy - building from components Substitute operator / modifiers in Perl Commenting a Perl Regular Expression Perl, the substitute operator s Matching within multiline strings, and ignoring case in regular expressions C++ and Perl - why did they do it THAT way? Coloured text in a terminal from Perl Don't expose your regular expressions Storing a regular expression in a perl variable Perl Regular Expressions - finding the position and length of the match Remember to process blank lines Commenting Perl regular expressions