Take the 2-minute tour ×

In Perl, the operator s/ is used to replace parts of a string. Now s/ will alter its parameter (the string) in place. I would however like to replace parts of a string befor printing it, as in

print "bla: ", replace("a","b",$myvar),"\n";

Is there such replace function in Perl, or some other way to do it? s/ will not work directly in this case, and I'd like to avoid using a helper variable. Is there some way to do this in-line?

share|improve this question
add comment

5 Answers

up vote 17 down vote accepted


require 5.013002;
print "bla: ", $myvar =~ s/a/b/r, "\n";

See perl5132delta.

share|improve this answer
wow, new option, nice –  unbeli Aug 9 '10 at 13:31
Well whaddya know! Very handy. Had I known about this I would have used it about 80% of the time! –  j_random_hacker Aug 9 '10 at 13:41
Ah, now I see it's actually very recent... Perl 5.13.2. So it's still of interest to look for workarounds for older versions. –  j_random_hacker Aug 9 '10 at 13:47
It's interesting to know about that but unfortunately it doesn't really answer the question. –  user181548 Aug 10 '10 at 4:22
add comment

No, there's no such function in perl. A possible solution is to use a do() block, like this:

my $str = do { (my $tmp = $orig_str) =~ s/a/b/; $tmp };

The temporarily lexical variable $tmp exists only in this block.

share|improve this answer
Was added in commit 4f4d7508b0c2c114e5f52420e0e87a853c5f642a. –  daxim Aug 9 '10 at 13:30
Interesting, didn't know about do(). For my purpose it's a bit messy, though :-/. –  sleske Aug 9 '10 at 15:23
add comment
print "bla: ", $_, "\n" if ($_ = $myvar) =~ s/a/b/g or 1;
share|improve this answer
Sorry, I don't understand. Why the "if"? –  sleske Aug 9 '10 at 15:30
@sleske : What if $myvar doesn't contain an a. In that case, $_ would be undefined. It's also why the or 1 has been added in this answer, to force the print to go ahead even if there isn't anything to substitute. Not terribly readable IMO. –  Zaid Aug 9 '10 at 15:50
@Zaid: ? $_ would not be undefined; it was just set to $myvar. This is better done as ($_ = $myvar) =~ s/a/b/g, print "bla: ", $_, "\n" –  ysth Aug 9 '10 at 16:21
@ysth : Ack. You're right. The or 1 threw me off. So is the or 1 just contributing to line noise? –  Zaid Aug 9 '10 at 16:26
@Zaid: no, the or 1 makes sure the if is true so the print even happens - not necessary using the comma operator instead –  ysth Aug 9 '10 at 19:26
show 1 more comment

If you really want, you can make your own, but I wouldn't because you have much more functionality with s/// ... you could build that functionality into your function, but why recreate something that already exists?

#!/usr/bin/perl -w    

use strict;     


   sub main{    
      my $foo = "blahblahblah";          
      print '$foo: ' , replace("lah","ar",$foo) , "\n";  #$foo: barbarbar


   sub replace {
      my ($from,$to,$string) = @_;
      $string =~s/$from/$to/ig;                          #case-insensitive/global (all occurrences)

      return $string;
share|improve this answer
add comment
print "bla: ", $myvar =~ tr{a}{b},"\n";
share|improve this answer
add comment

Your Answer


