in reply to Re: Change utility; code optimization
in thread Change utility; code optimization
After the value of $changefrom is interpolated, the regular expression engine compiles the regex, sees "\n", and compiles it to match a newline. (If $changeto instead contained a literal newline, the result would be the same, because a literal newline in a regex matches a literal newline.)
On the replacement side, however, once the value of $changeto is interpolated, there is no second pass over the string to turn "\n" into a newline.
One solution is to code the extra pass over $changeto yourself, as in:
Please note that that simple example will change \\n to \<newline>.$changeto =~ s/\\n/\n/g; $file_contents =~ s/$changefrom/$changeto/gi;
Of course, you then have to consider which escape sequences you want to allow. \t and \r? How about \040, \x20, or \cD? That's up to you. :)
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: Re: Re: Change utility; code optimization
by gryphon (Abbot) on Feb 13, 2001 at 04:16 UTC | |
by chipmunk (Parson) on Feb 13, 2001 at 10:02 UTC |