| 1 |
NAME |
| 2 |
Text::Trim - remove leading and/or trailing whitespace from strings |
| 3 |
|
| 4 |
VERSION |
| 5 |
version 1.04 |
| 6 |
|
| 7 |
SYNOPSIS |
| 8 |
use Text::Trim; |
| 9 |
|
| 10 |
$text = "\timportant data\n"; |
| 11 |
$data = trim $text; |
| 12 |
# now $data contains "important data" and $text is unchanged |
| 13 |
|
| 14 |
# or: |
| 15 |
trim $text; # work in-place, $text now contains "important data" |
| 16 |
|
| 17 |
@lines = <STDIN>; |
| 18 |
rtrim @lines; # remove trailing whitespace from all lines |
| 19 |
|
| 20 |
# Alternatively: |
| 21 |
@lines = rtrim <STDIN>; |
| 22 |
|
| 23 |
# Or even: |
| 24 |
while (<STDIN>) { |
| 25 |
trim; # Change $_ in place |
| 26 |
# ... |
| 27 |
} |
| 28 |
|
| 29 |
DESCRIPTION |
| 30 |
This module provides functions for removing leading and/or trailing |
| 31 |
whitespace from strings. It is basically a wrapper around some simple |
| 32 |
regexes with a flexible context-based interface. |
| 33 |
|
| 34 |
EXPORTS |
| 35 |
All functions are exported by default. |
| 36 |
|
| 37 |
CONTEXT HANDLING |
| 38 |
void context |
| 39 |
Functions called in void context change their arguments in-place |
| 40 |
|
| 41 |
trim(@strings); # All strings in @strings are trimmed in-place |
| 42 |
|
| 43 |
ltrim($text); # remove leading whitespace on $text |
| 44 |
|
| 45 |
rtrim; # remove trailing whitespace on $_ |
| 46 |
|
| 47 |
No changes are made to arguments in non-void contexts. |
| 48 |
|
| 49 |
list context |
| 50 |
Values passed in are changed and returned without affecting the |
| 51 |
originals. |
| 52 |
|
| 53 |
@result = trim(@strings); # @strings is unchanged |
| 54 |
|
| 55 |
@result = rtrim; # @result contains rtrimmed $_ |
| 56 |
|
| 57 |
($result) = ltrim(@strings); # like $result = ltrim($strings[0]); |
| 58 |
|
| 59 |
scalar context |
| 60 |
As list context but multiple arguments are stringified before being |
| 61 |
returned. Single arguments are unaffected. This means that under these |
| 62 |
circumstances, the value of $" ($LIST_SEPARATOR) is used to join the |
| 63 |
values. If you don't want this, make sure you only use single arguments |
| 64 |
when calling in scalar context. |
| 65 |
|
| 66 |
@strings = ("\thello\n", "\tthere\n"); |
| 67 |
$trimmed = trim(@strings); |
| 68 |
# $trimmed = "hello there" |
| 69 |
|
| 70 |
local $" = ', '; |
| 71 |
$trimmed = trim(@strings); |
| 72 |
# Now $trimmed = "hello, there" |
| 73 |
|
| 74 |
$trimmed = rtrim; |
| 75 |
# $trimmed = $_ minus trailing whitespace |
| 76 |
|
| 77 |
Undefined values |
| 78 |
If any of the functions are called with undefined values, the behaviour |
| 79 |
is in general to pass them through unchanged. When stringifying a list |
| 80 |
(calling in scalar context with multiple arguments) undefined elements |
| 81 |
are excluded, but if all elements are undefined then the return value is |
| 82 |
also undefined. |
| 83 |
|
| 84 |
$foo = trim(undef); # $foo is undefined |
| 85 |
$foo = trim(undef, undef); # $foo is undefined |
| 86 |
@foo = trim(undef, undef); # @foo contains 2 undefined values |
| 87 |
trim(@foo) # @foo still contains 2 undefined values |
| 88 |
$foo = trim('', undef); # $foo is '' |
| 89 |
|
| 90 |
FUNCTIONS |
| 91 |
trim |
| 92 |
Removes leading and trailing whitespace from all arguments, or $_ if |
| 93 |
none are provided. |
| 94 |
|
| 95 |
rtrim |
| 96 |
Like "trim()" but removes only trailing (right) whitespace. |
| 97 |
|
| 98 |
ltrim |
| 99 |
Like "trim()" but removes only leading (left) whitespace. |
| 100 |
|
| 101 |
UNICODE |
| 102 |
Because this module is implemented using Perl regular expressions, it is |
| 103 |
capable of recognising and removing unicode whitespace characters (such |
| 104 |
as non-breaking spaces) from scalars with the utf8 flag on. See Encode |
| 105 |
for details about the utf8 flag. |
| 106 |
|
| 107 |
Note that this only applies in the case of perl versions after 5.8.0 or |
| 108 |
so. |
| 109 |
|
| 110 |
SEE ALSO |
| 111 |
Brent B. Powers' String::Strip performs a similar function in XS. |
| 112 |
|
| 113 |
AUTHORS |
| 114 |
Matt Lawrence <[email protected]> - Original author and maintainer |
| 115 |
|
| 116 |
Ryan Thompson <[email protected]> - Co-maintainer, miscellaneous fixes |
| 117 |
|
| 118 |
SUPPORT |
| 119 |
Issue Tracker: https://github.com/rjt-pl/Text-Trim/issues |
| 120 |
Bug reports and feature requests |
| 121 |
|
| 122 |
GitHub Repository: https://github.com/rjt-pl/Text-Trim.git |
| 123 |
|
| 124 |
|
| 125 |
ACKNOWLEDGEMENTS |
| 126 |
Terrence Brannon <[email protected]> for bringing my attention to |
| 127 |
String::Strip and suggesting documentation changes. |
| 128 |
|
| 129 |
LICENSE |
| 130 |
This program is free software; you can redistribute it and/or modify it |
| 131 |
under the same terms as Perl itself. |
| 132 |
|
| 133 |
<http://dev.perl.org/licenses/artistic.html> |
| 134 |
|