Posted on 2006-11-11 12:15:51-08 by deg
Made some changes
Hi,

I have made some changes to my local copy of this module (see diff below).

These mainly consist of -

1. Report 'bad sgf' if there are duplicate property identifiers within a node. Previously all duplicates except the last one were silently erased.

2. Added AUTOLOAD functions to SGF::Node and SGF, so property values can be found using the sgf tag. eg. $sgf->RU, however this will break any modules that use this one, because i got rid of the defined methods ($sgf->rules for example.) I suppose these old ones could be left in for backwards compatibility, but they seem unnecessary.

How should i proceed?

dan

diff mysgfmod.pm /usr/lib/perl5/site_perl/5.8.7/Games/Go/SGF.pm -w

1c1 < package mysgfmod; --- > package Games::Go::SGF; 2a3 > use 5.006; 9c10,12 < our %EXPORT_TAGS = ( 'all' => [ qw() ] ); --- > our %EXPORT_TAGS = ( 'all' => [ qw( > > ) ] ); 11,13c14 < our @EXPORT = qw(); < our $VERSION = '0.02'; < our $AUTOLOAD; --- > our @EXPORT = qw( 15c16,17 < use Parse::RecDescent; --- > ); > our $VERSION = '0.01'; 17,24d18 < { my %seen; < sub isDuplicate { < my $ident = shift; < if (exists($seen{$ident})) {return 1} < else {$seen{ $ident } = 1; return 0} < } < sub clearHash { %seen = () } < } 25a20 > use Parse::RecDescent; 27,39c22,30 < GameTree : "(" Node(s) GameTree(s?) ")" { < $return = $item[2]; < if(@{$item[3]}) {push @$return, bless $item[3], "mysgfmod::Variation"} } < Node : ";" Property(s) { < $return = bless { map {@$_} @{$item[2]} }, "mysgfmod::Node"; < mysgfmod::clearHash() < } < Property : PropIdent PropValue(s) { < if (@{$item[2]} == 1) { $item[2]=$item[2][0] } < $return = [ $item[1], $item[2] ] < } < PropIdent : /[A-Z]+/ <reject: mysgfmod::isDuplicate( $item[1] )> { $return = $item[1] } < PropValue : "[" ValueType "]" --- > GameTree : "(" Sequence GameTree(s?) ")" > { $return = $item[2]; if(@{$item[3]}) {push @$return, bless $item[3], "Games::Go: +:SGF::Variation"} } > Sequence : Node(s) > Node : ";" Property(s) > { $return = bless { map {@$_} @{$item[2]} }, "Games::Go::SGF::Node"; } > Property : PropIdent PropValue(s) > { if (@{$item[2]} ==1 ) { $item[2]=$item[2][0] } $return = [$item[1], $item[2] ] +} > PropIdent : /[A-Z]+/ > PropValue : "[" CValueType "]" 41,42c32,35 < ValueType : SimpleText | SimpleText ":" SimpleText < SimpleText: Empty "[" SimpleText "]" SimpleText --- > CValueType: ValueType | Compose > ValueType : SimpleText > Compose : ValueType ":" ValueType > SimpleText: /[^\[\]]*/ "[" SimpleText "]" SimpleText 44,48c37 < | Empty < Empty : /[^\[\]]*/ < }; < < my $parser = new Parse::RecDescent $grammar or die "Bad grammar!\n"; --- > | /[^\[\]]*/ 49a39,40 > }; > my $parser = new Parse::RecDescent $grammar; 54,56c45,46 < my $a = $parser->GameTree(shift); < defined $a or die "Bad sgf!\n"; < bless $a, "mysgfmod"; --- > my $a = $parser->GameTree(shift) or croak "Couldn't parse SGF file\n"; > bless $a, "Games::Go::SGF"; 65c55 < if (ref $a->[$_] eq "mysgfmod::Variation") { --- > if (ref $a->[$_] eq "Games::Go::SGF::Variation") { 73a64,70 > # Game info methods > sub date { shift->[0]->{DT}; } > sub time { shift->[0]->{DT}; } > sub white { shift->[0]->{PW}; } > sub black { shift->[0]->{PB}; } > sub size { shift->[0]->{SZ}; } > sub komi { shift->[0]->{KM}; } 76,84c73 < sub AUTOLOAD { < my $self = shift; < my $type = ref($self) or croak $self.' is not an object'; < my $name = $AUTOLOAD; < $name =~ s/.*://; # strip fully-qualified portion < return $self->[0]->{$name}; < } < < package mysgfmod::Variation; --- > package Games::Go::SGF::Variation; 96,99d84 < sub DESTROY { } < < package mysgfmod::Node; < our $AUTOLOAD; 100a86 > package Games::Go::SGF::Node; 102,115d87 < sub colour { # because a pass can be written B[] < my $node = shift; < if (exists($node->{B})){ 'Black' } < else { if (exists($node->{W})){ 'White' } < else {'Neither'} < } < } < < sub AUTOLOAD { < my $node = shift; < my $name = $AUTOLOAD; < $name =~ s/.*://; # strip fully-qualified portion < return $node->{$name}; < }
Direct Responses: Write a response
Perl Weekly newsletter
A free weekly newsletter for people who are busy to read all the blogs. click here to check it out.