in reply to Is silent use of $_ for empty argument lists reasonable for "shortcut" functions?

Try something like:
sub trim { my (@out) = @_; foreach (@out) { s/^\s+//; s/\s+$//; } return wantarray ? @out : $out[0]; }
I think this avoids your problem.
  • Comment on Re: Is silent use of $_ for empty argument lists reasonable for "shortcut" functions?
  • Download Code

Replies are listed 'Best First'.
Re^2: Is silent use of $_ for empty argument lists reasonable for "shortcut" functions?
by Wyrdweaver (Beadle) on Aug 25, 2007 at 23:28 UTC
    No, the "shortcut" idiom modifies its arguments in place if called in void context. I only want to disconnect aliasing if the function is called in a non-void context.

    That way the function will return value(s) when asked or operate on it's arguments in place when called appropriately. Eg:
    @strs = ( " a string ", " b string "); $s = " my string "; $a = trim($s); # $a gets the trimmed version of $s @b = trim(@strs); # @b gets trimmed versions of all strings in @strs trim($s); # trim $s in-place trim(@strs); # trim all @str in-place
    - Wyrd
Re^2: Is silent use of $_ for empty argument lists reasonable for "shortcut" functions?
by bruceb3 (Pilgrim) on Aug 25, 2007 at 23:25 UTC
    How does this code differentiate between being call with no args and being called with an empty list?
      It doesn't. I misunderstood your question.