in reply to Can I change the definition of '\s'?

$data =~ s/^\s+|\s+$/;
It might just be a typo, but as written that code will only trim trailing whitespace if there is no leading whitespace... In other words, if a string has leading whitespace it won't ever check for trailing whitespace. You'll need the /g modifier to get your example code to match your spec.
$data =~ s/^\s+|\s+$/g;

-Blake