So, one of my favorite pieces of perl is Text::Trim, which uses contexts to do Just The Right Thing. Here is the trim() function, with some comments that I wrote:

sub trim { @_ = @_ ? @_ : $_ if defined wantarray; # When the caller is looking for a return value # (not void context), make a copy of the arguments, # so the original value is not changed. # If there are no arguments passed, # copy the value of $_ instead. for (@_ ? @_ : $_) { next unless defined; s/\A\s+//; s/\s+\z// } # If @_ contains any values, perform the # substitution on all defined values. If @_ contains # no values, perform the substitution on $_ instead. return @_ if wantarray || !defined wantarray; # return the changed values in array context # or (for a reason which escapes me) void context. if (my @def = grep defined, @_) { return "@def" } else { return } # In scalar context, return the results of # the substitution on all # defined values, if any. Otherwise just return. }

This code is pretty hard to read, mostly because of the punctuation variables. But it allows the caller to do

@trimmed = trim(@not_trimmed);

or

foreach (@untrimmed) { say trim; }

or

trim(@data);

And the results make sense.

But, as with so many things, it doesn't work with lexical $_. I thought maybe I could get this to work with the new underscore _ prototype, but unfortunately that imposes scalar context on the argument. In other words,

sub trim (_;@) { # ... as above } my @data = (' a', ' b ' , 'c '); say trim (@data);

displays not "abc" but "3", since that's the value of @data in scalar context (the number of elements).

I can't think of any way around this that allows the flexibility of the Text::Trim calling conventions and also the use of lexical $_.

Any ideas?


In reply to underscore prototype imposing scalar context by Aaronrp

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.