Text-Trim-1.02000755001751001751 011355104254 14410 5ustar00mlawrencemlawrence000000000000README000444001751001751 717211355104254 15355 0ustar00mlawrencemlawrence000000000000Text-Trim-1.02NAME Text::Trim - remove leading and/or trailing whitespace from strings VERSION version 1.02 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 = ; rtrim @lines; # remove trailing whitespace from all lines # Alternatively: @lines = rtrim ; # Or even: while () { trim; # Change $_ in place # ... } 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. EXPORTS All functions are exported by default. CONTEXT HANDLING 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. 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]); 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 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 '' FUNCTIONS trim Removes leading and trailing whitespace from all arguments, or $_ if none are provided. rtrim Like trim() but removes only trailing (right) whitespace. ltrim Like trim() but removes only leading (left) whitespace. 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 Encode for details about the utf8 flag. Note that this only applies in the case of perl versions after 5.8.0 or so. SEE ALSO Brent B. Powers' String::Strip performs a similar function in XS. AUTHOR Matt Lawrence ACKNOWLEDGEMENTS Terrence Brannon for bringing my attention to String::Strip and suggesting documentation changes. Build.PL000444001751001751 55011355104254 15742 0ustar00mlawrencemlawrence000000000000Text-Trim-1.02use strict; use warnings; use Module::Build; Module::Build->new( module_name => 'Text::Trim', requires => { perl => '5', }, build_requires => { 'Test::More' => 0, }, license => 'perl', create_makefile_pl => 'passthrough', create_readme => 1, )->create_build_script; Makefile.PL000444001751001751 234611355104254 16445 0ustar00mlawrencemlawrence000000000000Text-Trim-1.02# Note: this file was auto-generated by Module::Build::Compat version 0.33 require 5; unless (eval "use Module::Build::Compat 0.02; 1" ) { print "This module requires Module::Build to install itself.\n"; require ExtUtils::MakeMaker; my $yn = ExtUtils::MakeMaker::prompt (' Install Module::Build now from CPAN?', 'y'); unless ($yn =~ /^y/i) { die " *** Cannot install without Module::Build. Exiting ...\n"; } require Cwd; require File::Spec; require CPAN; # Save this 'cause CPAN will chdir all over the place. my $cwd = Cwd::cwd(); CPAN::Shell->install('Module::Build::Compat'); CPAN::Shell->expand("Module", "Module::Build::Compat")->uptodate or die "Couldn't install Module::Build, giving up.\n"; chdir $cwd or die "Cannot chdir() back to $cwd: $!"; } eval "use Module::Build::Compat 0.02; 1" or die $@; Module::Build::Compat->run_build_pl(args => \@ARGV); my $build_script = 'Build'; $build_script .= '.com' if $^O eq 'VMS'; exit(0) unless(-e $build_script); # cpantesters convention require Module::Build; Module::Build::Compat->write_makefile(build_class => 'Module::Build'); MANIFEST000444001751001751 25711355104254 15603 0ustar00mlawrencemlawrence000000000000Text-Trim-1.02Build.PL Changes lib/Text/Trim.pm Makefile.PL MANIFEST This list of files META.yml README t/01..trim.t t/02..ltrim.t t/03..rtrim.t t/04..unicode.t t/05..undef.t t/99..pod.t META.yml000444001751001751 72711355104254 15725 0ustar00mlawrencemlawrence000000000000Text-Trim-1.02--- name: Text-Trim version: 1.02 author: - 'Matt Lawrence Emattlaw@cpan.orgE' abstract: remove leading and/or trailing whitespace from strings license: perl resources: license: http://dev.perl.org/licenses/ requires: perl: 5 build_requires: Test::More: 0 provides: Text::Trim: file: lib/Text/Trim.pm version: 1.02 generated_by: Module::Build version 0.33 meta-spec: url: http://module-build.sourceforge.net/META-spec-v1.4.html version: 1.4 Changes000444001751001751 26711355104254 15746 0ustar00mlawrencemlawrence000000000000Text-Trim-1.02 1.01 Mon Feb 27 2006 - Added reference to String::Strip in docs - Added docs and tests demonstrating unicode support 1.00 Wed Feb 22 2006 - Initial version lib000755001751001751 011355104254 15077 5ustar00mlawrencemlawrence000000000000Text-Trim-1.02Text000755001751001751 011355104254 16023 5ustar00mlawrencemlawrence000000000000Text-Trim-1.02/libTrim.pm000444001751001751 1070611355104254 17455 0ustar00mlawrencemlawrence000000000000Text-Trim-1.02/lib/Textpackage 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 = ; rtrim @lines; # remove trailing whitespace from all lines # Alternatively: @lines = rtrim ; # Or even: while () { 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 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 performs a similar function in XS. =head1 AUTHOR Matt Lawrence Emattlaw@cpan.orgE =head1 ACKNOWLEDGEMENTS Terrence Brannon Emetaperl@gmail.comE for bringing my attention to L and suggesting documentation changes. =cut vim: ts=8 sts=4 sw=4 sr et t000755001751001751 011355104254 14574 5ustar00mlawrencemlawrence000000000000Text-Trim-1.0204..unicode.t000444001751001751 131311355104254 17042 0ustar00mlawrencemlawrence000000000000Text-Trim-1.02/tuse strict; use warnings; use Test::More tests => 4; BEGIN { use_ok('Text::Trim') } SKIP: { eval { require Encode }; if ($@) { skip("Unicode tests require Encode.pm (core with 5.7.3 and later)", 3); } import Encode qw( decode :fallbacks ); # unicode non-breaking space; my $nbsp = "\xc2\xa0"; my $text = my $orig = decode('UTF-8', "$nbsp$nbsp\tFoo Bar Baz$nbsp\t\r\n", FB_WARN()); my $expected = 'Foo Bar Baz'; is(trim($text), $expected, 'trim with unicode whitespace works'); is($text, $orig, 'original string unaffected'); trim($text); is($text, $expected, 'works in void context too'); } __END__ vim: ft=perl ts=8 sts=4 sw=4 sr et 03..rtrim.t000444001751001751 177011355104254 16557 0ustar00mlawrencemlawrence000000000000Text-Trim-1.02/tuse strict; use warnings; use Test::More tests => 10; BEGIN { use_ok('Text::Trim'); } my $text = my $orig = "\t foo\t \t\n"; my $expected = "\t foo"; is(rtrim($text), $expected, 'rtrim in scalar context'); is($text, $orig, ".. didn't affect original"); rtrim($text); is($text, $expected, 'rtrim in place changes original'); { local $_ = $orig; my $rtrimmed = rtrim; is($rtrimmed, $expected, '$scalar = rtrim() works on $_'); is($_, , $orig, ".. didn't affect original"); rtrim; is($_, , $expected, "rtrim() alters \$_") } my @before = ( " foo ", "\tbar\t", "\nbaz\n", ); my @expected = (" foo", "\tbar", "\nbaz"); my @rtrimmed = rtrim @before; is_deeply(\@rtrimmed, \@expected, 'rtrim on a list in list content'); rtrim @before; is_deeply(\@before, \@expected, 'rtrim on a list in place'); $expected = "@expected"; my $rtrimmed = rtrim @before; is($rtrimmed, $expected, 'rtrim on a list in scalar context'); __END__ vim: ft=perl ts=8 sts=4 sw=4 sr et 02..ltrim.t000444001751001751 177411355104254 16554 0ustar00mlawrencemlawrence000000000000Text-Trim-1.02/tuse strict; use warnings; use Test::More tests => 10; BEGIN { use_ok('Text::Trim'); } my $text = my $orig = "\t foo\t \t\n"; my $expected = "foo\t \t\n"; is(ltrim($text), $expected, 'ltrim in scalar context'); is($text, $orig, ".. didn't affect original"); ltrim($text); is($text, $expected, 'ltrim in place changes original'); { local $_ = $orig; my $ltrimmed = ltrim; is($ltrimmed, $expected, '$scalar = ltrim() works on $_'); is($_, , $orig, ".. didn't affect original"); ltrim; is($_, , $expected, "ltrim() alters \$_") } my @before = ( " foo ", "\tbar\t", "\nbaz\n", ); my @expected = ("foo ", "bar\t", "baz\n"); my @ltrimmed = ltrim @before; is_deeply(\@ltrimmed, \@expected, 'ltrim on a list in list content'); ltrim @before; is_deeply(\@before, \@expected, 'ltrim on a list in place'); $expected = "@expected"; my $ltrimmed = ltrim @before; is($ltrimmed, $expected, 'ltrim on a list in scalar context'); __END__ vim: ft=perl ts=8 sts=4 sw=4 sr et 01..trim.t000444001751001751 166311355104254 16374 0ustar00mlawrencemlawrence000000000000Text-Trim-1.02/tuse strict; use warnings; use Test::More tests => 10; BEGIN { use_ok('Text::Trim'); } my $text = my $orig = "\t foo\t \t\n"; is(trim($text), 'foo', 'trim in scalar context'); is($text, $orig, ".. didn't affect original"); trim($text); is($text, 'foo', 'trim in place changes original'); { local $_ = $orig; my $trimmed = trim; is($trimmed, 'foo', '$scalar = trim() works on $_'); is($_, , $orig, ".. didn't affect original"); trim; is($_, , 'foo', "trim() alters \$_") } my @before = ( " foo ", "\tbar\t", "\nbaz\n", ); my @expected = qw( foo bar baz ); my @trimmed = trim @before; is_deeply(\@trimmed, \@expected, 'trim on a list in list content'); trim @before; is_deeply(\@before, \@expected, 'trim on a list in place'); my $expected = "@expected"; my $trimmed = trim @before; is($trimmed, $expected, 'trim on a list in scalar context'); __END__ vim: ft=perl ts=8 sts=4 sw=4 sr et 05..undef.t000444001751001751 303611355104254 16522 0ustar00mlawrencemlawrence000000000000Text-Trim-1.02/tuse strict; use warnings; use Test::More tests => 27; use Text::Trim; local $SIG{__WARN__} = sub { push our @warnings, @_ }; my %op = do { no strict 'refs'; map { $_ => \&{$_} } qw( trim ltrim rtrim ) }; # Some data interspersed with undef my @list = (" x ", undef, " y", undef, "\tz\n"); my %expect = ( ltrim => { list => [ "x ", undef, "y", undef, "z\n" ], scalar => "x :y:z\n", }, rtrim => { list => [ " x", undef, " y", undef, "\tz" ], scalar => " x: y:\tz", }, trim => { list => [ "x", undef, "y", undef, "z" ], scalar => "x:y:z", } ); for my $op (keys %op) { my $trim = $op{$op}; my $expect = $expect{$op}; { local our @warnings; is $trim->(undef), undef, "scalar $op(undef)"; is @warnings, 0, '.. no warnings'; } { local our @warnings; my $out = do { local $" = ':'; $trim->(@list) }; is $out, $expect->{scalar}, "$op list with undefs"; is @warnings, 0, '.. no warnings'; } { local our @warnings; my @out = $trim->(@list); is_deeply \@out, $expect->{list}, "$op list in list context"; is @warnings, 0, '.. no warnings'; } { local our @warnings; $trim->(undef); is @warnings, 0, "void $op(undef) produces no warnings" } { local our @warnings; my @copy = @list; $trim->(@copy); is_deeply \@copy, $expect->{list}, "void $op list"; is @warnings, 0, '.. no warnings'; } } 99..pod.t000444001751001751 144311355104254 16220 0ustar00mlawrencemlawrence000000000000Text-Trim-1.02/t BEGIN { our @modules = qw( Text::Trim ); } use File::Spec::Functions qw( catfile ); use Test::More tests => our @modules * 2; SKIP: { eval 'use Test::Pod'; skip "Test::Pod not installed", scalar @modules if $@; for my $module (@modules) { my @path = ('lib', split('::', $module)); my $file = pop(@path) . '.pm'; pod_file_ok(catfile(@path, $file), "$module pod ok"); } } SKIP: { eval 'use Test::Pod::Coverage'; skip "Test::Pod::Coverage not installed", scalar @modules if $@; for my $module (@modules) { pod_coverage_ok( $module, { also_private => [ qr(^[[:upper:][:digit:]_]+$) ] }, "$module pod coverage ok" ); } } __END__ vim: ft=perl ts=8 sts=4 sw=4 sr et