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 944655
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 15, 20262026-05-15T22:37:40+00:00 2026-05-15T22:37:40+00:00

Normally if you wish to change a variable with regex you do this: $string

  • 0

Normally if you wish to change a variable with regex you do this:

$string =~ s/matchCase/changeCase/; 

But is there a way to simply do the replace inline without setting it back to the variable?

I wish to use it in something like this:

my $name="jason";
print "Your name without spaces is: " $name => (/\s+/''/g);

Something like that, kind of like the preg_replace function in PHP.

  • 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-15T22:37:41+00:00Added an answer on May 15, 2026 at 10:37 pm

    Revised for Perl 5.14.

    Since 5.14, with the /r flag to return the substitution, you can do this:

    print "Your name without spaces is: [", do { $name =~ s/\s+//gr; }
        , "]\n";
    

    You can use map and a lexical variable.

    my $name=" jason ";
    
    print "Your name without spaces is: ["
        , ( map { my $a = $_; $a =~ s/\s+//g; $a } ( $name ))
        , "]\n";
    

    Now, you have to use a lexical because $_ will alias and thus modify your variable.

    The output is

    Your name without spaces is: [jason]
    # but: $name still ' jason '
    

    Admittedly do will work just as well (and perhaps better)

    print "Your name without spaces is: ["
        , do { my ( $a = $name ) =~ s/\s+//g; $a }
        , "]\n";
    

    But the lexical copying is still there. The assignment within in the my is an abbreviation that some people prefer (not me).

    For this idiom, I have developed an operator I call filter:

    sub filter (&@) { 
        my $block = shift;
        if ( wantarray ) { 
            return map { &$block; $_ } @_ ? @_ : $_;
        }
        else { 
           local $_ = shift || $_;
           $block->( $_ );
           return $_;
        }
    }
    

    And you call it like so:

    print "Your name without spaces is: [", ( filter { s/\s+//g } $name )
        , "]\n";
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Normally we use window.location.href=/index.php?querystring; in javascript. Is there a way to send the querystring
Normally this would be something like: glutAddMenuEntry(-, <opid>); But it doesn't work. After looking
I would normally do this sort of thing in jQuery, but this will require
Normally when I want to change the connection for a Query Editor Window in
Normally, if I want to start a new activity I can use StartActivity(typeof(foo)); This
Normally the XML files I have to parse are like this: <row id=1> <title>widget<title>
I have an existing system that I do not wish to change where I
Normally, if I wish to stop a default event in mootools I can do
Normally I do this for all my user inputs both strings and numerical inputs:
I realize you can use the upsize wizard in access to convert this normally

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.