Wyrdweaver has asked for the wisdom of the Perl Monks concerning the following question:
On the one hand, it allows perlish natural use of the function, such as:sub trim { @_ = @_ ? @_ : $_ if defined wantarray; # disconnect aliases in no +n-void context for (@_ ? @_ : $_) { s/\A\s+//; s/\s+\z// } return wantarray ? @_ : "@_"; }
But, on the other hand, empty argument lists may bite you by operating unexpectedly on '$_':foreach (@args) { <...>; trim; <...>; }
I'm honestly tempted to rewrite the "shortcut" function as:foreach (@args) { my @b; <...> @b = (); trim(@b); # silently the same as trim($_) since @b has no elements <...>; }
In the best of all worlds, I'd rewrite the function to differentiate between "trim()" and "trim( () )". But, given the implementation of subroutine argument passing, I haven't come across or originated any way of doing that. (Let me know if you have...)sub trim { if ( !@_ && !defined(wantarray) ) { carp 'Useless use of trim with + no arguments in void return context (did you mean "trim($_)"?)'; ret +urn; } if ( !@_ ) { carp "Useless use of trim with no arguments"; return; + } @_ = @_ if defined wantarray; for (@_) { s/\A\s+//; s/\s+\z// } return wantarray ? @_ : "@_"; }
|
|---|