views:

122

answers:

4

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?

+1  A: 

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.

eugene y
Was added in [commit 4f4d7508b0c2c114e5f52420e0e87a853c5f642a](http://perl5.git.perl.org/perl.git/commit/4f4d7508b0c2c114e5f52420e0e87a853c5f642a).
daxim
Interesting, didn't know about `do()`. For my purpose it's a bit messy, though :-/.
sleske
+11  A: 

Untested:

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

See perl5132delta.

daxim
wow, new option, nice
unbeli
Well whaddya know! Very handy. Had I known about this I would have used it about 80% of the time!
j_random_hacker
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
It's interesting to know about that but unfortunately it doesn't really answer the question.
Kinopiko
A: 
print "bla: ", $_, "\n" if ($_ = $myvar) =~ s/a/b/g or 1;
unbeli
Sorry, I don't understand. Why the "if"?
sleske
@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
@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
@ysth : Ack. You're right. The `or 1` threw me off. So is the `or 1` just contributing to line noise?
Zaid
@Zaid: no, the or 1 makes sure the if is true so the print even happens - not necessary using the comma operator instead
ysth
@Zaid: if there is nothing replaces the `s///g` returns `0`, which equals `false` and so nothing is printed. The `or 1` or `|| 1` ensures that there will always be something printed (the original string with no replacement).
vol7ron
A: 

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;     

   main();   

   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;
   }
vol7ron