If you're using 5.10, you can use the new _ (underscore) prototype to create subroutines that magically use either a passed parameter or $_:

sub display_thing (_) { print $_[0] }; $_='Hello 5.10'; display_thing;

Another way would be to just wrap display_thing with your own wrapper that handles the decision between $_ and @_:

my $old_display_thing = \&display_thing; *display_thing = sub { if (@_) { &$old_display_thing(@_) } else { &$old_display_thing($_) }; };

But if you're rewriting or wrapping display_thing (and I see no way around rewriting or wrapping it), why not rewrite or wrap it to accept multiple arguments straight away?

sub display_thing { for (@_) { print "Displaying thing >$_<\n"; }; };

In reply to Re: Is it possible to write a sub that understand default variables ? by Corion
in thread Is it possible to write a sub that understand default variables ? by bcarnazzi

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.