#!/usr/bin/perl use strict; use warnings; use feature 'say'; sub trim { my ($str) = @_; $str =~ s/^\s+|\s+$//g; return $str; } my $test = ' sample '; say 'Before (trim):'; say $test; say trim($test); say 'Without (trim):'; say $test; $test =~ s/^\s+|\s+$//g; say $test; __END__ $ perl test.pl Before (trim): sample sample Without (trim): sample sample