OOOOOOhhhhh, this has pissed me off.

Look at this piece of code. it says that if the substitution succeeds, 
return a token. If it fails return undef and warn me.

  sub eatOptionalArgument {
    my $in = $_[0];

    if ($in =~ s/^\s*$optionalArgument//) {
      return new Token $1, $&;
    } else {
      warn "No optional argument found";
      return undef;
    }
  }

Great. What could be nicer? 

Hold on. Here's the RE in question. I direct your attention to the comment.

# matches either nothing OR an argument in brackets ($1 doesn't include [])
$optionalArgument = "(?:\\[([^]]*)\\])?"; # Contains one level of grouping

So. The bloody thing is always successful, whether a change took place or 
not. It is impossible for that warning to be triggered. It exists simply to 
sucker a mug like me who has just spent far too long scratching his head 
over it.

Functionally identical, less offensive:
sub eatOptionalArgument {
        my $in = $_[0];
        my $token=undef;

        # Note that this particular RE always succeeds in matching
        # $in, so $token is always initialised. 
        if ($in =~ s/^\s*$optionalArgument//) {
                $token = new Token $1, $&;
        }

        return $token;
}

Fade out stage left with howls of rage and gnashing of teeth.

-- 
Angus

Reply via email to