Pitonyak::StringUtil - General string utilities.
String module created by Andrew Pitonyak for his personal use to format strings for pretty output.
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 smart_printer_default
routine, you must first import it. For example,
use Pitonyak::StringUtil qw(smart_printer_default);
|
Return the length of the longest string in an array.
Arguments are not modified.
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:
use Pitonyak::StringUtil qw(array_width right_fmt); |
produces:
one |
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.
center_fmt($width_to_use, @strings_to_format)
Modify the arguments to be strings centered in the specified width.
use Pitonyak::StringUtil qw(center_fmt); |
produces:
one |
Modify each argument to change runs of white space to a single space.
compact_space(@list_of_strings)
Modify each argument to change runs of white space to a single space.
use Pitonyak::StringUtil qw(compact_space);
|
delimited_values($delimiter, @values)
For each string in @values
, split it based on the delimiter.
White space is trimmed from the front and rear.
An array of values is returned.
substr_no_space($line, $start, $len)
Extract text from $line
starting at the zero based
location $start
, with length $len
.
Leading and trailing spaces are removed.
Bounds are checked to avoid errors.
hash_key_width($hash_reference)
Determine the maximum width of the keys in a hash reference. Usually used to right justify hash keys. For example:
use Pitonyak::StringUtil qw(hash_key_width right_fmt);
|
produces
three => 3
|
The function is defined as sub hash_key_width(\%)
,
so Perl automatically uses a hash reference for the call;
in the example above, %map
is used rather than \%map
.
hash_val_width($hash_reference)
Determine the maximum width of the values in a hash.
The usage is the same as hash_key_width
.
Modify the arguments by appending space to force them to be the specified length.
Modify the arguments by appending space to force them to be the specified length.
use Pitonyak::StringUtil qw(hash_key_width left_fmt);
|
produces
three => 3
|
num_int_digits($integer_number)
Return the number of digits in this integer. This method fails if the number is not an integer. For example, consider the following example:
use Pitonyak::StringUtil qw(num_int_digits right_fmt);
|
produces
7 = 1 = 7 = 7 |
Make special note of the results for numbers with decimals, and especially for numbers that are very large in magnitude (as in much larger than an integer).
Format numbers to contain leading zeros.
Return N-digit strings representing the number with leading zeros.
use Pitonyak::StringUtil qw(num_with_leading_zeros); |
produces
00007 <= 7 |
trans_blank($value)
Return $value
if it is defined, and an empty string if it is not.
The trans_blank
method is roughly equivalent to:
return defined($value) ? $value : '';
The primary purpose is to avoid warnings such as "Use of uninitialized value in concatenation (.) or string at...".
use Pitonyak::StringUtil qw(trans_blank); |
trans_blank($value, $default)
Returns $value if it is defined with length greater than zero and $default
if it is not.
In other words, a string of length zero is treated the same as an undefined value.
A typical value for $default
is the empty string, which means trans_blank
is rarely called with two arguments.
Return $value
if it is defined, and $default
if it is not.
The trans_blank
method is roughly equivalent to:
return defined($value) ? $value : $default;
The primary purpose is to avoid warnings such as "Use of uninitialized value in concatenation (.) or string at...".
use Pitonyak::StringUtil qw(trans_blank); |
trim_fmt($width_to_use, @strings_to_format)
Trim all strings so that their length is not greater than
$width_to_use
. Strings that are too long are truncated.
The string arguments are modified.
use Pitonyak::StringUtil qw(trim_fmt); |
produces
(12345), (123)
|
trim_space(@strings_to_format)
Remove leading and trailing white space. The parameters are modified.
Modify the arguments by prepending space to force them to be the specified length.
right_fmt($width_to_use, @strings_to_format)
Modify the arguments by prepending space to force them to be the specified length.
use Pitonyak::StringUtil qw(hash_key_width right_fmt);
|
produces
three => 3
|
smart_printer($left, $left_grow, $separator, @Things_to_print)
Print the argument in a pleasing way depending on the type. The arguments are not modified. The first three arguments direct how items are printed.
An item is printed based on its type.
See smart_printer_default
to see a typical example.
Simple wrapper to call smart_printer
.
use Pitonyak::StringUtil qw(smart_printer_default);
|
produces
{ |
Copyright 1998-2008, Andrew Pitonyak (andrew@pitonyak.org)
This library is free software; you can redistribute it and/or modify it under the same terms as Perl itself.
Version 1.00 First release
Version 1.01 Changed internal documentation to POD
Version 1.02 Updated POD and package name for use in CEFM project. Fixed a bug where each negative number caused the width to become smaller for the next iteration if multiple numbers were passed at the same time.
Version 1.03 Updated POD for minor corrections.