Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl Monk, Perl Meditation
 
PerlMonks  

Re^2: DWIM Part Nineteen: Unary Operators in Void Context

by CheeseLord (Deacon)
on Aug 21, 2001 at 18:43 UTC ( [id://106585]=note: print w/replies, xml ) Need Help??


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

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://106585]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others imbibing at the Monastery: (8)
As of 2024-04-18 17:03 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found