Search code examples
perlterminaltext-formatting

How to write a single parsed line into paragraph?


I extracted a paragraph using XML::Simple parser. I got it parsed in a single line. Here is the code and I extracted abstract.

use LWP::Simple;
use XML::Simple;
use Data::Dumper;

open (FH, ">:utf8","xmlparsed2.txt");
my $db1 = "pubmed";
my $q = 16404398;
my $xml = new XML::Simple;
$urlxml = "http://eutils.ncbi.nlm.nih.gov/entrez/eutils/efetch.fcgi?db=$db1&id=$q&retmode=xml&rettype=abstract";
$dataxml = get($urlxml);
$data = $xml->XMLin("$dataxml", ForceArray => [qw( MeshHeading Author AbstractText )], ForceContent=>1);
print FH Dumper($data);
print FH "Abstract: ".join "\n", map {join ":",($_->{NlmCategory},$_->{content})} @{$data->{PubmedArticle}->{MedlineCitation}->{Article}->{Abstract}->{AbstractText}};

My question is: can it be parsed to fit the window and be in a paragraph rather than in a single line?


Solution

  • My question is can it be parsed to fit the window

    So question 1, how to determine the size of terminal window

    The answer is use

    use Term::Size;
    
    ($columns, $rows) = Term::Size::chars *STDOUT{IO};
    ($x, $y) = Term::Size::pixels;
    

    and be in paragraph rather than in a single line?

    for question 2, use core module Text::Wrap, or Text::Fold ...