Perlfunc: trim()

Perl function to trim leading and trailing whitespace, borrowed from String::Util.

sub trim {
    my ($s) = (@_, $_); # Passed in var, or default to $_
    if (!defined($s) || length($s) == 0) { return ""; }
    $s =~ s/^\s*//;
    $s =~ s/\s*$//;

    return $s;
}

To trim an each item in an array you can do:

@array = map { &trim($_) } @array;
Leave A Reply
All content licensed under the Creative Commons License