/usr/share/perl5/Text/Trim.pm is in libtext-trim-perl 1.02-2.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 | package Text::Trim;
use strict;
use warnings;
=head1 NAME
Text::Trim - remove leading and/or trailing whitespace from strings
=head1 VERSION
version 1.02
=cut
our $VERSION = '1.02';
=head1 SYNOPSIS
use Text::Trim;
$text = "\timportant data\n";
$data = trim $text;
# now $data contains "important data" and $text is unchanged
# or:
trim $text; # work in-place, $text now contains "important data"
@lines = <STDIN>;
rtrim @lines; # remove trailing whitespace from all lines
# Alternatively:
@lines = rtrim <STDIN>;
# Or even:
while (<STDIN>) {
trim; # Change $_ in place
# ...
}
=head1 DESCRIPTION
This module provides functions for removing leading and/or trailing whitespace
from strings. It is basically a wrapper around some simple regexes with a
flexible context-based interface.
=head1 EXPORTS
All functions are exported by default.
=cut
use Exporter;
our @ISA = qw( Exporter );
our @EXPORT = qw( rtrim ltrim trim );
=head1 CONTEXT HANDLING
=head2 void context
Functions called in void context change their arguments in-place
trim(@strings); # All strings in @strings are trimmed in-place
ltrim($text); # remove leading whitespace on $text
rtrim; # remove trailing whitespace on $_
No changes are made to arguments in non-void contexts.
=head2 list context
Values passed in are changed and returned without affecting the originals.
@result = trim(@strings); # @strings is unchanged
@result = rtrim; # @result contains rtrimmed $_
($result) = ltrim(@strings); # like $result = ltrim($strings[0]);
=head2 scalar context
As list context but multiple arguments are stringified before being returned.
Single arguments are unaffected. This means that under these circumstances, the
value of $" ($LIST_SEPARATOR) is used to join the values. If you don't want
this, make sure you only use single arguments when calling in scalar context.
@strings = ("\thello\n", "\tthere\n");
$trimmed = trim(@strings);
# $trimmed = "hello there"
local $" = ', ';
$trimmed = trim(@strings);
# Now $trimmed = "hello, there"
$trimmed = rtrim;
# $trimmed = $_ minus trailing whitespace
=head2 Undefined values
If any of the functions are called with undefined values, the behaviour is in
general to pass them through unchanged. When stringifying a list (calling in
scalar context with multiple arguments) undefined elements are excluded, but
if all elements are undefined then the return value is also undefined.
$foo = trim(undef); # $foo is undefined
$foo = trim(undef, undef); # $foo is undefined
@foo = trim(undef, undef); # @foo contains 2 undefined values
trim(@foo) # @foo still contains 2 undefined values
$foo = trim('', undef); # $foo is ''
=head1 FUNCTIONS
=head2 trim
Removes leading and trailing whitespace from all arguments, or $_ if none are
provided.
=cut
sub trim {
@_ = @_ ? @_ : $_ if defined wantarray;
for (@_ ? @_ : $_) { next unless defined; s/\A\s+//; s/\s+\z// }
return @_ if wantarray || !defined wantarray;
if (my @def = grep defined, @_) { return "@def" } else { return }
}
=head2 rtrim
Like trim() but removes only trailing (right) whitespace.
=cut
sub rtrim {
@_ = @_ ? @_ : $_ if defined wantarray;
for (@_ ? @_ : $_) { next unless defined; s/\s+\z// }
return @_ if wantarray || !defined wantarray;
if (my @def = grep defined, @_) { return "@def" } else { return }
}
=head2 ltrim
Like trim() but removes only leading (left) whitespace.
=cut
sub ltrim {
@_ = @_ ? @_ : $_ if defined wantarray;
for (@_ ? @_ : $_) { next unless defined; s/\A\s+// }
return @_ if wantarray || !defined wantarray;
if (my @def = grep defined, @_) { return "@def" } else { return }
}
1;
__END__
=head1 UNICODE
Because this module is implemented using perl regular expressions, it is capable
of recognising and removing unicode whitespace characters (such as non-breaking
spaces) from scalars with the utf8 flag on. See L<Encode> for details about the
utf8 flag.
Note that this only applies in the case of perl versions after 5.8.0 or so.
=head1 SEE ALSO
Brent B. Powers' L<String::Strip> performs a similar function in XS.
=head1 AUTHOR
Matt Lawrence E<lt>mattlaw@cpan.orgE<gt>
=head1 ACKNOWLEDGEMENTS
Terrence Brannon E<lt>metaperl@gmail.comE<gt> for bringing my attention to
L<String::Strip> and suggesting documentation changes.
=cut
vim: ts=8 sts=4 sw=4 sr et
|