There's more than one way to do things | |
PerlMonks |
underscore prototype imposing scalar contextby Aaronrp (Scribe) |
on Mar 10, 2012 at 01:04 UTC ( [id://958820]=perlquestion: print w/replies, xml ) | Need Help?? |
Aaronrp has asked for the wisdom of the Perl Monks concerning the following question: 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:
This code is pretty hard to read, mostly because of the punctuation variables. But it allows the caller to do
or
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,
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?
Back to
Seekers of Perl Wisdom
|
|