tags:

views:

185

answers:

4

Hello!

 #!/usr/bin/env perl
 use warnings; 
 use strict;

 my $text = 'hello ' x 30;

 printf "%-20s : %s\n", 'very important text', $text;

the output of this script looks more ore less like this:

very important text      : hello hello hello  hello   
hello hello hello hello hello hello hello hello  
hello hello hello hello hello hello hello hello  
...   

but I would like an output like this:

very important text: hello hello hello hello  
                     hello hello hello hello  
                     hello hello hello hello  
                     ... 

I forgot to mention: the text should have an open end in the sense that the right end of the textlines should align corresponding to the size of the terminal.

How could I change my script to reach my goal?

A: 

While I am not sure from your question precisely what format you would like your output in; I can tell you that the key to pretty output in the Perl language is to use formats. Here is a primer on how to use them to achieve pretty much any output formatting you would like.

http://www.webreference.com/programming/perl/format/

Michelle Six
I've had a glance at Perl6::Form but please see my comment to karthi-27
sid_com
+3  A: 

Try this ,

use strict;
use warnings;

 my $text = 'hello ' x 30;

 $text=~s/((\b.+?\b){8})/$1\n                       /gs;
 printf "%-20s : %s\n", 'very important text', $text;
karthi_ms
I forgot to mention: the text should have an open end in the sense that the right end of the text-lines should align corresponding to the size of the terminal.
sid_com
+5  A: 

You can use Text::Wrap:

use strict;
use Text::Wrap;

my $text = "hello " x 30;
my $init = ' ' x 20;
$Text::Wrap::columns = 80;

print wrap ( '', $init,  'very important text : ' . $text );
justintime
The right end of the text-lines should align corresponding to the width of the terminal. With Text::Wrap I have a fixed width of columns.
sid_com
@sid_com: You can use Term::Size (http://search.cpan.org/~timpx/Term-Size-0.2/Size.pm) to get the terminal's width in columns and set $Text::Wrap::columns accordingly.
Dave Sherohman
accepted this answer in liaison with the Dave Sherohman comment.
sid_com
A: 
#!/usr/bin/env perl
use warnings; 
use strict;
use 5.010;
use Text::Wrap;
use Term::Size;

my $text = 'hello ' x 30;
my $init = ' ' x 22;
my( $columns, $rows ) = Term::Size::chars *STDOUT{IO};
$Text::Wrap::columns = $columns;

say wrap ( '', $init,  'very important text : ' . $text );
sid_com