$|=1; # So it works in the IDE

@a = ("    space in front","space in back  ", "    space everywhere     ");

# You can choose any of the following 3 versions:
foreach $e (@a){
  # Two line version (kind of a Perl standard):
  $e =~ s/^\s+//; # delete leading space
  $e =~ s/\s+$//; # delete trailing space

  # One line version 1
  #$line =~ s/^\s*(.*?)\s*$/$1/; # Replace the string with stuff between leading
                                 # and trailing spaces. The saved match has to be
                                 # non-greedy!
  # One line version 2
  #$line =~ s/(^\s+|\s+$)//g; # Delete either leading or trailing spaces,
                              # but use the "g" modifier to do it twice!

  print "New string: |$e|\n"; # the pipes are there to see start and end
}

