Yes, what's really going on here is that, while the computer treats
$a == $b as commutative, the human brain is linguistically wired, and tends to parse such constructs asymmetrically as a topic and a predicate. And as it happens, the topic likes to be first. For similar reasons most OO programmers will choose to write:
foo.equals(0)
rather than:
0.equals(foo)
Despite the fact that Ruby considers 0 to be a Real Object, I suspect that even Ruby programmers will prefer the first form to the second, because that's how our brains are wired.
The solution to all this in Perl 6 will be that assignment to an existing variable in boolean context will produce a warning under strictures, but assignment to a my declaration will not. The typical use for assignment in a boolean context is actually not the if statement but the while statement:
while $item = shift(@foo) {...}
and in most cases this is probably better written as:
while my $item = shift(@foo) {...}
or even better:
for @foo -> $item {...}
Likewise,
if $x = long_expression() {...}
can turn into
given long_expression -> $x {...}
I suppose we might also allow the gcc-like workaround of putting extra parens when you want to hide the assignment from the boolean context.
-
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.