blib/lib/String/Trim.pm

filter: Statementstmt 100% 44 / 44 Branchbran 91.1% 31 / 34 Conditioncond n/a MC/DCmcdc n/a Subroutinesub 100% 4 / 4 Podpod 100% 1 / 1 totaltotal 96.3% 80 / 83 CC 24 SCAR 30.7
CC
24
Cov
96%
trim
24.1
BEGIN
1.0
BEGIN
1.0
CRAP 21.5
↑ Summary
1 package String::Trim;
2 # ABSTRACT: trim whitespace from your strings
3 9 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 9 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 9 our $VERSION = '0.005'; # VERSION
6  
7  
8 9 ▶use Exporter 5.57 qw(import);
Subroutine BEGIN: called CC=1 SCAR=0.0
9 9 our @EXPORT = qw(trim);
10  
11  
12 36 ▶sub trim { # Starting point: http://www.perlmonks.org/?node_id=36684
Subroutine trim: called CC=24 SCAR=31.8
Pod documented
13 36 my $t = qr{^\s+|\s+$};
14 36 ▶ if (defined wantarray) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined wantarray) { } 15 21 36
15 15 ▶ @_ = (@_ ? @_ : $_);
Branch: 1 branch
BranchTrueFalseTotal
@_ ? : 15 0 15
16 15 ▶ if (ref $_[0] eq 'ARRAY') {
Branch: 2 branches
BranchTrueFalseTotal
if (ref $_[0] eq 'ARRAY') { } 2 13 15
elsif (ref $_[0] eq 'HASH') { } 1 12 13
17 2 @_ = @{ $_[0] };
18 2 ▶ for (@_) { s{$t}{}g if defined $_ }
Branch: 1 branch
BranchTrueFalseTotal
if defined $_ 4 1 5
19 2 return \@_;
20 }
21 elsif (ref $_[0] eq 'HASH') {
22 1 foreach my $k (keys %{ $_[0] }) {
23 5 (my $nk = $k) =~ s{$t}{}g;
24 5 ▶ if (defined $_[0]->{$k}) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $_[0]{$k}) { } 4 1 5
25 4 ($_[0]->{$nk} = $_[0]->{$k}) =~ s{$t}{}g;
26 }
27 else {
28 1 $_[0]->{$nk} = undef;
29 }
30 5 ▶ delete $_[0]->{$k} unless $k eq $nk;
Branch: 1 branch
BranchTrueFalseTotal
unless $k eq $nk 2 3 5
31 }
32 }
33 else {
34 12 ▶ for (@_ ? @_ : $_) { s{$t}{}g if defined $_ }
Branch: 2 branches
BranchTrueFalseTotal
@_ ? : 12 0 12
if defined $_ 13 2 15
35 }
36 13 ▶ return wantarray ? @_ : $_[0];
Branch: 1 branch
BranchTrueFalseTotal
wantarray ? : 2 11 13
37 }
38 else {
39 21 ▶ if (ref $_[0] eq 'ARRAY') {
Branch: 2 branches
BranchTrueFalseTotal
if (ref $_[0] eq 'ARRAY') { } 2 19 21
elsif (ref $_[0] eq 'HASH') { } 1 18 19
40 2 ▶ for (@{ $_[0] }) { s{$t}{}g if defined $_ }
Branch: 1 branch
BranchTrueFalseTotal
if defined $_ 4 1 5
41 }
42 elsif (ref $_[0] eq 'HASH') {
43 1 foreach my $k (keys %{ $_[0] }) {
44 5 (my $nk = $k) =~ s{$t}{}g;
45 5 ▶ if (defined $_[0]->{$k}) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $_[0]{$k}) { } 4 1 5
46 4 ($_[0]->{$nk} = $_[0]->{$k}) =~ s{$t}{}g;
47 }
48 else {
49 1 $_[0]->{$nk} = undef;
50 }
51 5 ▶ delete $_[0]->{$k} unless $k eq $nk
Branch: 1 branch
BranchTrueFalseTotal
unless $k eq $nk 0 5 5
52 }
53 }
54 else {
55 18 ▶ for (@_ ? @_ : $_) { s{$t}{}g if defined $_ }
Branch: 2 branches
BranchTrueFalseTotal
@_ ? : 13 5 18
if defined $_ 27 3 30
56 }
57 }
58 }
59  
60  
61 9 1;
62  
63  
64  
65 =pod
66  
67 =encoding utf-8
68  
69 =head1 NAME
70  
71 String::Trim - trim whitespace from your strings
72  
73 =head1 VERSION
74  
75 version 0.005
76  
77 =head1 SYNOPSIS
78  
79 use String::Trim;
80  
81 print "Do it? ";
82 trim(my $response = <>);
83 print "'$response'\n";
84  
85 =head1 DESCRIPTION
86  
87 C<String::Trim> trims whitespace off your strings. L<chomp> trims only C<$/> (typically,
88 that's newline), but C<trim> will trim all leading and trailing whitespace.
89  
90 =head1 FUNCTIONS
91  
92 =head2 trim
93  
94 Returns the string with all leading and trailing whitespace removed. Trimming
95 undef gives you undef. Alternatively, you can trim in-place.
96  
97 my $var = ' my string ';
98 my $trimmed = trim($var);
99 # OR
100 trim($var);
101  
102 C<trim> also knows how to trim an array or arrayref:
103  
104 my @to_trim = (' one ', ' two ', ' three ');
105 my @trimmed = trim(@to_trim);
106 # OR
107 trim(@to_trim);
108 # OR
109 my $trimmed = trim(\@to_trim);
110 # OR
111 trim(\@to_trim);
112  
113 =head1 RATIONALE
114  
115 C<trim> is often used by beginners, who may not understand how to spin their own. While
116 L<String::Util> does have a C<trim> function, it depends on L<Debug::ShowStuff>, which
117 depends on L<Taint>, which fails the test suite, and doesn't appear to be maintained.
118 This module installs, is actively maintained, and has no non-core dependencies.
119  
120 Other options include L<Text::Trim> and L<String::Strip> (which is implemented in XS,
121 and is therefore likely to be very fast, but requires a C compiler).
122  
123 =head1 AVAILABILITY
124  
125 The latest version of this module is available from the Comprehensive Perl
126 Archive Network (CPAN). Visit L<http://www.perl.com/CPAN/> to find a CPAN
127 site near you, or see L<http://search.cpan.org/dist/String-Trim/>.
128  
129 The development version lives at L<http://github.com/doherty/String-Trim>
130 and may be cloned from L<git://github.com/doherty/String-Trim.git>.
131 Instead of sending patches, please fork this project using the standard
132 git and github infrastructure.
133  
134 =head1 SOURCE
135  
136 The development version is on github at L<http://github.com/doherty/String-Trim>
137 and may be cloned from L<git://github.com/doherty/String-Trim.git>
138  
139 =head1 BUGS AND LIMITATIONS
140  
141 No bugs have been reported.
142  
143 Please report any bugs or feature requests through the web interface at
144 L<https://github.com/doherty/String-Trim/issues>.
145  
146 =head1 CREDITS
147  
148 This module was inspired by code at L<http://www.perlmonks.org/?node_id=36684> written
149 by japhy (Jeff Pinyan), dragonchild, and Aristotle. This Perl module was written by Mike
150 Doherty.
151  
152 =head1 AUTHORS
153  
154 =over 4
155  
156 =item *
157  
158 Mike Doherty <doherty@cpan.org>
159  
160 =item *
161  
162 Jeff Pinyan <pinyan@cpan.org>
163  
164 =item *
165  
166 Rob Kinyon <rkinyon@cpan.org>
167  
168 =item *
169  
170 Αριστοτέλης Παγκαλτζής (Aristotle Pagaltzis) <aristotle@cpan.org>
171  
172 =back
173  
174 =head1 COPYRIGHT AND LICENSE
175  
176 This software is copyright (c) 2010 by Mike Doherty.
177  
178 This is free software; you can redistribute it and/or modify it under
179 the same terms as the Perl 5 programming language system itself.
180  
181 =cut
182  
183  
184 __END__
↑ Summary