This is more a joke than a real solution, still might be useful. This builds a Perl routine via eval and then calls it from the content of your own page. It works more or less like a JSP compiler, turning the page data into native source code, and then compiling it via eval. If you add a thin layer of error control, this might be a viable solution.

The routine it creates expects a hash containing true/false values (the same you will test in your page) and returns a string with the results.

my $code = ""; my %H; while (<DATA>) { chomp; if ( /<%\s*if (.+?)\s*%>/i ) { $code .= " if (\$H{$1}) { \n"; } elsif ( /<%\s*elsif (.+?)\s*%>/i ) { $code .= "} elsif (\$H{$1}) { \n"; } elsif ( /<%\s*else\s*%>/i ) { $code .= "} else { \n "; } elsif ( /<%\s*endif\s*%>/i ) { $code .= "};\n "; } else { $code .= " \$r .= \"$_\";\n"; } } $code = "sub page_expr\n {\n my (%H) = \@_; my \$r = '';\n $code\n; re +turn \$r }; "; eval $code; $H{boys} = 1; $H{nice_boys} = 1; print page_expr( %H ); __DATA__ <%IF boys %> &nbspYou can play football for the whole day. <%IF nice_boys %> &nbspYou don't need to get permission from your teacher. <%ELSIF agreesive_boys%> &nbspPlease respect others <%ELSE%> &nbspPlease talk to me first <%ENDIF%> <%ELSIF girls%> &nbspI suggest you to go to library <%ELSE%> &nbspStay here <%ENDIF%>
This creates dynamically this code (I reformatted it a bit just to make it more readable):
sub page_expr {
 my (%H) = @_; my $r = '';
  if ($H{boys}) {
    $r .= "   You can play football for the whole day.";
    if ($H{nice_boys}) {
      $r .= "     You don't need to get .";
    } elsif ($H{agreesive_boys}) {
      $r .= "     Please respect others";
    } else {
      $r .= "     Please talk to me first";
    };
  } elsif ($H{girls}) {
   $r .= "   I suggest you to go to library";
  } else {
   $r .= "   Stay here";
  };
  return $r;
};
When run, this program prints:
   You can play football for the whole day.    
   You don't need to get permission from your teacher.

Just my euro 0.02. Hope it helps.




 

In reply to Re: Need advice (module) to parse nested IF/ELSIF.. by l3nz
in thread Need advice (module) to parse nested IF/ELSIF.. by taizica

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.