The stupid question is the question not asked | |
PerlMonks |
comment on |
( [id://3333]=superdoc: print w/replies, xml ) | Need Help?? |
The ability to use the trinary operator as an Lvalue is documented in Programming Perl (the Camel book). "You can assign to the conditional operator if both the second and third arguments are legal lvalues (meaning that you can assign to them):" The book also adds the following caviet, "This is not necessarily guaranteed to contribute to the readability of the program. But it can be used to create some cool entries in an Obfuscated Perl contest."
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:
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 In reply to Re: Re: More Lvalue Subs - Double-Barreled Closures
by davido
|
|