Perl 5.8
Improvements for fairly simple string substitutions in an existing Perl script.
The purpose of the code is clear, and the code works.
For a given string, replace each occurrence of the TAB, LF, or CR character with a single space, and replace each occurrence of the double quote with two double quotation marks. Here is a snippet from existing code:
# replace all tab, newline and return characters with single space $val01 =~s/[\t\n\r]/ /g; $val02 =~s/[\t\n\r]/ /g; $val03 =~s/[\t\n\r]/ /g;
Question: Is there a better way to perform these string manipulations?
In a βbetter way,β I would like to execute them more efficiently, avoiding the use of regular expressions (possibly using tr/// to replace the tab, newline, and lf characters) or perhaps using ( qr// ) to avoid recompiling.
NOTE. I considered moving string manipulation operations into a subroutine to reduce regexp.
NOTE. This code works, it really is not broken. I just want to know if a more suitable coding convention exists.
NOTE. These operations are performed in a loop, a large number (> 10000) iterations.
NOTE. This script is currently running under perl v5.8.8. (The script has require 5.6.0 , but it can be changed to require 5.8.8 . (Installing a later version of Perl is currently not an option on the production server.)
> perl -v This is perl, v5.8.8 built for sun4-solaris-thread-multi (with 33 registered patches, see perl -V for more detail)