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

  • SEARCH
  • Home
  • 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 738715
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 14, 20262026-05-14T07:52:06+00:00 2026-05-14T07:52:06+00:00

I got a headache looking for this: How do you use s/// in an

  • 0

I got a headache looking for this:
How do you use s/// in an expression as opposed to an assignment. To clarify what I mean, I’m looking for a perl equivalent of python’s re.sub(…) when used in the following context:

newstring = re.sub('ab', 'cd', oldstring)

The only way I know how to do this in perl so far is:

$oldstring =~ s/ab/cd/;
$newstring = $oldstring;

Note the extra assignment.

  • 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-14T07:52:06+00:00Added an answer on May 14, 2026 at 7:52 am

    You seem to have a misconception about how =~ works. =~ is a binding operator that associates a variable with a regexp operator. It does not do any assignment.

    The regexp operators all work by default with the topic variable $_, so s/foo/bar/; is the same as $_ =~ s/foo/bar/;. No assignment occurs. The topic variable is transformed.

    The case is analogous when operating on any other variable. $var =~ s/foo/bar/; transforms $var by replacing the first instance of foo with bar. No assignment occurs.

    The best advice I can give you is to write Python in Python and Perl in Perl. Don’t expect the two languages to be the same.

    You could do like DVK suggests and write a subroutine that will reproduce the substitution behavior you are used to.

    Or you could try some idiomatic Perl. Based on your expressed desire to apply multiple transformations in one line, I’ve provided a couple examples you might find useful.

    Here I use a for loop over one item to topicalize $var and apply many hard-coded transformations:

    for( $var ) {
        s/foo/bar/;
        s/fizz/buzz/;
        s/whop/bop-a-loo-bop/;
        s/parkay/butter/;
        s/cow/burger/;
    }
    

    Or maybe you need to apply a variable group of transforms. I define a subroutine to loop over a list of array references that define old/new transformation pairs. This example takes advantage of Perl’s list oriented argument processing to handle any number of transformations.

    my $foo = transform(
        'abcd' =>
        [ 'a',  'b'    ], 
        [ 'bb', 'c'    ],
        [ 'cc', 'd'    ],
        [ 'dd', 'DONE' ],
    );
    
    sub transform {
        my $var = shift;
        for (@_ ) {
            my ($old, $new) = @$_;
            $var =~ s/$old/$new/;
        }
    
        return $var;
    }
    

    Finally a bit of messing about to provide a version of transform that modifies its first argument:

    my $foo = 'abcd';
    
    transform_in_place(
        $foo =>
        [ 'a',  'b'    ], 
        [ 'bb', 'c'    ],
        [ 'cc', 'd'    ],
        [ 'dd', 'DONE' ],
    );
    
    print "$foo\n";
    
    sub transform_in_place {
        for my $i (1..$#_ ) {
            my ($old, $new) = @{$_[$i]};
            $_[0] =~ s/$old/$new/;
        }
    }
    

    For my own project I’d probably use one of the first two options depending on the needs of the particular problem.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Simple question but I got a headache to solve this game. Example regex. [a-zA-Z0-9\s]
Hey all. Ive got a headache trying to get my head around this. I
I've got some code to increase/decrease font size. This is giving me a headache
Got a real headache at this stage on a Friday! I'm trying to add
Alright, this is giving me headache for over an hour. I've got several ScrollViews
Got this error message while trying to load view: The model item passed into
Got a question for some jQuery wizard who might stumble upon this. When I
got a problem, can't get my head around this jquery and would appreciate your
got some problems. Built an applet that has to be used step-by-step. After each
I got headache how to make my fluid content will float to right. left

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.