Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 4273564
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 21, 20262026-05-21T07:43:47+00:00 2026-05-21T07:43:47+00:00

Perl 5.8 Improvements for fairly straightforward string substitutions, in an existing Perl script. The

  • 0

Perl 5.8

Improvements for fairly straightforward string substitutions, in an existing Perl script.
The intent of the code is clear, and the code is working.

For a given string, replace every occurrence of a TAB, LF or CR character with a single space, and replace every occurrence of a double quote with two double quotes. Here’s a snippet from the 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;

# escape all double quote characters by replacing with two double quotes
$val01  =~s/"/""/g;
$val02  =~s/"/""/g;
$val03  =~s/"/""/g;

Question:Is there a better way to perform these string manipulations?

By “better way”, I mean to perform them more efficiently, avoiding use of regular expressions (possibly using tr/// to replace the tab, newline and lf characters), or possibly using using the (qr//) to avoid recompilation.

NOTE: I’ve considered moving the string manipulation operations to a subroutine, to reduce the repetition of the regular expressions.

NOTE: This code works, it isn’t really broken. I just want to know if there is a more appropriate coding convention.

NOTE: These operations are performed in a loop, a large number (>10000) of iterations.

NOTE: This script currently executes under perl v5.8.8. (The script has a require 5.6.0, but this can be changed to require 5.8.8. (Installing a later version of Perl is not currently 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)
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-21T07:43:48+00:00Added an answer on May 21, 2026 at 7:43 am

    Your existing solution looks fine to me.

    As for avoiding recompilation, you don’t need to worry about that. Perl’s regular expressions are compiled only once as it is, unless they contain interpolated expressions, which yours don’t.

    For the sake of completeness, I should mention that even if interpolated expressions are present, you can tell Perl to compile the regex once only by supplying the /o flag.

    $var =~ s/foo/bar/;    # compiles once
    $var =~ s/$foo/bar/;   # compiles each time
    $var =~ s/$foo/bar/o;  # compiles once, using the value $foo has
                           # the first time the expression is evaluated
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Perl is really good for writing the kind of string/file parsing programs that I
In Perl, what is a good way to perform a replacement on a string
Perl noob here - i have the following script if(substr($pc, 3,1)!= ){ $newpc =
Perl code fragment: my $export = $doc; $export =~ s:\.odt:\.pdf:; How would this be
The Perl script that contains a Unix command to grep the ethernet NICs cannot
In perl, if I have a string $str and a pattern saved in $pat
I wrote this sample code to check whether integer or string index is better
My problem is: I have a perl script which uses lot of memory (expected
Perl: $string =~ s/[áàâã]/a/gi; #This line always prepends an a $string =~ s/[éèêë]/e/gi; $string
Perl is continuing to surprise me. I have a code which takes an input

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.