sub myTrim { my $str = shift; #shift faster than @_ for one paramater $str =~ s/^\s*//; #no space at front $str =~ s/\s*$//; #no space at rear return $str; }
Trimming leading and trailing whitespace from a string is a FAQ. Like you, I think it's simplest and clearest to do it in two simple statements. To nickpick your solution, it's better to replace * with + like so:
$str =~ s/^\s+//; # no whitespace at front $str =~ s/\s+$//; # no whitespace at rear
See also:
In reply to Re^2: Mapping list to hash is dropping list items (trim whitespace)
by eyepopslikeamosquito
in thread Mapping list to hash is dropping list items
by almsdealer
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |