in reply to Re: DWIM Part Nineteen: Unary Operators in Void Context
in thread DWIM Part Nineteen: Unary Operators in Void Context

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