<?xml version="1.0" encoding="windows-1252"?>
<node id="240003" title="Re: Flag variables" created="2003-03-03 07:20:32" updated="2004-08-22 10:20:54">
<type id="11">
note</type>
<author id="127116">
gmax</author>
<data>
<field name="doctext">
&lt;p&gt;As a significant example of why flags can be messy, check this particularly thorny 
&lt;a href="http://search.cpan.org/src/MAIRE/Chess-Pgn-0.03/Pgn.pm"&gt;source code&lt;/a&gt;.&lt;/p&gt;
&lt;p&gt;I needed to use that &lt;a href="http://search.cpan.org/perldoc?Chess::Pgn"&gt;module&lt;/a&gt; and I found out that it was faulty. I tried to fix it, but its logic eluded me. I contacted the author, who didn't do anything, and therefore I wrote my own [id://141468|parser] using a rather different approach.&lt;/p&gt;
&lt;p&gt;This module is supposed to parse chess games. The problem in this code is that it skips the last game in each file it parses. I was able to spot where the problem is. As for fixing it using the same labyrinth of flags system, I simply couldn't.&lt;/p&gt;
&lt;code&gt;
##
## WARNING! broken code ahead!
##

  my $FLAG=1;
  my $GAME=0;

##
##  ... snip
##

sub ReadGame
{
  my $self=shift;
  my $continue=1;
  $self-&gt;_init();
  do
   {
    if ( $continue == 2 ) { $continue = 0 } 
    if ( $LINE =~/\[(.*)\]/ ) 
     {
      my $line=$1;
      my ($key,$val,$junk)=split(/"/,$line);
      $key=~s/ $//g,
      $self-&gt;{$key}=$val;
      $GAME=0;
     }
    elsif ( $LINE !~/^$/)
     {
        $self-&gt;{Game}.=$LINE; 
        $GAME=1;
     }
    elsif ( $GAME == 1 ) 
     {
        $FLAG=0;  
     }
    $LINE=&lt;FICPGN&gt;;
    if ( eof(FICPGN) &amp;&amp; $continue == 1  ) { $continue = 2  }
            $LINE=~s/\r//;
   }
  while ( $FLAG==1 );
  return ( $continue ) ;
 }
&lt;/code&gt;
This code uses not one but THREE flags, transforming a would-be OOP module into a peculiar example of spaghetti code.&lt;br&gt;


&lt;CODE&gt;
 _  _ _  _  
(_|| | |(_|&gt;&lt;
 _|   
&lt;/CODE&gt;</field>
<field name="root_node">
239992</field>
<field name="parent_node">
239992</field>
</data>
</node>
