Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

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:

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

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", '~';
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading