I have a string having a repeating pattern, and I want to replace each occurrence of such pattern with another string. The replacement string is formed by concatenating a set of other strings. An example is as below. I first tried concatenating using the . operator as shown.
But the output contained the dots themselves, so Perl does not treat it as an operator, but a literal ..
#!/usr/bin/perl
use warnings;
use strict;
my $start = 'not-so-';
my $end = '-but-a-little-bad';
my $string = 'I am a good boy. Infact I am a very good boy';
print "Before: $string\n";
>>>> $string =~ s/(good)/$start.$1.$end/g;
print "Later : $string\n";
So I removed the .s, and my statement became $string =~ s/(good)/$start$1$end/g;, and the output is as per expectation. But, I feel a statement like this might cause maintenance issues later.
My question: Is there a better way of concatenating strings other than this?
You notation
is good. If you prefer, you can also write
but it’s totally equivalent.