Perlfunc: trim()

Perl function to trim leading and trailing whitespace.

sub trim {
    my $s = shift();
    if (!defined($s) || length($s) == 0) { return ""; }

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

    return $s;
}

Here is a version that with work with strings and arrays.

sub trim {
    if (wantarray) {
        my @ret;
        foreach (@_) {
            push(@ret,scalar(trim($_)));
        }

        return @ret;
    } else {
        my $s = shift();
        if (!defined($s) || length($s) == 0) { return ""; }
        $s =~ s/^\s*//;
        $s =~ s/\s*$//;

        return $s;
    }
}


Note: Replies will be formatted with PHP Markdown Extra syntax.

Name: Email (Not Required):
 
Logged IP: 54.236.35.159
To prevent spam please submit by clicking the kitten: