Beefy Boxes and Bandwidth Generously Provided by pair Networks
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:

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":



  • Are you posting in the right place? Check out Where do I post X? to know for sure.
  • Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
    <code> <a> <b> <big> <blockquote> <br /> <dd> <dl> <dt> <em> <font> <h1> <h2> <h3> <h4> <h5> <h6> <hr /> <i> <li> <nbsp> <ol> <p> <small> <strike> <strong> <sub> <sup> <table> <td> <th> <tr> <tt> <u> <ul>
  • Snippets of code should be wrapped in <code> tags not <pre> tags. In fact, <pre> tags should generally be avoided. If they must be used, extreme care should be taken to ensure that their contents do not have long lines (<70 chars), in order to prevent horizontal scrolling (and possible janitor intervention).
  • Want more info? How to link or How to display code and escape characters are good places to start.
Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others wandering the Monastery: (3)
As of 2024-03-29 02:28 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found