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:

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


In reply to Re: Re: More Lvalue Subs - Double-Barreled Closures by davido
in thread More Lvalue Subs - Double-Barreled Closures by Zaxo

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.