Hello, fellow monks,

I've got a silly question regarding the use of short-circuit operators in assignments. I want to do something like this (in my code there's actually and instead of or – but I'd like to change it):

if (my $num = $recFono->field_as_text('102') or my $type = $recFono->f +ield_as_text('104')) { something(); }

The problem is of course that the second assignment isn't executed if the first one succeeds. I'd like both assignments to be executed and only then evaluated so the code in the conditional runs if either one or both of those variables are set. A non-short-circuiting or, so to speak.

I've been looking into using the comma operator for the assignments then re-evaluating the assigned variables with or – all in one long chain, but apparently those variables aren't in scope yet at that point.

Is what I'm doing here silly or am I overlooking a simple logical trick? Is it a bad idea to execute, assign and do logical evaluation in one go?

Thanks!


In reply to logical non-short-circuit operators by december

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.