This is a very useful function that can be used to do many unexpected things since you modify arguments when you modify $_. Also see map.
This is a very useful operator, so I will go into it somewhat.
If used like this:
You can modify the substitution operator by putting letters after the final slash. For example, putting a ``g'' after the last slash will make the substitution work for every occurance in the text. (Not just the first one.)
You could do a pattern match without case sensitivity by using ``i'' like this:
You can also use the $n variables, if you wish:
There are a couple of other modifiers that you might find useful:
| e |
This causes the replacement text to be evaluated like it were an expression. This is very useful for translating text encodings. For example, in HTML, URLs have a restricted syntax. If you wish to send certain characters, like space and slash, as text they need to be translated to a %hex notation. Here is a single line of perl that will decode such encodings: In this case, everything in $decodeme that matches a percent followed by two hex characters will be handed to the expression on the right. In this case, hex interprets the two characters together as a hex number into the machine representation of the number. This number is handed to chr, which turns interprets that number as an ASCII value and turns it into a character. This character replaces the percent-two-hex text that matched and the next match is sought. |
|---|---|
| m |
Treat the string as if it were multiple lines. Normally, it is assumed that the end of the line is also the end of the string, so $ always matches the end of the string. This changes pattern matches to seek out a \n character for the end of a line to match $. |
| s |
Treat the string as if it were a single line. This is the default behavior. |
| o |
Complie pattern once. Okay, I have not told you everything about regular expressions. You see, they are even more complicated than they seem. You type them in and perl has to compile them into some internal form with which it can operate. Normally, perl can tell that evey time it encounters your regular expression, it is going to be the same. When it knows this, it compiles the regular expression and keeps that compiled version around to use again. Sometimes, perl doesn't know, like when you have a variable in the regular expression. In this case, perl will recompile the regular expression every time it is excountered. You can use ``o'' to force perl to compile once and never again. This is intended as a way for you to help perl run faster. Unless you are doing something that is using regular expressions in a very time critical way, you should not use this. Even if you think you need it, make sure you are using it right and test your code! |
| x |
Use extended regular expressions. Don't ask. |