I may be missing some subtleties here, but if you want a no-arg call to operate on $_ (handy shortcut) and you don't want a call with an empty array arg to operation on $_ by mistake (nasty unexpected side-effect), the way to write the sub would (I think) be like this:
sub trim { my @args = ( @_ ) ? @_ : $_; for ( @args ) { s/\A\s+//; s/\s+\z//; } return wantarray ? @args : $args[0]; }
If you pass an empty array to that verion, it will not modify $_.

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.


In reply to Re: Is silent use of $_ for empty argument lists reasonable for "shortcut" functions? by graff
in thread Is silent use of $_ for empty argument lists reasonable for "shortcut" functions? by Wyrdweaver

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.