Escape(3)
π NAME π
Okay so this is the Escape(3) page from the User Contributed Perl Documentation, and the module is String::Escape β literally here for backslash escapes, quoted phrases, word elision, etc. β¨
β¨ SYNOPSIS β¨
This module hands you 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. It's giving string glow-up station π
.
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);
π«Ά INTERFACE π«Ά
All of the public functions described below are available as optional exports. No forcing, just options β¨.
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. Choose your own adventure, bestie.
π Quoting π
Each of these functions takes a single simple scalar argument and returns its escaped (or unescaped) equivalent. Simple, clean, iconic. Periodt.
quote($value) : $escapedβ adds double quote characters to each end of the string. Slay.unquote($value) : $escapedβ if the string both begins and ends with double quote characters, they get removed; otherwise the string is returned unchanged.quote_non_words($value) : $escapedβ as above, but only quotes empty, punctuated, and multiword values; simple values consisting of alphanumerics without special characters are not quoted. It's giving selective β¨.singlequote($value) : $escapedβ adds single quote characters to each end of the string.unsinglequote($value) : $escapedβ if the string both begins and ends with single quote characters, they are removed; otherwise the string is returned unchanged.
πΈ Backslash Escaping Functions πΈ
Each of these functions takes a single simple scalar argument and returns its escaped (or unescaped) equivalent. We love consistency π.
These functions recognize common whitespace sequences "\r", "\n", and "\t", as well as hex escapes "\x4F" and ocatal "\020" (that's octal, bestieβthe page just has a typo and we're keeping the lore π). Not me reading ocatal and giggling.
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. It's a whole vibe check for what counts as special.
backslash($value) : $escapedβ converts special characters to their backslash-escaped equivalents.unbackslash($value) : $escapedβ converts backslash escape sequences in a string back to their original characters.qqbackslash($value) : $escapedβ converts special characters to their backslash-escaped equivalents and then wraps the results with double quotes.unqqbackslash($value) : $escapedβ strips surrounding double quotes then converts backslash escape sequences back to their original characters.
Here are a few examples:
Example one, bestie:
print backslash( "\tNow is the time\nfor all good folks\n" );
Output:
\tNow is the time\nfor all good folks\n
Example two:
print unbackslash( '\\tNow is the time\\nfor all good folks\\n' );
Output:
Now is the time
for all good folks
π¦ Legacy Backslash Functions π¦
In addition to the four functions listed above, there is a corresponding set which use a slightly different set of escape sequences. Okay, legacy era π. iykyk if you're maintaining old code.
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. So like, use the new ones unless you literally have to, fr fr.
printable($value) : $escapedβ converts return, newline, tab, backslash and unprintable characters to their backslash-escaped equivalents.unprintable($value) : $escapedβ converts backslash escape sequences in a string back to their original value.qprintable($value) : $escapedβ converts special characters to their backslash-escaped equivalents and then wraps the results with double quotes.
Note, bestie: this is not MIME quoted-printable encoding. Not the same thing, no cap.
unqprintable($value) : $escapedβ strips surrounding double quotes then converts backslash escape sequences back to their original value.
π Other Backslash Functions π
In addition to the functions listed above, there is also one function that mirrors the behavior of Perl's built-in quotemeta() function. It's giving "we have quotemeta at home" but in a good way π«Ά.
unquotemeta($value) : $escapedβ strips out backslashes before any character.
π§ 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. It's literally the "make it fit" function βοΈ.
elide($string) : $elided_string
elide($string, $length) : $elided_string
elide($string, $length, $word_boundary_strictness) : $elided_string
elide($string, $length, $word_boundary_strictness, $elipses) :
$elided_string
Return a single-quoted, shortened version of the string, with ellipsis.
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 omitted 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. (The original page spells it ommitted, and honestly same.)
$Elipsesβ the string of characters used to indicate the end of the excerpt. Initialized to'...'.$DefaultLengthβ the default target excerpt length, used when theelidefunction is called with a single argument. Initialized to60.$DefaultStrictnessβ the default word-boundary flexibility, used when theelidefunction is called without the third argument. Initialized to10.
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
π₯ escape() π₯
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. It's the runtime choose-your-escape era, and we're obsessed π₯. It's giving main character energy for runtime dispatch.
escape($escapes, $value) : $escaped_value
escape($escapes, @values) : @escaped_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 context (the page says "list contact," iconic typo, but it means list context), 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. Cute.
Valid escape specifications are:
- one of the keys defined in
%Escapesβ the corresponding 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.
Warning, bestie: 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. So like, don't do that π.
String::Escape::names() : @defined_escapesβ returns a list of defined escape specification strings.String::Escape::add( $escape_name, \&escape_function );β add a new escape specification and corresponding function.
By default, all of the public functions described below are available as named escape commands, as well as the following built-in functions:
none: return the string unchanged.uppercase: calls the built-inucfunction.lowercase: calls the built-inlcfunction.initialcase: calls the built-inlcanducfirstfunctions.
Here are a few examples:
Example one:
print escape('qprintable', "\tNow is the time\nfor all good folks\n" );
Output:
"\tNow is the time\nfor all good folks\n"
Example two:
print escape('uppercase qprintable', "\tNow is the time\nfor all good folks\n" );
Output:
"\tNOW IS THE TIME\nFOR ALL GOOD FOLKS\n"
Example three:
print join '--', escape('printable', "\tNow is the time\n", "for all good folks\n" );
Output:
\tNow is the time\n--for all good folks\n
Example four: you can add more escaping functions to the supported set by calling add().
String::Escape::add( 'html', \&HTML::Entities::encode_entities );
print escape('html', "AT&T" );
Output:
AT&T
π Space-separated Lists and Hashes π
@words = string2list( $space_separated_phrases );β converts a space separated string of words and quoted phrases to an array;$space_sparated_string = list2string( @words );β joins an array of strings into a space separated string of words and quoted phrases;%hash = string2hash( $string );β converts a space separated string of equal-sign-associatedkey=valuepairs into a simple hash.$string = hash2string( %hash );β converts a simple hash into a space separated string of equal-sign-associatedkey=valuepairs.%hash = list2hash( @words );β converts an array of equal-sign-associatedkey=valuestrings into a simple hash.@words = hash2list( %hash );β converts a hash to an array of equal-sign-associatedkey=valuestrings.
Here are a few examples:
Example one:
print list2string('hello', 'I move next march');
Output:
hello "I move next march"
Example two:
@list = string2list('one "second item" 3 "four\nlines\nof\ntext"');
print $list[1];
Output:
second item
Example three:
print hash2string( 'foo' => 'Animal Cities', 'bar' => 'Cheap' );
Output:
foo="Animal Cities" bar=Cheap
Example four:
%hash = string2hash('key=value "undefined key" words="the cat in the hat"');
print $hash{'words'};
Output:
the cat in the hat
print exists $hash{'undefined_key'} and ! defined $hash{'undefined_key'};
Output:
1
π SEE ALSO π
Ok so like, sooo many modules out there give you collections of string escaping functions for super specific contexts π β so if this one's not it for you, go shop around, bestie.
The string2list function is similar to the quotewords function in the standard distribution β see Text::ParseWords π«Ά.
And if you need to stringify more complex data structures, use other packages β check out Storable, Data::Dumper, or other similar packages β¨.
π BUGS π
So these are the issues or changes that are under consideration for future releases, ngl:
- Does this problem with the
\rcharacter only show up on Windows? (And is it, in fact, a feature rather than a bug? π)
http://rt.cpan.org/Public/Bug/Display.html?id=19766
- Consider changes to word parsing in
string2list: Perhaps use\bword-boundary test inelide's regular expression rather than\s|\Z? Perhaps quotes embedded in a word (eg:a@"!a) shouldn't cause phrase breaks? π€ - 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? π - We should allow escape specifications to contain
=signs and optional arguments, so that users can request certain string lengths with"escape("lowercase elide=20 quoted", @_". π
β VERSION β
This is version 2010.002. Periodt.
π INSTALLATION π
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 sequence:
perl Makefile.PL
make test
make install
β¦or your local equivalent, bestie π«Ά.
π¦ SUPPORT π¦
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) πΈ.
π AUTHOR π
Matthew Simon Cavalletto, <simonm at cavalletto.org>
Initial versions developed at Evolution Online Systems with Eleanor J. Evans and Jeremy G. Bishop π.
π§ LICENSE π§
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.
Aaaand that's the whole page, bestie β no cap, you made it to the end π This wasEscape(3), shipped withperl v5.42.0and dated2011-11-18. Go forth and escape responsibly π β¨π¦
π¦ where this page comes from
- opensuse-leap 16.0: perl-String-Escape 2010.002-bp160.1.11 Β· GPL-1.0+ or Artistic-1.0
- π girlified by deepseek-ai/DeepSeek-V4.1-Flash-1017 on : a language model's rewrite of the original, under its terms