Beefy Boxes and Bandwidth Generously Provided by pair Networks
Think about Loose Coupling
 
PerlMonks  

Comment on

( #3333=superdoc: print w/ replies, xml ) Need Help??

As a significant example of why flags can be messy, check this particularly thorny source code.

I needed to use that module 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 parser using a rather different approach.

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.

## ## WARNING! broken code ahead! ## my $FLAG=1; my $GAME=0; ## ## ... snip ## sub ReadGame { my $self=shift; my $continue=1; $self->_init(); do { if ( $continue == 2 ) { $continue = 0 } if ( $LINE =~/\[(.*)\]/ ) { my $line=$1; my ($key,$val,$junk)=split(/"/,$line); $key=~s/ $//g, $self->{$key}=$val; $GAME=0; } elsif ( $LINE !~/^$/) { $self->{Game}.=$LINE; $GAME=1; } elsif ( $GAME == 1 ) { $FLAG=0; } $LINE=<FICPGN>; if ( eof(FICPGN) && $continue == 1 ) { $continue = 2 } $LINE=~s/\r//; } while ( $FLAG==1 ); return ( $continue ) ; }
This code uses not one but THREE flags, transforming a would-be OOP module into a peculiar example of spaghetti code.
_ _ _ _ (_|| | |(_|>< _|

In reply to Re: Flag variables by gmax
in thread Flag variables by oakbox

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!
  • 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, 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, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • Outside of code tags, you may need to use entities for some characters:
            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.
  • Log In?
    Username:
    Password:

    What's my password?
    Create A New User
    Chatterbox?
    and the web crawler heard nothing...

    How do I use this? | Other CB clients
    Other Users?
    Others browsing the Monastery: (4)
    As of 2014-09-21 13:27 GMT
    Sections?
    Information?
    Find Nodes?
    Leftovers?
      Voting Booth?

      How do you remember the number of days in each month?











      Results (171 votes), past polls