sub mytrim { $_[0] =~ s/^\s+|\s+$//g; } my $text = ' this could use a trim. '; mytrim $text;