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

Matthew Browning mb at matthewb.org
Mon Jul 7 04:32:55 CDT 2003


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

On Monday 07 July 2003 09:47, Simon Waters wrote:
> Matthew Browning wrote:
> > Perl filehandles look pretty much the same as in C.  The ones you
> > are dealing with are hidden behind two layers of obscurity.
>
> Keep telling me that it isn't just me being thick ;-)
>
> > Use cperl-mode, not the default perl-mode with GNU Emacs.
>
> Hmm okay when I figure out how to change mode.... 

M-x cperl-mode

then

M-x cperl <tab> <tab>

...to see a bunch of functions.

[...]

> One gotcha whose syntax has escaped me (or perhaps I need another new
> Parse.pm?), is the case where Chess::PGN::Parse doesn't find any
> comments in the original game. Then the hash $pgn->comments is
> undefined, and attempts to append new comments fail (can't put hash
> pairs into a non-existent hash, I don't think we can blame the
> language design for that one).
>

You are confusing accessing the values of a hash by reference with an 
object method.  Since they look pretty much the same and often do the 
same thing this is understandable.  Consider the following:

my $hash = { one => 1, two => 2, three => 3 };
print $hash->{two} # prints `2'
print $hash->two   # error - not an object.

my $object = Package->new( one => 1, two => 2, three => 3 );
print $object->{two} # *might* print `2' (if constructor args end up as 
part of the object) but breaks OO conventions
print $object->two # *might* print `2' (if this method is seen as a 
`get' to private data) - refer to package docs.

So: hash $pgn->comments is not undefined, but the method `comments' of 
this package is not returning anything here.  By default, this package 
initialises it as an anonymous hash.

Your choices are two.  You could hack the package to introduce a method 
called `set_comments' or somesuch, which may well not be a good idea, 
but would probably look something like this (don't quote me): 

sub set_comments {

  my $self = shift;
  $self->{GameComments} = {@_}; # replaces what was in there before.
}

...quite a lot of packages seem to use an AUTOLOAD function to catch 
all get/sets to accessible data and behave accordingly.  IIRC, this 
technique is documented in Tom Christiansens perltoot.

Alternatively, work out some way of tracking the comments in your own 
program.

> I assume I need to create an empty hash and assign it to the Parse
> object, before attempting to append comments (this I assume is
> quicker than checking if it is defined each time, but the "how" not
> the "when" is escaping me).
>
> I was trying something like "%{$pgn->comments} = [ my %empty ]", but
> no joy...

The package already assigns an empty hash for the comments; it just 
provides no public method to add to it.  In principal, there is nothing 
to stop you adding stuff to it but this is bad OO vibes:

$pgn->{GameComments}->{key} = value;

...this might be more an issue for the developer. I have read the 
module itself and found a couple of bits of it confusing.  This is not 
helped by the fact that I do not appreciate what PGN files are, but if 
there is a genuine reason for needing this method in the package then 
the author should know about it (I suppose).

Interesting stuff ;)

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

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

iD8DBQE/CT5Hy5o0lRFL2ooRArZBAKCBXjdRQGV4X01+XF/KheMZ2KsIGACeIUGU
O+/L4nq264bbjpbib3quEt8=
=kwr1
-----END PGP SIGNATURE-----



More information about the Devoncornwall-pm mailing list