CheeseLord decides to risk exposing a possible misunderstanding of context issues...

Actually, you're on the right track. The final line of the function is in void context because the function itself was called in void context. Tweaking your example a bit will show this:

#!/usr/bin/perl -w use strict; use Void qw( lc ); # My lc sub lc_and_chop(\$) { chop ${$_[0]}; lc ${$_[0]}; } my $foo = 'fUnKy'; lc_and_chop $foo; print "$foo\n";

Will print "funk". However, if we change the context of the function call...

- lc_and_chop $foo; + my $bar = lc_and_chop $foo;

We get "fUnK" printed out. $bar, incidentally, now contains 'funk'. So, you are correct, AFAICT. The solution would be to either switch the calls to lc and chop, or add an extra line returning what we want at the end. I was always in favor of explicit returns, anyway... (Of course, you could always not use my lc, but where would the fun be in that? ;-))

For those interested (and for those looking for holes in my reasoning), here's what the Void::lc looks like:

sub lc (\$) { my $ref = shift; my $val = CORE::lc $$ref; return $val if defined wantarray; $$ref = $val; }

Update: Minor tweak to lc code. (Should do straight copy and paste next time...)

His Royal Cheeziness


In reply to Re^2: DWIM Part Nineteen: Unary Operators in Void Context by CheeseLord
in thread DWIM Part Nineteen: Unary Operators in Void Context by CheeseLord

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.