package Pitonyak::StringUtil; #************************************************************ =head1 NAME Pitonyak::StringUtil - General string utilities. String module created by Andrew Pitonyak for his personal use to format strings for pretty output. =head1 SYNOPSIS The subroutines in this module are intended to be used as methods, not as calls on an object. Therefore, you should call all methods directly. Many of the subroutines modify the arguments. For example, when you trim space from a string, the argument is modified. Methods are not available unless they are specifically imported. So, to use the C<< smart_printer_default >> routine, you must first import it. For example, =begin html
use Pitonyak::StringUtil qw(smart_printer_default);
my %hash2 = ('a' => 'A', 'b' => 'B');
my @strings = ('123456789', '123', \%hash2);
my %hash1 = ('one' => 1, 'two' => 2, 'ary' => \@strings);
print smart_printer_default(\%hash1);
=end html =head1 DESCRIPTION =cut #************************************************************ require Exporter; use vars qw($VERSION @ISA @EXPORT @EXPORT_OK); $VERSION = '1.04'; @ISA = qw(Exporter); @EXPORT = qw( ); @EXPORT_OK = qw( array_width center_fmt compact_space delimited_values substr_no_space hash_key_width hash_val_width left_fmt num_int_digits num_with_leading_zeros trans_blank trim_fmt trim_space right_fmt smart_printer smart_printer_default ); use Carp; use strict; #************************************************************ =pod =head2 array_width Return the length of the longest string in an array. Arguments are not modified. =over 4 =item C<< array_width([arg1], [arg2], ... [argn]) >> Each argument or array element should be a scalar or a reference to an array. The primary usage is to print a group of strings in a fixed width field. For example: =begin html
use Pitonyak::StringUtil qw(array_width right_fmt);

my @strings = ('one', 'two', 'three', 'four');
foreach (right_fmt(array_width(@strings), @strings)) {
  print "$_\n";
}
=end html produces: =begin html
  one
  two
three
 four
=end html =back =cut #************************************************************ sub array_width { my $width = 0; my $this_width; foreach (@_) { $this_width = ( ref($_) ne 'ARRAY' ) ? length($_) : array_width(@$_); $width = $this_width if $this_width > $width; } return $width; } #************************************************************ =pod =head2 center_fmt Modify the arguments to be strings centered in the specified width. A string that is longer than the specified width is truncated. In array context, return the entire array. In scalar context, return only the first string. =over 4 =item C<< center_fmt($width_to_use, @strings_to_format) >> Modify the arguments to be strings centered in the specified width. =begin html
use Pitonyak::StringUtil qw(center_fmt);

my $long_str = 'I am really long';
my @strings = ('one', 'two', 'three', 'four');
foreach (center_fmt(8, @strings, $long_str)) {
  print "$_\n";
}
=end html produces: =begin html
  one
  two
 three
  four
I am rea
=end html =back =cut #************************************************************ sub center_fmt { # No parameter, return undef if ( $#_ < 1 ) { carp("Usage: center_fmt(, )"); return undef; } my $len = $_[0]; my @strings = trim_fmt(@_); my @rc; foreach my $str (@strings) { my $slop = $len - length($str); my $left_space = int( $slop / 2 ); my $right_space = $slop - $left_space; $str = " " x $left_space . $str . " " x $right_space if $slop > 0; push ( @rc, $str ); } return wantarray ? @strings : $strings[0]; } #************************************************************ =pod =head2 compact_space Modify each argument to change runs of white space to a single space. =over 4 =item C<< compact_space(@list_of_strings) >> Modify each argument to change runs of white space to a single space. =begin html