I describe each sub, and then put a few comments here and there. But when I need to use this sub somewhere in a program, I usually just include a condensed version.
################################################# Trim
#
# This function removes whitespace, newline
# characters and other special characters
# before and after STRING. Returns a new string.
#
# Usage: STRING = Trim(STRING)
#
sub Trim
{
my $T = $_[0];
defined $T or return '';
my $N = length($T);
my $X = 0; # ptr to first non-whitespace
my $Y = 0; # ptr to last non-whitespace
while ($N--)
{
if (vec($T, $N, 8) > 32)
{
$X = $N;
$Y or $Y = $N + 1;
}
}
return substr($T, $X, $Y - $X);
}
Condensed version:
sub Trim { my $T = $_[0]; defined $T or return ''; my $N = length($T); my $X = 0; my $Y = 0; while ($N--) { if (vec($T, $N, 8) > 32) { $X = $N; $Y or $Y = $N + 1; } } return substr($T, $X, $Y - $X); }