| 1 | package Text::Trim; | ||||||||||||||||||||||||||||
| 2 | |||||||||||||||||||||||||||||
| 3 | 7 | ▶ | use strict; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 4 | 7 | ▶ | use warnings; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 5 | |||||||||||||||||||||||||||||
| 6 | =head1 NAME | ||||||||||||||||||||||||||||
| 7 | |||||||||||||||||||||||||||||
| 8 | Text::Trim - remove leading and/or trailing whitespace from strings | ||||||||||||||||||||||||||||
| 9 | |||||||||||||||||||||||||||||
| 10 | =head1 VERSION | ||||||||||||||||||||||||||||
| 11 | |||||||||||||||||||||||||||||
| 12 | version 1.04 | ||||||||||||||||||||||||||||
| 13 | |||||||||||||||||||||||||||||
| 14 | =cut | ||||||||||||||||||||||||||||
| 15 | |||||||||||||||||||||||||||||
| 16 | 7 | our $VERSION = '1.04'; | |||||||||||||||||||||||||||
| 17 | |||||||||||||||||||||||||||||
| 18 | =head1 SYNOPSIS | ||||||||||||||||||||||||||||
| 19 | |||||||||||||||||||||||||||||
| 20 | use Text::Trim; | ||||||||||||||||||||||||||||
| 21 | |||||||||||||||||||||||||||||
| 22 | $text = "\timportant data\n"; | ||||||||||||||||||||||||||||
| 23 | $data = trim $text; | ||||||||||||||||||||||||||||
| 24 | # now $data contains "important data" and $text is unchanged | ||||||||||||||||||||||||||||
| 25 | |||||||||||||||||||||||||||||
| 26 | # or: | ||||||||||||||||||||||||||||
| 27 | trim $text; # work in-place, $text now contains "important data" | ||||||||||||||||||||||||||||
| 28 | |||||||||||||||||||||||||||||
| 29 | @lines = <STDIN>; | ||||||||||||||||||||||||||||
| 30 | rtrim @lines; # remove trailing whitespace from all lines | ||||||||||||||||||||||||||||
| 31 | |||||||||||||||||||||||||||||
| 32 | # Alternatively: | ||||||||||||||||||||||||||||
| 33 | @lines = rtrim <STDIN>; | ||||||||||||||||||||||||||||
| 34 | |||||||||||||||||||||||||||||
| 35 | # Or even: | ||||||||||||||||||||||||||||
| 36 | while (<STDIN>) { | ||||||||||||||||||||||||||||
| 37 | trim; # Change $_ in place | ||||||||||||||||||||||||||||
| 38 | # ... | ||||||||||||||||||||||||||||
| 39 | } | ||||||||||||||||||||||||||||
| 40 | |||||||||||||||||||||||||||||
| 41 | =head1 DESCRIPTION | ||||||||||||||||||||||||||||
| 42 | |||||||||||||||||||||||||||||
| 43 | This module provides functions for removing leading and/or trailing whitespace | ||||||||||||||||||||||||||||
| 44 | from strings. It is basically a wrapper around some simple regexes with a | ||||||||||||||||||||||||||||
| 45 | flexible context-based interface. | ||||||||||||||||||||||||||||
| 46 | |||||||||||||||||||||||||||||
| 47 | =head1 EXPORTS | ||||||||||||||||||||||||||||
| 48 | |||||||||||||||||||||||||||||
| 49 | All functions are exported by default. | ||||||||||||||||||||||||||||
| 50 | |||||||||||||||||||||||||||||
| 51 | =cut | ||||||||||||||||||||||||||||
| 52 | |||||||||||||||||||||||||||||
| 53 | 7 | ▶ | use Exporter; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 54 | |||||||||||||||||||||||||||||
| 55 | 7 | our @ISA = qw( Exporter ); | |||||||||||||||||||||||||||
| 56 | 7 | our @EXPORT = qw( rtrim ltrim trim ); | |||||||||||||||||||||||||||
| 57 | |||||||||||||||||||||||||||||
| 58 | =head1 CONTEXT HANDLING | ||||||||||||||||||||||||||||
| 59 | |||||||||||||||||||||||||||||
| 60 | =head2 void context | ||||||||||||||||||||||||||||
| 61 | |||||||||||||||||||||||||||||
| 62 | Functions called in void context change their arguments in-place | ||||||||||||||||||||||||||||
| 63 | |||||||||||||||||||||||||||||
| 64 | trim(@strings); # All strings in @strings are trimmed in-place | ||||||||||||||||||||||||||||
| 65 | |||||||||||||||||||||||||||||
| 66 | ltrim($text); # remove leading whitespace on $text | ||||||||||||||||||||||||||||
| 67 | |||||||||||||||||||||||||||||
| 68 | rtrim; # remove trailing whitespace on $_ | ||||||||||||||||||||||||||||
| 69 | |||||||||||||||||||||||||||||
| 70 | No changes are made to arguments in non-void contexts. | ||||||||||||||||||||||||||||
| 71 | |||||||||||||||||||||||||||||
| 72 | =head2 list context | ||||||||||||||||||||||||||||
| 73 | |||||||||||||||||||||||||||||
| 74 | Values passed in are changed and returned without affecting the originals. | ||||||||||||||||||||||||||||
| 75 | |||||||||||||||||||||||||||||
| 76 | @result = trim(@strings); # @strings is unchanged | ||||||||||||||||||||||||||||
| 77 | |||||||||||||||||||||||||||||
| 78 | @result = rtrim; # @result contains rtrimmed $_ | ||||||||||||||||||||||||||||
| 79 | |||||||||||||||||||||||||||||
| 80 | ($result) = ltrim(@strings); # like $result = ltrim($strings[0]); | ||||||||||||||||||||||||||||
| 81 | |||||||||||||||||||||||||||||
| 82 | =head2 scalar context | ||||||||||||||||||||||||||||
| 83 | |||||||||||||||||||||||||||||
| 84 | As list context but multiple arguments are stringified before being returned. | ||||||||||||||||||||||||||||
| 85 | Single arguments are unaffected. This means that under these circumstances, | ||||||||||||||||||||||||||||
| 86 | the value of C<$"> (C<$LIST_SEPARATOR>) is used to join the values. If you | ||||||||||||||||||||||||||||
| 87 | don't want this, make sure you only use single arguments when calling in | ||||||||||||||||||||||||||||
| 88 | scalar context. | ||||||||||||||||||||||||||||
| 89 | |||||||||||||||||||||||||||||
| 90 | @strings = ("\thello\n", "\tthere\n"); | ||||||||||||||||||||||||||||
| 91 | $trimmed = trim(@strings); | ||||||||||||||||||||||||||||
| 92 | # $trimmed = "hello there" | ||||||||||||||||||||||||||||
| 93 | |||||||||||||||||||||||||||||
| 94 | local $" = ', '; | ||||||||||||||||||||||||||||
| 95 | $trimmed = trim(@strings); | ||||||||||||||||||||||||||||
| 96 | # Now $trimmed = "hello, there" | ||||||||||||||||||||||||||||
| 97 | |||||||||||||||||||||||||||||
| 98 | $trimmed = rtrim; | ||||||||||||||||||||||||||||
| 99 | # $trimmed = $_ minus trailing whitespace | ||||||||||||||||||||||||||||
| 100 | |||||||||||||||||||||||||||||
| 101 | =head2 Undefined values | ||||||||||||||||||||||||||||
| 102 | |||||||||||||||||||||||||||||
| 103 | If any of the functions are called with undefined values, the behaviour is in | ||||||||||||||||||||||||||||
| 104 | general to pass them through unchanged. When stringifying a list (calling in | ||||||||||||||||||||||||||||
| 105 | scalar context with multiple arguments) undefined elements are excluded, but | ||||||||||||||||||||||||||||
| 106 | if all elements are undefined then the return value is also undefined. | ||||||||||||||||||||||||||||
| 107 | |||||||||||||||||||||||||||||
| 108 | $foo = trim(undef); # $foo is undefined | ||||||||||||||||||||||||||||
| 109 | $foo = trim(undef, undef); # $foo is undefined | ||||||||||||||||||||||||||||
| 110 | @foo = trim(undef, undef); # @foo contains 2 undefined values | ||||||||||||||||||||||||||||
| 111 | trim(@foo) # @foo still contains 2 undefined values | ||||||||||||||||||||||||||||
| 112 | $foo = trim('', undef); # $foo is '' | ||||||||||||||||||||||||||||
| 113 | |||||||||||||||||||||||||||||
| 114 | =head1 FUNCTIONS | ||||||||||||||||||||||||||||
| 115 | |||||||||||||||||||||||||||||
| 116 | =head2 trim | ||||||||||||||||||||||||||||
| 117 | |||||||||||||||||||||||||||||
| 118 | Removes leading and trailing whitespace from all arguments, or C<$_> if none | ||||||||||||||||||||||||||||
| 119 | are provided. | ||||||||||||||||||||||||||||
| 120 | |||||||||||||||||||||||||||||
| 121 | =cut | ||||||||||||||||||||||||||||
| 122 | |||||||||||||||||||||||||||||
| 123 | 14 | ▶ | sub trim { | ||||||||||||||||||||||||||
|
Subroutine
trim: called CC=9 SCAR=22.0
Pod
documented
| |||||||||||||||||||||||||||||
| 124 | 14 | ▶ | @_ = @_ ? @_ : $_ if defined wantarray; | ||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||
| 125 | |||||||||||||||||||||||||||||
| 126 | 14 | ▶ | for (@_ ? @_ : $_) { next unless defined; s/\A\s+//; s/\s+\z// } | ||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||
| 127 | |||||||||||||||||||||||||||||
| 128 | 14 | ▶ | return @_ if wantarray || !defined wantarray; | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%3/3
wantarray or not defined wantarray
MC/DC100%2/2
wantarray or not defined wantarray
wantarray
not defined wantarray
Truth table100%3/3
A or not(B)
A= wantarrayB= not defined wantarray
| |||||||||||||||||||||||||||||
| 129 | |||||||||||||||||||||||||||||
| 130 | 6 | ▶ | if (my @def = grep defined, @_) { return "@def" } else { return } | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 131 | } | ||||||||||||||||||||||||||||
| 132 | |||||||||||||||||||||||||||||
| 133 | =head2 rtrim | ||||||||||||||||||||||||||||
| 134 | |||||||||||||||||||||||||||||
| 135 | Like C<trim()> but removes only trailing (right) whitespace. | ||||||||||||||||||||||||||||
| 136 | |||||||||||||||||||||||||||||
| 137 | =cut | ||||||||||||||||||||||||||||
| 138 | |||||||||||||||||||||||||||||
| 139 | 12 | ▶ | sub rtrim { | ||||||||||||||||||||||||||
|
Subroutine
rtrim: called CC=9 SCAR=22.0
Pod
documented
| |||||||||||||||||||||||||||||
| 140 | 12 | ▶ | @_ = @_ ? @_ : $_ if defined wantarray; | ||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||
| 141 | |||||||||||||||||||||||||||||
| 142 | 12 | ▶ | for (@_ ? @_ : $_) { next unless defined; s/\s+\z// } | ||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||
| 143 | |||||||||||||||||||||||||||||
| 144 | 12 | ▶ | return @_ if wantarray || !defined wantarray; | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%3/3
wantarray or not defined wantarray
MC/DC100%2/2
wantarray or not defined wantarray
wantarray
not defined wantarray
Truth table100%3/3
A or not(B)
A= wantarrayB= not defined wantarray
| |||||||||||||||||||||||||||||
| 145 | |||||||||||||||||||||||||||||
| 146 | 5 | ▶ | if (my @def = grep defined, @_) { return "@def" } else { return } | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 147 | } | ||||||||||||||||||||||||||||
| 148 | |||||||||||||||||||||||||||||
| 149 | =head2 ltrim | ||||||||||||||||||||||||||||
| 150 | |||||||||||||||||||||||||||||
| 151 | Like C<trim()> but removes only leading (left) whitespace. | ||||||||||||||||||||||||||||
| 152 | |||||||||||||||||||||||||||||
| 153 | =cut | ||||||||||||||||||||||||||||
| 154 | |||||||||||||||||||||||||||||
| 155 | 12 | ▶ | sub ltrim { | ||||||||||||||||||||||||||
|
Subroutine
ltrim: called CC=9 SCAR=22.0
Pod
documented
| |||||||||||||||||||||||||||||
| 156 | 12 | ▶ | @_ = @_ ? @_ : $_ if defined wantarray; | ||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||
| 157 | |||||||||||||||||||||||||||||
| 158 | 12 | ▶ | for (@_ ? @_ : $_) { next unless defined; s/\A\s+// } | ||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||
| 159 | |||||||||||||||||||||||||||||
| 160 | 12 | ▶ | return @_ if wantarray || !defined wantarray; | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%3/3
wantarray or not defined wantarray
MC/DC100%2/2
wantarray or not defined wantarray
wantarray
not defined wantarray
Truth table100%3/3
A or not(B)
A= wantarrayB= not defined wantarray
| |||||||||||||||||||||||||||||
| 161 | |||||||||||||||||||||||||||||
| 162 | 5 | ▶ | if (my @def = grep defined, @_) { return "@def" } else { return } | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 163 | } | ||||||||||||||||||||||||||||
| 164 | |||||||||||||||||||||||||||||
| 165 | 7 | 1; | |||||||||||||||||||||||||||
| 166 | |||||||||||||||||||||||||||||
| 167 | __END__ | ||||||||||||||||||||||||||||