I have a very long substitution string in a Perl regular expression, which I would like to split over multiple lines. What is the syntax to split long substitution strings over multiple lines? I have tried the following, without success (the whitespace isn't ignored):
$var =~ s/(some)(thing)/A_very_long_string_with_many_characters_that_should_be_split_over_
multiple_lines_but_can't_be_for_whatever_reason._The_matching_groups_are_"$1"_and_"$2"/x;
My regex replacement string is much more interleaved that this contrived example, so I can't just put the large literal portion into a variable. In other words, the following is not helpful in my case:
my $prefix = 'A_very_long_string_with_many_characters_that_should_be_split_over_' .
'multiple_lines_but_can\'t_be_for_whatever_reason._The_matching_groups_are_';
$var =~ s/(some)(thing)/$prefix"$1"_and_"$2"/;
Does anyone know of a way to split substitution strings over multiple lines?
The replacement component of the s///
operator is not a regex, and is not affected by the /x
modifier. It is a string, similar to the contents of a double quoted string or backticks. Your only options are to construct it in a variable like you alluded to, or apply the /e
modifier, which turns the replacement into a perl expression, in which you could construct the string, but you need to quote the parts since bareword strings are not allowed under strict.
use strict;
use warnings;
$var =~ s/(some)(thing)/'A_very_long_string_with_many_characters_that_should_be_split_over_' .
qq{multiple_lines_but_can't_be_for_whatever_reason._The_matching_groups_are_"$1"_and_"$2"}/e;