$pgn->comments was Re: DCPM: perltutopen blues....

Matthew Browning mb at matthewb.org
Wed Jul 9 08:04:12 CDT 2003


-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

On Monday 07 July 2003 13:00, Simon Waters wrote:
> I think adding comments to an existing game is a reasonable use of
> the "module", although it might not fit the modules name, or quite
> what the authors intended.
>
> But as you say, it isn't clear why an empty hash isn't
> provided/working, even if I'm poking data straight into an objects
> "private" areas.


I had another look at this today because I needed a break.

The following example, adapted from `test_1_PGN.pl' ( included in 
examples/ ) illustrates how one might go about populating the comments 
hash as the process goes on.  It works against the example input 
`kk_2001.pgn' inasmuch as I am able arbitrarily to assign key-value 
pairs to the GameComments hash and then output them at the end but, as 
previously mentioned, this *is* breaking the interface to the module 
and constitutes flamebait in certain circles ;)

If I had loads of time I'd look at trying to do something with 
Parse::RecDescent here...

Apologies in advance:
#!/usr/bin/perl -w

use strict;
use Chess::PGN::Parse;

$\ = "\n";

my $key;

my $filename = shift || die "filename required";

my $pgn = Chess::PGN::Parse->new( $filename ) or die $!;

while( $pgn->read_game() ) {

  $pgn->{GameComments}->{++$key} = 'comment';

  my $comments = $pgn->comments;

  print $pgn->event, " / ",
        $pgn->white, " - ",
	$pgn->black, " : ",
	$pgn->result, "\n";

  if( $pgn->parse_game() ) {

    print join( ' ', @{ $pgn->moves } );
  }

  for( keys %$comments ) {

    print "$_:\t$comments->{$_}";
  }
  print ref $pgn->comments;
  print "-" x 60, "\n";
  print "\n";
}

exit;


Matthew Browning

- -- 
http://matthewb.org/public_key.txt

-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.2.1 (GNU/Linux)

iD8DBQE/DBLTy5o0lRFL2ooRApV9AJ9fjAdKxTKcJZvymUdI71Djlx/6SACfUCKd
pyKenQeAhBkI0dUxQMuJ1Wk=
=pmVm
-----END PGP SIGNATURE-----



More information about the Devoncornwall-pm mailing list