in reply to Inline substitution regex
[ In the future, post the code you actually ran rather than making stuff up. Your code does NOT produce the output you specified. In fact, it doesn't run at all. ]
I don't understand why, but it's a common misconception that operators return their left-hand argument. Why you expect the following to print 3?
print( 3 + 4 );
Then why would expect the following to print $text?
print( $text =~ s/this/that/ );
Operators are documented in perlop. s/// is no exception. Search for s/PATTERN/REPLACEMENT/ in the "Regexp Quote-Like Operators" section. It is documented to return the number of substitutions made.
What's the correct way to do it inline?
I'd say it's not correct to do it inline
$text =~ s/this/that/; print $text;
You can manage with the following, but you're complicating your code again.
print( do { $text =~ s/this/that/; $text } );
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re^2: Inline substitution regex
by bradcan (Novice) on Sep 29, 2009 at 00:47 UTC | |
by ikegami (Patriarch) on Sep 29, 2009 at 05:14 UTC | |
by vitoco (Hermit) on Sep 29, 2009 at 13:26 UTC | |
by ikegami (Patriarch) on Sep 29, 2009 at 16:31 UTC | |
by vitoco (Hermit) on Sep 29, 2009 at 18:20 UTC | |
| |
|
Re^2: Inline substitution regex
by LanX (Saint) on Sep 29, 2009 at 14:35 UTC | |
by ikegami (Patriarch) on Sep 29, 2009 at 16:12 UTC |