U
usenet
I have a variable which contains a text string. I want to print a
modified verion of the string without changing the variable. Consider
this code, which simply reverses each occurance of the word "gas":
my $word = 'gas';
my $string = "Jumping Jack Flash, It's a gas, gas, gas.";
(my $temp = $string) =~ s/$word/reverse $word/ieg;
print "$temp\n";
That works, but it uses an intermediate variable ($temp). I dislike
intermediate variables.
I could create a subroutine, but that's even uglier.
Is there an elegant way to print the modified version of the string
without an intermediate variable or a subroutine?
modified verion of the string without changing the variable. Consider
this code, which simply reverses each occurance of the word "gas":
my $word = 'gas';
my $string = "Jumping Jack Flash, It's a gas, gas, gas.";
(my $temp = $string) =~ s/$word/reverse $word/ieg;
print "$temp\n";
That works, but it uses an intermediate variable ($temp). I dislike
intermediate variables.
I could create a subroutine, but that's even uglier.
Is there an elegant way to print the modified version of the string
without an intermediate variable or a subroutine?