Your code is hitting the difference between && and and. The former is for logically anding two simple values together within a larger expression; the latter, spelled-out, low-precedence and is for anding together larger expressions, which is what you're doing here. You should be spelling out and in this case. Or, when in doubt, parenthesize.

It may be noted that in practice and is a much more common and useful operator than &&, though both have their uses.

They both short-circuit in the same way, and I suspect that both might also optimize the same way in a parenthesized case, though what gets optimized is really an internals issue that does not make a lot of difference at the language level. (In your code, if the 1 && were not optimized away, the result would still be the same and would still not be what you want. The optimization is just an implementation detail.)

There are languages that are largely immune to this sort of issue, due to not having complex precedence tables. Most lisp-based languages fall into this category: Common Lisp, Scheme, elisp, and so on. But to work with those languages, you really NEED an editor that does paren matching or automatic indentation -- preferably both.


;$;=sub{$/};@;=map{my($a,$b)=($_,$;);$;=sub{$a.$b->()}} split//,".rekcah lreP rehtona tsuJ";$\=$;[-1]->();print

In reply to Re: Short Circuit Operator and Hash Assignment by jonadab
in thread Short Circuit Operator and Hash Assignment by periapt

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.