ifthenelse: if elsif(s?) else(?) endif { ... }

Because of the (s), elsif(s?) returns a reference to an array of "stuff that elsif matches". The array may contain 0 or more elements. That means the line identified below will always return true for $item[2]

ifthenelse: if elsif(s?) else(?) endif { $return = 'NIL'; for (my $i = 1; $i < @item; $i++) { print " iftest: $i '" . $item[$i] . "'\n"; if ($item[$i] ne 'NIL') { # Always true for $item[2]. $return = $item[$i]; # Always an array ref for $item[2]. last; # Always gets here for $item[2]. } } ... }

Comments:

By the way, why are you using 'NIL'? It would be more convenient to use a false value. Only undef will fail the production, so you can use 0 and '' safely.

Also, do you realize the above allows for if if var then var endif then var endif? if is rarely considered an expression. The ternary conditional operator in Perl (... ? ... : ...) and in Excel (@IF(..., ..., ...)) are examples of where ifs are expressions.

Finally, I find it very odd that you evaluate at compile time. For example, in
if cond then action1 else action2 endif
you unconditionally evaluate action1 first, then you unconditionally evaluate action2, then you decide which value to return based on cond. That is dangerous.

The following returns a nice tree for expr to evaluate:

expr : if { # I doubt this should be in the grammar. $return = evaluate($main::c_v, $item[1]); } if : /if/i expr /then/i expr if_(?) { [ if => @item[2, 4, 5] ] } if_ : elsif { $item[1] } | else { $item[1] } elsif : /elsif/i expr /then/i expr if_ { [ if => @item[2, 4, 5] ] } else : /else/i expr { $item[2] }

In reply to Re: To make the Model WORK by ikegami
in thread To make the Model WORK by samizdat

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.