Search code examples
perlformat

Perl: how to format a string containing a tilde character "~"


I have run into an issue where a perl script we use to parse a text file is omitting lines containing the tilde (~) character, and I can't figure out why.

The sample below illustrates what I mean:

#!/usr/bin/perl
use warnings;

formline "  testing1\n";
formline " ~testing2\n";
formline "  testing3\n";

my $body_text = $^A;
$^A = "";

print $body_text

The output of this example is:

testing1
testing3

The line containing the tilde is dropped entirely from the accumulator. This happens whether there is any text preceding the character or not.

Is there any way to print the line with the tilde treated as a literal part of the string?


Solution

  • ~ is special in forms (see perlform) and there's no way to escape it. But you can create a field for it and populate it with a tilde:

    formline " \@testing2\n", '~';