***** infoCopter.com/perl *****
Wrap a string or text
#!/usr/bin/perl -w
use strict;
$| = 1;
my $long_text =
qq~----+----1----+----2----+----3abcdefghijklmnopqrstuvwxyABCDEFGHIJKLMNOPQRSTUVWXYZ~;
$long_text .= qq~more and more text here~;
&mylog( msg => $long_text, wrap => 30 );
sub mylog (%) {
my %args = @_;
$args{'wrap'} = 70 if $args{'wrap'} < 1;
my $len = length($args{'msg'});
my $i = 0;
while($len > $args{'wrap'}) {
print "->\t", substr($args{'msg'}, $i, $args{'wrap'}), "\n";
$len -= $args{'wrap'};
$i += $args{'wrap'};
}
print "->\t", substr($args{'msg'}, $i), "\n";
}
Output
# ./test4.pl
-> ----+----1----+----2----+----3
-> abcdefghijklmnopqrstuvwxyABCDE
-> FGHIJKLMNOPQRSTUVWXYZmore and
-> more text here
Generic version with indents
#!/usr/bin/perl -w
use strict;
print &wrap('----+----1----+----2----+----3--');
sub wrap ($) {
my $logtext = $_[0];
my $wrap = 10;
my $indent = 0; my $indent_tab = '';
my $len = length($logtext);
my $wrapped_string = '';
my $i = 0;
my $continue = 0;
my $inden = '';
while($len > $wrap) {
$inden = $continue ? $indent_tab : '';
my $j = $continue ? $indent : 0;
$wrapped_string .= $inden;
$wrapped_string .= substr($logtext, $i, $wrap - $j) . "\n";
$len -= ($wrap - $j);
$i += $wrap - $j;
$continue = 1;
}
if ($i > 0 or $len < $wrap) {
$inden = $continue ? $indent_tab : '';
my $j = $continue ? $indent : 0;
$wrapped_string .= $inden;
$wrapped_string .= substr($logtext, $i) . "\n";
}
$wrapped_string;
}
|