NAME
String::Tools - Various tools for handling strings.
VERSION
version 0.14.136
SYNOPSIS
use String::Tools qw(define is_blank shrink stitch stitcher subst trim);
my $val = define undef; # ''
say is_blank(undef); # 1 (true)
say is_blank(''); # 1 (true)
say is_blank("\t\n\0"); # 1 (true)
say is_blank("0"); # '' (false)
say shrink(" This is a test\n"); # 'This is a test'
## stitch ##
say stitch( qw(This is a test) ); # "This is a test"
say stitch(
qw(This is a test), "\n",
qw(of small proportions)
); # "This is a test\nof small proportions"
# Format some other language in a more readable format,
# yet keep the resulting string small for transport across the network.
say stitch(qw(
SELECT *
FROM table
WHERE foo = ?
AND bar = ?
));
# "SELECT * FROM table WHERE foo = ? AND bar = ?"
## subst ##
my $date = 'Today is ${ day } of $month, in the year $year';
say subst( $date, day => '15th', month => 'August', year => 2013 );
# OR
say subst( $date, { day => '15th', month => 'August', year => 2013 } );
# OR
say subst( $date, [ day => '15th', month => 'August', year => 2013 ] );
my $lookfor = 'The thing you're looking for is $_.';
say subst( $lookfor, 'this' );
say 'No, wait! ', subst( $lookfor, _ => 'that' );
say trim(" This is a test\n"); # 'This is a test'
# Describe what to trim:
say trim(" This is a test\n",
l => '\s+', r => '\n+'); # 'This is a test'
DESCRIPTION
String::Tools is a collection of tools to manipulate strings.
Variables
$THREAD-
The default thread to use while stitching a string together. Defaults to a single space,
' '. Used in "shrink( $string )" and "stitch( @list )". $BLANK-
The default regular expression character class to determine if a string component is blank. Defaults to
[[:cntrl:][:space:]]. Used in "is_blank( $string )", "shrink( $string )", "stitch( @list )", and "trim( $string, qr/l/, qr/r/ )".
Functions
define( $scalar = $_ )-
Returns
$scalarif it is defined, or the empty string if it's undefined. Useful in avoiding the 'Use of uninitialized value' warnings.$scalardefaults to$_if not specified. is_blank( $string = $_ )-
Return true if
$stringis blank. A blank$stringis undefined, the empty string, or a string that conisists entirely of "$BLANK" characters.$stringdefaults to$_if not specified. shrink( $string = $_ )-
Combine multiple consecutive
$BLANKcharacters into one$THREADcharacter throughout$string.$stringdefaults to$_if not specified. stitch( @list )-
Stitch together the elements of list with "$THREAD". If an item in
@listis blank (as measured by "is_blank( $string )"), then the item is stitched without "$THREAD".This approach is more intuitive than
join:say join( ' ' => qw( 1 2 3 ... ), "\n", qw( Can anybody hear? ) ); # "1 2 3 ... \n Can anybody hear?" say join( ' ' => qw( 1 2 3 ... ) ); say join( ' ' => qw( Can anybody hear? ) ); # "1 2 3 ...\nCan anybody hear?" # say stitch( ' ' => qw( 1 2 3 ... ), "\n", qw( Can anybody hear? ) ); # "1 2 3 ...\nCan anybody hear?" say join( ' ' => $user, qw( home dir is /home/ ), $user ); # "$user home dir is /home/ $user" say join( ' ' => $user, qw( home dir is /home/ ) ) . $user; # "$user home dir is /home/$user" # say stitch( ' ' => $user, qw( home dir is /home/ ), '', $user ); # "$user home dir is /home/$user" stitcher( $thread => @list )-
Stitch together the elements of list with
$threadin place of "$THREAD". subst( $string, %variables = ( _ => $_ ) )-
Take in
$string, and do a search and replace of all the variables named in%variableswith the associated values.The
%variablesparameter can be a hash, hash reference, array reference, list, scalar, or empty. The single scalar is treated as if the name is the underscore. The empty case is handled by using underscore as the name, and$_as the value.If you really want to replace nothing in the string, then pass in an empty hash reference or empty array reference, as an empty hash or empty list will be treated as the empty case.
Only names which are in
%variableswill be replaced. This means that substitutions that are in$stringwhich are not mentioned in%variablesare simply ignored and left as is.Returns the string with substitutions made.
trim( $string, qr/l/ = qr/$BLANK+/, qr/r/ = $l )-
Trim
stringof leading and trailing characters.$stringdefaults to$_if not specified. The paramtersl(lead) andr(rear) are both optional, and can be specified positionally, or as key-value pairs. Iflis undefined, the default pattern is/$BLANK+/, matched at the beginning of the string. Ifris undefined, the default pattern is the value ofl, matched at the end of the string.If you don't want to trim the start or end of a string, set the corresponding parameter to the empty string
''.say map trim, @strings; say trim(' This is a test ') # 'This is a test' say trim('--This is a test==', qr/-/, qr/=/); # '-This is a test=' say trim(' This is a test!!', r => qr/[.?!]+/, l => qr/\s+/); # 'This is a test'
BUGS
None known.
TODO
Nothing?
AUTHOR
Bob Kleemann
SEE ALSO
"join" in perlfunc, Any templating system.