Perl: function to trim string leading and trailing whitespace

Landon Kuhn picture Landon Kuhn · Jan 4, 2011 · Viewed 130.3k times · Source

Is there a built-in function to trim leading and trailing whitespace such that trim(" hello world ") eq "hello world"?

Answer

Mark Byers picture Mark Byers · Jan 4, 2011

Here's one approach using a regular expression:

$string =~ s/^\s+|\s+$//g ;     # remove both leading and trailing whitespace

Perl 6 will include a trim function:

$string .= trim;

Source: Wikipedia