Perlfunc: trim()

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

sub trim {
    my $s = shift();

    if (!defined($s)) {
        return undef;
    }

    $s =~ s/^\s*//u;
    $s =~ s/\s*$//u;

    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