man String::Escape () - Registry of string functions, including backslash escapes

NAME

String::Escape - Registry of string functions, including backslash escapes

SYNOPSIS

  use String::Escape qw( printable unprintable );
  # Convert control, high-bit chars to \n or \xxx escapes
  $output = printable($value);
  # Convert escape sequences back to original chars
  $value = unprintable($input);

  use String::Escape qw( elide );
  # Shorten strings to fit, if necessary
  foreach (@_) { print elide( $_, 79 ) . "\n"; }

  use String::Escape qw( string2list list2string );
  # Pack and unpack simple lists by quoting each item
  $list = list2string( @list );
  @list = string2list( $list );

  use String::Escape qw( string2hash hash2string );
  # Pack and unpack simple hashes by quoting each item
  $hash = hash2string( %hash );
  %hash = string2hash( $hash );

  use String::Escape qw( escape );
  # Defer selection of escaping routines until runtime
  $escape_name = $use_quotes ? 'qprintable' : 'printable';
  @escaped = escape($escape_name, @values);

DESCRIPTION

This module provides a flexible calling interface to some frequently-performed string conversion functions, including applying and removing C/Unix-style backslash escapes like \n and \t, wrapping and removing double-quotes, and truncating to fit within a desired length.

Furthermore, the escape() function provides for dynamic selection of operations by using a package hash variable to map escape specification strings to the functions which implement them. The lookup imposes a bit of a performance penalty, but allows for some useful late-binding behaviour. Compound specifications (ex. 'quoted uppercase') are expanded to a list of functions to be applied in order. Other modules may also register their functions here for later general use. (See the CALLING BY NAME section below for more.)

FUNCTION REFERENCE

Escaping And Unescaping Functions

Each of these functions takes a single simple scalar argument and returns its escaped (or unescaped) equivalent. Add double quote characters to each end of the string. As above, but only quotes empty, punctuated, and multiword values; simple values consisting of alphanumerics without special characters are not quoted. If the string both begins and ends with double quote characters, they are removed, otherwise the string is returned unchanged. These functions convert return, newline, tab, backslash and unprintable characters to their backslash-escaped equivalents and back again. The qprintable function applies printable escaping and then wraps the results with quote_non_words, while unqprintable applies unquote and then unprintable. (Note that this is not MIME quoted-printable encoding.)

Simple Arrays and Hashes

Converts a space separated string of words and quoted phrases to an array; Joins an array of strings into a space separated string of words and quoted phrases; Converts a space separated string of equal-sign-associated key=value pairs into a simple hash. Converts a simple hash into a space separated string of equal-sign-associated key=value pairs. Converts an array of equal-sign-associated key=value strings into a simple hash. Converts a hash to an array of equal-sign-associated key=value strings.

String Elision Function

This function extracts the leading portion of a provided string and appends ellipsis if it's longer than the desired maximum excerpt length. If the original string is shorter than CW$length, it is returned unchanged. At most CW$length characters are returned; if called with a single argument, CW$length defaults to CW$DefaultLength. Up to CW$word_boundary_strictness additional characters may be ommited in order to make the elided portion end on a word boundary; you can pass 0 to ignore word boundaries. If not provided, CW$word_boundary_strictness defaults to CW$DefaultStrictness.

$Elipses
The string of characters used to indicate the end of the excerpt. Initialized to '...'.
$DefaultLength
The default target excerpt length, used when the elide function is called with a single argument. Initialized to 60.
$DefaultStrictness
The default word-boundary flexibility, used when the elide function is called without the third argument. Initialized to 10.

CALLING BY NAME

These functions provide for the registration of string-escape specification names and corresponding functions, and then allow the invocation of one or several of these functions on one or several source string values. Returns an altered copy of the provided values by looking up the escapes string in a registry of string-modification functions. If called in a scalar context, operates on the single value passed in; if called in a list contact, operates identically on each of the provided values. Valid escape specifications are: The coresponding specification will be looked up and used.

a sequence of names separated by whitespace,
Each name will be looked up, and each of the associated functions will be applied successively, from left to right.
a reference to a function
The provided function will be called on with each value in turn.
a reference to an array
Each item in the array will be expanded as provided above. A fatal error will be generated if you pass an unsupported escape specification, or if the function is called with multiple values in a scalar context. Returns a list of defined escape specification strings. Add a new escape specification and corresponding function. By default, the CW%Escapes hash is initialized to contain the following mappings:
quote, unquote, or quote_non_words
printable, unprintable, qprintable, or unqprintable,
elide
Run the above-described functions of the same names.
uppercase, lowercase, or initialcase
Alters the case of letters in the string to upper or lower case, or for initialcase, sets the first letter to upper case and all others to lower.
none
Return an unchanged copy of the original value.

EXAMPLES

Here are a few example uses of these functions, along with their output.

Backslash Escaping

CWprint printable( "\tNow is the time\nfor all good folks\n" );

  \tNow is the time\nfor all good folks\n

CWprint unprintable( '\\tNow is the time\\nfor all good folks\\n' );

          Now is the time
  for all good folks

Escape By Name

CWprint escape('qprintable', "\tNow is the time\nfor all good folks\n" );

  "\tNow is the time\nfor all good folks\n"

CWprint escape('uppercase qprintable', "\tNow is the time\nfor all good folks\n" );

  "\tNOW IS THE TIME\nFOR ALL GOOD FOLKS\n"

CWprint join '--', escape('printable', "\tNow is the time\n", "for all good folks\n" );

  \tNow is the time\n--for all good folks\n

String Elision Function

CW$string = 'foo bar baz this that the other';

CWprint elide( $string, 100 );

  foo bar baz this that the other

CWprint elide( $string, 12 );

  foo bar...

CWprint elide( $string, 12, 0 );

  foo bar b...

Simple Arrays and Hashes

CWprint list2string('hello', 'I move next march');

  hello "I move next march"

CW@list = string2list('one "second item" 3 "four\nlines\nof\ntext"');

CWprint $list[1];

  second item

CWprint hash2string( 'foo' => 'Animal Cities', 'bar' => 'Cheap' );

  foo="Animal Cities" bar=Cheap

CW%hash = string2hash('key=value "undefined key" words="the cat in the hat"');

CWprint $hash{'words'};

  the cat in the hat

CWprint exists $hash{'undefined_key'} and ! defined $hash{'undefined_key'};

  1

PREREQUISITES AND INSTALLATION

This package should run on any standard Perl 5 installation.

To install this package, download and unpack the distribution archive from http://www.evoscript.com/dist/ or your favorite CPAN mirror, and execute the standard perl Makefile.PL, make test, make install sequence.

STATUS AND SUPPORT

This release of String::Escape is intended for public review and feedback. It has been tested in several environments and no major problems have been discovered, but it should be considered beta pending that feedback.

  Name            DSLI  Description
  --------------  ----  ---------------------------------------------
  String::
  ::Escape        bdpf  Registry of useful string escaping functions

Further information and support for this module is available at <www.evoscript.org>.

Please report bugs or other problems to CW<simonm@cavalletto.org>.

The following changes are in progress or under consideration:

•
Use word-boundary test in elide's regular expression rather than \s|\Z.
•
Check for possible problems in the use of printable escaping functions and list2hash. For example, are the encoded strings for hashes with high-bit characters in their keys properly unquoted and unescaped?
•
Update string2list; among other things, embedded quotes (eg: a@"!a) shouldn't cause phrase breaks.

SEE ALSO

Numerous modules provide collections of string manipulation functions; see String::Edit for an example.

The string2list function is similar to to the quotewords function in the standard distribution; see Text::ParseWords.

Use other packages to stringify more complex data structures; see Data::PropertyList, Data::Dumper, or other similar package.

CREDITS AND COPYRIGHT

Developed By

  M. Simon Cavalletto, simonm@cavalletto.org
  Evolution Softworks, www.evoscript.org

Contributors

  Eleanor J. Evans piglet@piglet.org
  Jeremy G. Bishop

Copyright

Copyright 2002 Matthew Simon Cavalletto.

Portions copyright 1996, 1997, 1998, 2001 Evolution Online Systems, Inc.

License

You may use, modify, and distribute this software under the same terms as Perl.