Lesson 8 | Perl e modifier |
Objective | e modifier is placed at the end of the substitution expression. |
Perl e modifier
The e
modifier is used when you want the right-hand side of the substitution expression to be evaluated as code, instead of being treated as a string.
For example,
s/$(\w+)//e;
If there is a need to substitute the substring with a replacement string which is a regular expression to be evaluated,
the 'e' modifier is used. The 'e' modifier is placed at the end of the substitution expression.
s/To_be_replaced/Regular_Expression/e;
That will replace anything that looks like a Perl scalar variable with the contents of that variable. This is extremely useful for embedding values in a text file. Let us look at this example more closely:
Perl e-substitution Modifier
Converting number formats
Sometimes the unstructured data that you receive will contain numerical data and the only changes that you will want to make are to reformat the numbers into a standardized format. This breaks down into two processes.
- First you have to recognize the numbers you are interested in, then
- you need to reformat them.
Recognizing numbers
How do you recognize a number?
The answer depends on what sort of numbers you are dealing with. Are they integers or floating points?
Can they be negative? Do you accept exponential notation (such as 1E6 for 1 Ã 106)?
When you answer these questions, you can build a regular expression that matches the particular type of number that you need to process.
To match natural numbers (i.e., positive integers) you can use a simple regular expression such as:
/\d+/
To match integers (with optional +/- signs) use
/[-+]?\d+/
To match a floating point number use
/[-+]?(\d+(\.\d*)?|\.\d+)/
To match a number that can optionally be in exponential notation, use
/[-+]?(?=\d|\.\d)\d*(\.\d*)?([eE]([-+]?\d+))?/
As these become rather complex, it might be a suitable time to consider using Perl's precompiled regular expression feature and creating your number-matching regular expressions in advance. You can do something like this:
my $num_re = qr/[-+]?(?=\d|\.\d)\d*(\.\d*)?([eE]([-+]?\d+))?/;
my @nums;
while ($data =~ /$num_re/g) {
push @nums, $1;
}
to print out a list of all of the numbers in $data.
If you have a function, reformat, that will change the numbers into your preferred format then you can use code like this:
$data =~ s/$num_re/reformat($1)/ge;
which makes use, once more, of the e modifier to execute the replacement string before using it.
Ad Regular Expressions