in reply to Is silent use of $_ for empty argument lists reasonable for "shortcut" functions?
If you pass an empty array to that verion, it will not modify $_.sub trim { my @args = ( @_ ) ? @_ : $_; for ( @args ) { s/\A\s+//; s/\s+\z//; } return wantarray ? @args : $args[0]; }
Update -- test suite:
#!/usr/bin/perl use strict; sub trim { my @args = ( @_ ) ? @_ : $_; for ( @args ) { s/\A\s+//; s/\s+\z//; } return wantarray ? @args : $args[0]; } $_ = " test "; print "test 1: ==" . trim . "==$_==\n"; $_ = " 1234 "; my @b = (); trim( @b ); print "test 2: ==". $_ . "==@b==\n"; my @c = ( " a ", " b ", " c " ); my $d = join "", trim( @c ); print "test 3: ==". $_ . "==" . $d . "==@c==\n"; __OUTPUT__ test 1: ==test== test == test 2: == 1234 ==== test 3: == 1234 ==abc== a b c ==
Having updated "test 1" to print out both the return value of trim and $_, I see that $_ is never modified, which I gather is not what you want. Sorry -- I get your point now, and I don't have an answer.
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re^2: Is silent use of $_ for empty argument lists reasonable for "shortcut" functions?
by ikegami (Patriarch) on Aug 27, 2007 at 17:15 UTC |