Mdc::StringUtils


Mdc::StringUtils


Name

Mdc::StringUtils

Commonly used text functions

Version 1.024 - 2:38 PM 2004-10-13


Synopsis

 use Mdc::StringUtils(TextWrap);
 print TextWrap(\$string)


Description

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.


Functions

ExpandTabs

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;

isNumeric

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.

RandomString

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

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

Rpad

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 )

Lpad

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 )

Lcase

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 )

TextWrap

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

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;

ValueIn

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

Pattern Match

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
MATCH CHARACTERS:
 ?      any single character
 *      any string of characters
 #      one or more consecutive numerals
OPTIONS:
 b      match from begining of string
 e      match from end of string
 be     match exact string
EXAMPLES:
 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)

isObscene

Simple test to determin if a string contains any obscene words.


Examples

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 );


Author and Copyright

StringUtils - Copyright (c) 2003, Mark K Mueller
Mueller Design and Consulting
PO Box 576705, Modesto, CA 95357
http://www.markmueller.com/
All Rights Reserved.


MkM