in reply to Re: passing newline in string argument
in thread passing newline in string argument
But what if someone actually wants to provide the two characters \n?
Special_K, just for completeness, here's one common way - however, as we both said, this is only a workaround with its own caveats, and the problem should be fixed in the shell instead!
sub interpolate_nl { my $str = shift; $str =~ s/(?<!\\)(?:\\\\)*\K\\n/\n/g; $str =~ s/\\\\/\\/g; return $str; # on 5.14 and above, the above can be compacted to: #shift =~ s/(?<!\\)(?:\\\\)*\K\\n/\n/gr =~ s/\\\\/\\/gr } use Test::More tests=>9; is interpolate_nl("a\\b"), "a\\b"; # * is interpolate_nl("a\\nb"), "a\nb"; is interpolate_nl("a\\\\b"), "a\\b"; is interpolate_nl("a\\\\nb"), "a\\nb"; is interpolate_nl("a\\\\\\b"), "a\\\\b"; # * is interpolate_nl("a\\\\\\nb"), "a\\\nb"; is interpolate_nl("a\\\\\\\\b"), "a\\\\b"; is interpolate_nl("a\\\\\\\\nb"), "a\\\\nb"; is interpolate_nl("a\\\\\\\\\\nb"), "a\\\\\nb"; # * \b and all other escapes are unhandled and ignored!
(Bonus: even more rope to shoot yourself in the foot: String::Unescape)
|
|---|