in reply to Re: More Lvalue Subs - Double-Barreled Closures
in thread More Lvalue Subs - Double-Barreled Closures
I just wanted to take a second to illustrate in simpler terms (so that people like me can understand) how to use the trinary operator as an lvalue. Consider the following example:
my $waypoint = 'N43.4589 W112.9443'; my $latitude; my $longitude; foreach ( split / /, $waypoint ) { ( /^[NnSs]/ ) ? $latitude : $longitude = $_; }
What this accomplishes is letting the program's logic decide which variable to assign a value to. In the above example, $waypoint could contain latitude first, or longitude first, delimeted by a space. It doesn't matter which one comes first in the string, $latitude and $longitude get assigned the proper half of the value based on the evaluation of the regexp /^[nNsS]/ (which implies latitude, whereas [wWeE] would imply longitude, but we just made the assumption that if it's not latitude, it has to be longitude).
This is a simple example of using the trinary operator as an lvalue, but it opens the door for a lot of cool things. Zaxo reasoned his way into a pretty cool use. If you really want to create some unreadable code, try nesting trinaries both as lvalues and rvalues in the same expression. It gets ugly fast, and takes a pencil and paper to be able to figure out what is being assigned to, and what is being assigned to it.
Dave
"If I had my life to do over again, I'd be a plumber." -- Albert Einstein
|
|---|