in reply to Is silent use of $_ for empty argument lists reasonable for "shortcut" functions?
Most of the time I wouldn't be tempted to write functions that operate on $_ if given no arguments. trim($_) is just not a big hardship compared to trim() (although I do use implicit $_ in some cases, I usually don't with chomp(), for example).
If I had a function that used $_ when given no arguments, then I'd likely not support taking a list of arguments (because of the ambiguity you noticed). Though, if you aren't likely to run into a relatively old version of Perl, then there is a somewhat ugly way around the ambiguity problem that I'm reluctant to mention because somebody would probably use it:
sub trim(;\[$@]@) { return _trim( $_ ) if ! @_; my $ref= shift @_; return _trim( eval{@$ref;1} ? @$ref : $$ref, @_ ) }
The ugliest part being that it breaks simple things like, my $l= trim(<STDIN>).
- tye
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re^2: Is silent use of $_ for empty argument lists reasonable for "shortcut" functions? (E2CUTE)
by Wyrdweaver (Beadle) on Aug 26, 2007 at 09:36 UTC | |
by lodin (Hermit) on Aug 26, 2007 at 10:48 UTC | |
by Wyrdweaver (Beadle) on Aug 26, 2007 at 18:00 UTC | |
by mr_mischief (Monsignor) on Aug 28, 2007 at 15:06 UTC |