Dear Brothers,
I would like to take arbitrary text input and manipulate it a bit. The input will be in a single scalar: $string. It might be null, empty, multiline or very large. I want to break everything into "words"; trim any leading, trailing or embedded white space; and get rid of line endings.
I have tried the following but the "map" is not doing what i expected. Is there an existing module that will do this better? What am I missing in this routine? Am I on the right track?
sub wordBreak {
my ($string) = @_;
my $tmp = "";
my @result = ();
my @array = ();
#
# break the input into "lines"
#
@array = split(/^/m, $string);
#
# turn on swallow ending white space mode and
# chomp the array elements
#
$tmp = $/;
$/ = "";
chomp(@array) if ($#array >= 0);
$/ = $tmp;
@result = split(
' ', # split the one line into words
join(
' ', # join the "lines" into one
map {
s/\s+/ /g; # get rid of extra whitespace
s/^ //;
s/ $//;
} @array
)
) if ($#array >= 0);
return @result;
}