Mdc::StringUtils
Commonly used text functions
Version 1.024 - 2:38 PM 2004-10-13
use Mdc::StringUtils(TextWrap); print TextWrap(\$string)
Provides a set of commonly used functions for use with text strings.
No function will be imported into your namespace by default.
Specify the function name(s) to import on the ``use'' statement.
Convert tabs into spaces
Argument of ExpandTabs should be a SCALAR reference. Will modify referenced variable by expanding any tabs in a paragraph by converting them into the appropriate number of spaces. Paragraph may contain new-lines.
ExpandTabs REF ExpandTabs \$text;
Test if value is numeric
Test if a given argument is a numeric value. Will return 1 if true, blank if not. Negative and decimal numbers will also return 1. A string that contains any white space will not be considered to be numeric.
Create a random string
Return a string of random characters from the set 0-9, a-z, and A-Z. A length may be supplied ranging from 1 to 4096 characters. String will not contain more than 3 consecutive apha or numeric characters. String will not repeat the same character with 20 consecutive characters.
Randomize Perl's built-in rand function further using using CPU time, process ID, REMOTE_ADDR and REMOTE_PORT. This function is automatically called at run time so typically there will be no need to call it again.
Randomize
Right pad
Pad the right side of a string to achieve a given string length. If the pad character is not defined, a space will be used.
Rpad( string, length, pad_character )
Pad with leading characters
Pad the left side of a string to achieve a given string length. If the pad character is not defined, a space will be used.
Lpad( string, length, pad_character )
Convert a string to lower case
For convenience, this routine will accept five different context variables: scalar, array, scalar reference, array reference, and hash reference.
In the scalar context, a lower-case scalar will be returned. In the scalar-reference context, the lower-case operation will be performed on the referenced variable.
In the array context, a lower-case list will be returned. In the array-reference context, the lower-case operation will be performed on each element of the referenced array.
In the hash-reference context, the lower-case operation will be performed on each value of the referenced hash.
Lcase( SCALAR ) Lcase( ARRAY ) Lcase( SCALAR reference ) Lcase( ARRAY reference ) Lcase( HASH reference )
Wrap text at given column
First argument of TextWrap should be a SCALAR reference. Will modify referenced variable by inserting new-line characters at or near the given column position. The new-line insertion point will be after a whole word or at a hyphen. The default column value is 80 if none is supplied. The minimum column value is 10 characters.
Extra white space will be filtered from input text. Tabs, carriage returns and newline characters will be replaced with single spaces. If it is desired that tabs or newline characters be retained, simply replace them with the appropriate escape sequence and they will be ignored.
If a line of text cannot be wrapped before the column location, it will be wrapped at the first available space after the column.
TextWrap SCALAR, SCALAR reference TextWrap SCALAR, SCALAR TextWrap SCALAR
TextWrap(49, \$var); # wrap text at column 49, modifies $var TextWrap(49, $var); # wrap text at column 49, returns modified string TextWrap(49); # wrap text at column 49 from $_, returns modified string
Trim leading and trailing white space
Will trim leading and trailing white space from a variable. Supplied argument may be a SCALAR variable or a reference to one. In the SCALAR context, the trimmed results will be returned. In REF contect, the variable referred to will be modified.
SCALAR:
print Trim $text;
SCALAR reference:
Trim \$text;
Find a value in a hash or an array
Will find a value a hash or an array. First argument is the value being searched for. Second argument may be a HASH reference, an ARRAY reference, or a list of values. Returns 1 if found, blank if not.
ValueIn SCALAR, HASHREF ValueIn SCALAR, ARRAYREF ValueIn SCALAR, LIST
Simple pattern matching function. Returns the matched substring if found. Return blank if no match found. Case is ignored.
Pattern_match EXPR, PATTERN, OPTIONS Pattern_match EXPR, PATTERN
? any single character * any string of characters # one or more consecutive numerals
b match from begining of string e match from end of string be match exact string
Pattern_match( '127.0.0.1', '#.#.#.#' ); # 127.0.0.1 Pattern_match( 'Current time: 10:30am', ' #:#?m' ); # 10:30a Pattern_match( 'Send $1,123.56 now', '$* ' ); # 1,123.56 Pattern_match( 'Send $1,123.56 now', '$* ' ); # 1,123.56 Pattern_match( '1234 5678' , '#' ); # 12345 Pattern_match( '1234 5678' , '#', 'e' ); # 5678 Pattern_match( '1234 5678' , '#', 'be' ); # (returns blank)
Simple test to determin if a string contains any obscene words.
isNumeric:
use Mdc::StringUtils 'isNumeric';
print "This is numeric" if isNumeric "-3.1416";
Trim:
use Mdc::StringUtils 'Trim';
Trim \$text;
TextWrap:
use Mdc::StringUtils 'TextWrap';
TextWrap(\$paragraph, 40);
ExpandTabs:
use Mdc::StringUtils 'ExpandTabs';
my $text = "\t^This line should have 8 leading spaces.".
"\n****\t^If this thing works these will line up.";
print ExpandTabs(\$text);
ValueIn:
@list = qw(one two three four five six seven);
print "found" if ValueIn('four',\@list);
%hash = (one => 1, two => 2, three => 3, four => 4);
print "found $key" if $key = ValueIn(3,\%hash);
RandomString:
$str = RandomString(); # 16 random characters
$str2 = RandomString(32); # 32 random characters
Lpad:
$amnt = Lpad('$9.95', 10 ); # return ' $9.95'
$num = Lpad(235, 10, 0 ); # return '0000000235'
Rpad:
$label = Rpad('Amount:', 10 ); # return 'Amount: '
Lcase:
# scalar and list context
$scalar = Lcase( $scalar );
@list = Lcase( @list );
($a,$b) = Lcase( $a, $b );
# reference context
Lcase( \$scalar );
Lcase( \@list );
Lcase( \%hash );