It's the typical trim(), with optional in-place
behavior, and $_ argument defaulting.
sub trim { @_ = $_ if not @_ and defined wantarray; @_ = @_ if defined wantarray; for (@_ ? @_ : $_) { s/^\s+//, s/\s+$// } return wantarray ? @_ : $_[0] if defined wantarray; } trim; # trims $_ inplace $new = trim; # trims (and returns) a copy of $_ trim $str; # trims $str inplace $new = trim $str; # trims (and returns) a copy of $str trim @list; # trims @list inplace @new = trim @list; # trims (and returns) a copy of @list
|
---|
Replies are listed 'Best First'. | |
---|---|
Re: trim() magic
by dragonchild (Archbishop) on Mar 30, 2005 at 14:21 UTC | |
Re: trim() magic
by Roy Johnson (Monsignor) on Jan 11, 2006 at 19:43 UTC | |
by japhy (Canon) on Jan 11, 2006 at 20:00 UTC | |
Re: trim() magic
by Aristotle (Chancellor) on Jan 13, 2006 at 03:48 UTC | |
by Aristotle (Chancellor) on Dec 01, 2010 at 02:40 UTC |
Back to
Cool Uses for Perl