Escape(3pm) | User Contributed Perl Documentation | Escape(3pm) |
String::Escape - Backslash escapes, quoted phrase, word elision, etc.
This module provides a flexible calling interface to some frequently-performed string conversion functions, including applying and removing backslash escapes like \n and \t, wrapping and removing double-quotes, and truncating to fit within a desired length.
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( escape ); # Defer selection of escaping routines until runtime $escape_name = $use_quotes ? 'qprintable' : 'printable'; @escaped = escape($escape_name, @values);
All of the public functions described below are available as optional exports.
You can either import the specific functions you want, or import only the "escape()" function and pass it the names of the functions to invoke.
Each of these functions takes a single simple scalar argument and returns its escaped (or unescaped) equivalent.
Each of these functions takes a single simple scalar argument and returns its escaped (or unescaped) equivalent.
These functions recognize common whitespace sequences "\r", "\n", and "\t", as well as hex escapes "\x4F" and ocatal "\020".
When escaping, alphanumeric characters and most punctuation is passed through unchanged; only the return, newline, tab, backslash, dollar, at sign and unprintable control and high-bit characters are escaped.
Here are a few examples:
print backslash( "\tNow is the time\nfor all good folks\n" ); \tNow is the time\nfor all good folks\n
print unbackslash( '\\tNow is the time\\nfor all good folks\\n' ); Now is the time for all good folks
In addition to the four functions listed above, there is a corresponding set which use a slightly different set of escape sequences.
These functions do not support as many escape sequences and use a non-standard format for hex escapes. In general, the above "backslash()" functions are recommended, while these functions are retained for legacy compatibility purposes.
(Note that this is not MIME quoted-printable encoding.)
In addition to the functions listed above, there is also one function that mirrors the behavior of Perl's built-in "quotemeta()" 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 $length, it is returned unchanged. At most $length characters are returned; if called with a single argument, $length defaults to $DefaultLength.
Up to $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, $word_boundary_strictness defaults to $DefaultStrictness.
Here are a few examples:
$string = 'foo bar baz this that the other'; print elide( $string, 12 ); # foo bar... print elide( $string, 12, 0 ); # foo bar b... print elide( $string, 100 ); # foo bar baz this that the other
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.
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.
Space-separated compound specifications like 'quoted uppercase' are expanded to a list of functions to be applied in order.
Valid escape specifications are:
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.
By default, all of the public functions described below are available as named escape commands, as well as the following built-in functions:
Here are a few examples:
"\tNow is the time\nfor all good folks\n"
"\tNOW IS THE TIME\nFOR ALL GOOD FOLKS\n"
\tNow is the time\n--for all good folks\n
"String::Escape::add( 'html', \&HTML::Entities::encode_entities );"
"print escape('html', "AT&T" );"
AT&T
Here are a few examples:
hello "I move next march"
"print $list[1];"
second item
foo="Animal Cities" bar=Cheap
"print $hash{'words'};"
the cat in the hat
"print exists $hash{'undefined_key'} and ! defined $hash{'undefined_key'};"
1
Numerous modules provide collections of string escaping functions for specific contexts.
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 Storable, Data::Dumper, or other similar package.
The following issues or changes are under consideration for future releases:
http://rt.cpan.org/Public/Bug/Display.html?id=19766
This is version 2010.002.
This package should run on any standard Perl 5 installation.
To install this package, download the distribution from a CPAN mirror, unpack the archive file, and execute the standard "perl Makefile.PL", "make test", "make install" sequence or your local equivalent.
Once installed, this module's documentation is available as a manual page via "perldoc String::Escape" or on CPAN sites such as "http://search.cpan.org/dist/String-Escape".
If you have questions or feedback about this module, please feel free to contact the author at the address shown below. Although there is no formal support program, I do attempt to answer email promptly. Bug reports that contain a failing test case are greatly appreciated, and suggested patches will be promptly considered for inclusion in future releases.
You can report bugs and request features via the CPAN web tracking system at "http://rt.cpan.org/NoAuth/ReportBug.html?Queue=String-Escape" or by sending mail to "bug-string-escape at rt.cpan.org".
If you've found this module useful or have feedback about your experience with it, consider sharing your opinion with other Perl users by posting your comment to CPAN's ratings system ("http://cpanratings.perl.org/rate/?distribution=String-Escape").
For more general discussion, you may wish to post a message on PerlMonks ("http://perlmonks.org/?node=Seekers%20of%20Perl%20Wisdom") or on the comp.lang.perl.misc newsgroup ("http://groups.google.com/group/comp.lang.perl.misc/topics").
Matthew Simon Cavalletto, "<simonm at cavalletto.org>"
Initial versions developed at Evolution Online Systems with Eleanor J. Evans and Jeremy G. Bishop.
Copyright 2010, 2002 Matthew Simon Cavalletto.
Portions copyright 1996, 1997, 1998, 2001 Evolution Online Systems, Inc.
You may use, modify, and distribute this software under the same terms as Perl.
See http://dev.perl.org/licenses/ for more information.
2022-12-12 | perl v5.36.0 |