jesus has asked for the wisdom of the Perl Monks concerning the following question:

Hi, I really need your help for my assignment. I got 4 variables extracted from RSS, for example:

$title = "Shooter of US teen appears in court"; $author = "Stephanie Kennedy"; $date = "Fri, 13 Apr 2012 06:00:16"; $content = "A Hispanic neighbourhood watch volunteer, charged with sec +ond-degree murder over the shooting death of an unarmed black teenage +r in the United States, has appeared briefly in court for the first t +ime.";

And I'd like the output (just on the terminal, not into a file) to be like this:

--------------------------------------------------------------------- | TITLE | Shooter of US teen appears in court | --------------------------------------------------------------------- | AUTHOR | Stephanie Kennedy | --------------------------------------------------------------------- | DATE | Fri, 13 Apr 2012 06:00:16 | --------------------------------------------------------------------- | CONTENT | A Hispanic neighbourhood watch volunteer, charged with | | | second-degree murder over the shooting death of an | | | unarmed black teenager in the United States, has | | | appeared briefly in court for the first time. | ---------------------------------------------------------------------
How can I do that? Please, help me! Thanks!

Replies are listed 'Best First'.
Re: Neat format and word wrap
by GrandFather (Saint) on Apr 13, 2012 at 00:08 UTC

    What have you tried? What problem are you having? printf is often used to print strings in fixed width fields. You can use a regular expression (see perlretut) with the g (global) switch to break a long string up into shorter strings.

    True laziness is hard work

      Have to admit... with his/her handle, I was sorely tempted to reply "Ask your father..."

        Shouldn't that be "Ask your Father..."?
Re: Neat format and word wrap
by clueless newbie (Curate) on Apr 13, 2012 at 14:41 UTC
    Or use Text::Table
    #! use strict; use warnings; use Text::Table; my $title = "Shooter of US teen appears in court"; my $author = "Stephanie Kennedy"; my $date = "Fri, 13 Apr 2012 06:00:16"; my $content = "A Hispanic neighbourhood watch volunteer, charged with +second-degree murder over the shooting death of an unarmed black teen +ager in the United States, has appeared briefly in court for the firs +t time."; # "Cheap" wrapper $content=~ s/(.{40}[^\s]*)\s+/$1\n/g; my $tb = Text::Table->new(\"| ","",\" | ","",\" |"); $tb->load( [ "TITLE",$title ], [ "AUTHOR",$author ], [ "DATE",$date ], [ "CONTENT",$content ], ); my $rule=$tb->rule('-', '+' ); for my $line ($tb->table) { print $rule unless ($line =~ /^\| +\|/); print $line; }; print $rule;
    to get something like
    +---------+---------------------------------------------+ | TITLE | Shooter of US teen appears in court | +---------+---------------------------------------------+ | AUTHOR | Stephanie Kennedy | +---------+---------------------------------------------+ | DATE | Fri, 13 Apr 2012 06:00:16 | +---------+---------------------------------------------+ | CONTENT | A Hispanic neighbourhood watch volunteer, | | | charged with second-degree murder over the | | | shooting death of an unarmed black teenager | | | in the United States, has appeared briefly | | | in court for the first time. | +---------+---------------------------------------------+
Re: Neat format and word wrap
by Anonymous Monk on Apr 13, 2012 at 02:30 UTC
Re: Neat format and word wrap
by MidLifeXis (Monsignor) on Apr 13, 2012 at 12:22 UTC

    See perlform for another way to do it.

    --MidLifeXis

Re: Neat format and word wrap
by trizen (Hermit) on Apr 13, 2012 at 14:29 UTC
    use 5.010; use Text::Wrap qw(wrap $columns); our $columns = 57; # change this my $hbar = q{-} x ($columns + 12); # DON'T change this printf "%s\n| TITLE |%-${columns}s| %s\n| AUTHOR |%-${columns}s| %s\n| DATE |%-${columns}s| %s\n| CONTENT |%-${columns}s| %s\n", map({ if (/\n/) { s{^(.*)\K\n} { sprintf("%*s\n|", $columns - length($1), '|'). q{ } x (length($hbar)-$columns-3). q{| } }egm; s{\n\|\h+\|(.*)\K\z} { sprintf '%*s', $columns - length($1), q{ } }e; s{\A(.*)\K\|$}{length($1)<$columns ? q{ |} : q{|}}em; } $_ } map({$hbar, q{ } . wrap(q{}, q{}, $_)} $title, $author, $date, $content),$hbar);
Re: Neat format and word wrap
by rovf (Priest) on Apr 13, 2012 at 15:01 UTC
Re: Neat format and word wrap
by Anonymous Monk on Apr 13, 2012 at 17:19 UTC
    format STDOUT = --------------------------------------------------------------------- | TITLE | ^<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<< |~ +~ $title --------------------------------------------------------------------- | AUTHOR | ^<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<< |~ +~ $author --------------------------------------------------------------------- | DATE | ^<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<< |~ +~ $date --------------------------------------------------------------------- | CONTENT | ^<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<< |~ +~ $content --------------------------------------------------------------------- . write;