From Simon at wretched.demon.co.uk Wed Jul 2 08:41:22 2003 From: Simon at wretched.demon.co.uk (Simon Waters) Date: Thu Aug 5 00:28:48 2004 Subject: DCPM: Newbie: Co-process pattern - examples of. Message-ID: <3F02E102.7090807@wretched.demon.co.uk> -----BEGIN PGP SIGNED MESSAGE----- Hash: SHA1 Hi, I want to write a Perl script that runs another program in parallel, sending commands to its stdin, and parsing its stdout. This is called a coprocess (at least in the Korn shell manual page). I was looking for perl examples and/or modules to help. The GnuPG module does this (if it can't use the gnupg shared memory coprocess model), but it looks; a) complicated (not least as it has two methods to communicate) b) distinctly like the kind of thing that might already have been packaged as a module. search.cpan.org only came up with the "filter" co-process, but I think(?) that is basically tacking a filter onto the end of your Perl output, which is not what I want to do, but maybe I misunderstood? My Perl skills are almost non-existent, so any examples I can find will help enormously. Also anyone done this under Windows? I'll be using an OS with it's own fork call, but portability would be nice to have. Simon -----BEGIN PGP SIGNATURE----- Comment: Using GnuPG with Mozilla - http://enigmail.mozdev.org iD8DBQE/AuEAGFXfHI9FVgYRAsmNAJ9NSt9Mn4xxZyuRBXH7BtpqDaatiQCfU2G+ RPd4xePYS2t9yWpd6xkulWA= =GnaQ -----END PGP SIGNATURE----- From steve at devon-it.co.uk Wed Jul 2 08:56:30 2003 From: steve at devon-it.co.uk (Steve Marvell) Date: Thu Aug 5 00:28:48 2004 Subject: DCPM: Newbie: Co-process pattern - examples of. In-Reply-To: <3F02E102.7090807@wretched.demon.co.uk> References: <3F02E102.7090807@wretched.demon.co.uk> Message-ID: <20030702135630.GA22663@flumpet.demon.co.uk> Simon Waters wrote: > I want to write a Perl script that runs another program in parallel, > sending commands to its stdin, and parsing its stdout. perldoc IPC::Open2 HTH Steve From Simon at wretched.demon.co.uk Wed Jul 2 11:48:20 2003 From: Simon at wretched.demon.co.uk (Simon Waters) Date: Thu Aug 5 00:28:48 2004 Subject: DCPM: Newbie: Co-process pattern - examples of. In-Reply-To: <20030702135630.GA22663@flumpet.demon.co.uk> References: <3F02E102.7090807@wretched.demon.co.uk> <20030702135630.GA22663@flumpet.demon.co.uk> Message-ID: <3F030CD4.8030201@wretched.demon.co.uk> -----BEGIN PGP SIGNED MESSAGE----- Hash: SHA1 Steve Marvell wrote: > Simon Waters wrote: > > >>I want to write a Perl script that runs another program in parallel, >>sending commands to its stdin, and parsing its stdout. > > > perldoc IPC::Open2 Thanks Steve, there are some interesting "you really don't want to use Open2" documents around, although I fit the minority when; a) I know output shouldn't be buffered or anything nasty. b) I have a trivial command to establish if the program is responsive (finished writing output). ... so I may "get away" with Open2. If I can master perl syntax I should have a working prototype by tomorrow.. Simon -----BEGIN PGP SIGNATURE----- Comment: Using GnuPG with Mozilla - http://enigmail.mozdev.org iD8DBQE/AwzSGFXfHI9FVgYRAoWAAKDIvP7OZG2aenZD3TwY/68lq5BzvQCeOxFq Fkjy/aPhSF/R6cLN3QVR5dg= =aKlo -----END PGP SIGNATURE----- From Simon at wretched.demon.co.uk Fri Jul 4 08:47:44 2003 From: Simon at wretched.demon.co.uk (Simon Waters) Date: Thu Aug 5 00:28:48 2004 Subject: DCPM: perltutopen blues.... In-Reply-To: <3F030CD4.8030201@wretched.demon.co.uk> References: <3F02E102.7090807@wretched.demon.co.uk> <20030702135630.GA22663@flumpet.demon.co.uk> <3F030CD4.8030201@wretched.demon.co.uk> Message-ID: <3F058580.7060508@wretched.demon.co.uk> -----BEGIN PGP SIGNED MESSAGE----- Hash: SHA1 Simon Waters wrote: > > If I can master perl syntax I should have a working prototype by > tomorrow.. Well it works after a fashion, I have a lot of rough edges to clean up. First is I would like to use; "script file1 file2 file3" or "script -" etc etc... My first attempts failed miserably.... The open is done by Chess::PGN::Parse, looks remarkably like that bit in the perltutopen, which starts muttering in old english (dweomer indeed) about how painful this is. Can someone explain what to do with "$_" in "while(<>){}" to feed it to "new Chess::PGN::Parse", previous I had "new Chess::PGN::Parse $pgn" where '$pgn = "1.pgn"' (1.pgn being a file name), and no "while" loop. -----BEGIN PGP SIGNATURE----- Comment: Using GnuPG with Mozilla - http://enigmail.mozdev.org iD8DBQE/BYV+GFXfHI9FVgYRAkmSAKCt6Fir9Ke35XbGcJjrbm/qgNnlWQCffLXt VVYO1jZw7PCZWz3VCaHdnjs= =lI7u -----END PGP SIGNATURE----- From mb at matthewb.org Fri Jul 4 09:05:50 2003 From: mb at matthewb.org (Matthew Browning) Date: Thu Aug 5 00:28:48 2004 Subject: DCPM: perltutopen blues.... In-Reply-To: <3F058580.7060508@wretched.demon.co.uk> References: <3F02E102.7090807@wretched.demon.co.uk> <3F030CD4.8030201@wretched.demon.co.uk> <3F058580.7060508@wretched.demon.co.uk> Message-ID: <200307041407.h64E78t24793@mail.pm.org> -----BEGIN PGP SIGNED MESSAGE----- Hash: SHA1 On Friday 04 July 2003 14:47, Simon Waters wrote: > Simon Waters wrote: > > If I can master perl syntax I should have a working prototype > > by tomorrow.. > > Well it works after a fashion, I have a lot of rough edges to clean > up. > > First is I would like to use; "script file1 file2 file3" or "script > -" etc etc... > Have a look at one of the GetOpt modules. GetOpt::Simple? > My first attempts failed miserably.... The open is done by > Chess::PGN::Parse, looks remarkably like that bit in the perltutopen, > which starts muttering in old english (dweomer indeed) about how > painful this is. > > Can someone explain what to do with "$_" in "while(<>){}" to feed it > to "new Chess::PGN::Parse", previous I had "new Chess::PGN::Parse > $pgn" where '$pgn = "1.pgn"' (1.pgn being a file name), and no > "while" loop. I had a quick look at the Chess::PGN::Parse module. It seems to take a filename as its contructor argument. You have a bunch of filenames that you've got from the command line somehow. I would address this by putting them in an array, probably, then iterating over it like: for( @filesFromCommandLine ) { my $object = Chess::PGN::Parse->new( $_ ); $object->interesting_chess_related_method(); push @array_of_objects, $object; } Let us know if I have misinterpreted the question. Matthew Browning - -- http://matthewb.org/public_key.txt -----BEGIN PGP SIGNATURE----- Version: GnuPG v1.2.1 (GNU/Linux) iD8DBQE/BYm+y5o0lRFL2ooRAommAJ9ctnb3/NXzkmEhmeaCSeHWwGdEkQCfV+Dp 3uifwrvdBC7h2Jh6W9k/HeU= =zvrh -----END PGP SIGNATURE----- From Simon at wretched.demon.co.uk Fri Jul 4 17:37:56 2003 From: Simon at wretched.demon.co.uk (Simon Waters) Date: Thu Aug 5 00:28:48 2004 Subject: DCPM: perltutopen blues.... In-Reply-To: <200307041407.h64E78t24793@mail.pm.org> References: <3F02E102.7090807@wretched.demon.co.uk> <3F030CD4.8030201@wretched.demon.co.uk> <3F058580.7060508@wretched.demon.co.uk> <200307041407.h64E78t24793@mail.pm.org> Message-ID: <3F0601C4.7050003@wretched.demon.co.uk> -----BEGIN PGP SIGNED MESSAGE----- Hash: SHA1 Matthew Browning wrote: > > Have a look at one of the GetOpt modules. GetOpt::Simple? For better or worse, I went with Getopt::Long, on the basis that I hope it does the same as the similar named C routine. > for( @filesFromCommandLine ) { > > my $object = Chess::PGN::Parse->new( $_ ); > > $object->interesting_chess_related_method(); > > push @array_of_objects, $object; > } > > Let us know if I have misinterpreted the question. Thanks, I think I was just getting confused with the documentation. I'm still slightly vague on when perl is doing file handles, and when it is expecting file names... When they look vaguely like good old fashioned Bourne shell file handles all is clear. My script has gained a plethora of "options", including the ubiquitous "--debug" ;-) Maybe trying to learn emacs and perl at the same time was a bit ambitious, but it will be the best indented code I've written in ages. Next fun bit will be a signal handler, it all looks simple("use sigtrap" ?), but I never seen to find any example scripts - maybe I should squander another book token. Hmm 15GBP book tokens, 10GBP extra if WH Smiths... I'll go check the review section. Then ensuring it is self documenting.... then sorting the output routines, and I'll be ready to fix the special cases... Simon BTW: Why does "use warnings" dislike "@array[2]" and suggest "$array[2]", seems less clear to me, or am I missing something elementary? -----BEGIN PGP SIGNATURE----- Comment: Using GnuPG with Mozilla - http://enigmail.mozdev.org iD8DBQE/BgHAGFXfHI9FVgYRAuJsAKDDp8gke09wKifxE0p3g6mwIeVBhwCgvQ2o BC6P6zqyJQnntUVjIuqh9Q4= =C4C+ -----END PGP SIGNATURE----- From mb at matthewb.org Mon Jul 7 03:01:27 2003 From: mb at matthewb.org (Matthew Browning) Date: Thu Aug 5 00:28:48 2004 Subject: DCPM: perltutopen blues.... In-Reply-To: <3F0601C4.7050003@wretched.demon.co.uk> References: <3F02E102.7090807@wretched.demon.co.uk> <200307041407.h64E78t24793@mail.pm.org> <3F0601C4.7050003@wretched.demon.co.uk> Message-ID: <200307070802.h6782f716651@mail.pm.org> -----BEGIN PGP SIGNED MESSAGE----- Hash: SHA1 On Friday 04 July 2003 23:37, Simon Waters wrote: > Thanks, I think I was just getting confused with the documentation. > I'm still slightly vague on when perl is doing file handles, and when > it is expecting file names... When they look vaguely like good old > fashioned Bourne shell file handles all is clear. > Perl filehandles look pretty much the same as in C. The ones you are dealing with are hidden behind two layers of obscurity. First, your module takes a filename as an argument and manipulates the filhandle internally, second it does so by means of the IO::File (standard) module. FWIW, I typically use IO::File because it allows you to treat a filehandle as a scalar (by returning a reference to one) which is cool if you want a painless way to build an array of them or chuck them around various functions etc. > My script has gained a plethora of "options", including the > ubiquitous "--debug" ;-) > > Maybe trying to learn emacs and perl at the same time was a bit > ambitious, but it will be the best indented code I've written in > ages. > They make a good team: C-x h C-u M-| perl -pe 'EXPR' (Mark Jason Dominus) ...I have bound F12 to a Lisp function that runs the script I am working on, diplaying the output in a new buffer. That is quite handy. Also, check out M-x cperl-perldoc for context-specific help. Use cperl-mode, not the default perl-mode with GNU Emacs. > Next fun bit will be a signal handler, it all looks simple("use > sigtrap" ?), but I never seen to find any example scripts - maybe I > should squander another book token. Hmm 15GBP book tokens, 10GBP > extra if WH Smiths... I'll go check the review section. > I guess the Camel is your best bet. I have read pretty much all the books there are on Perl (seriously) and if there is one that really made me think *wow* it is `Object-Oriented Perl' by Damien Conway (Manning). Having said that, it does not address your problem. > Then ensuring it is self documenting.... $ perldoc perlpod > then sorting the output > routines, and I'll be ready to fix the special cases... > ... > Why does "use warnings" dislike "@array[2]" and suggest "$array[2]", > seems less clear to me, or am I missing something elementary? The first is an array slice, the second an array element. While contrary to the conventions we see in other languages, Perl wants you to refer to a scalar as a scalar, even if it is an element of an array. The first is a list, albeit with one element. Just keep telling yourself: `it all makes perfect sense'... Matthew Browning. - -- http://matthewb.org/public_key.txt -----BEGIN PGP SIGNATURE----- Version: GnuPG v1.2.1 (GNU/Linux) iD8DBQE/CSjdy5o0lRFL2ooRAhURAJ4tuncYsFxI18P40SFGYlsuBAyDqACfbydl 5D8PxpXeyInasNmZya9L4/Q= =u/Hv -----END PGP SIGNATURE----- From Simon at wretched.demon.co.uk Mon Jul 7 03:47:31 2003 From: Simon at wretched.demon.co.uk (Simon Waters) Date: Thu Aug 5 00:28:48 2004 Subject: $pgn->comments was Re: DCPM: perltutopen blues.... In-Reply-To: <200307070802.h6782f716651@mail.pm.org> References: <3F02E102.7090807@wretched.demon.co.uk> <200307041407.h64E78t24793@mail.pm.org> <3F0601C4.7050003@wretched.demon.co.uk> <200307070802.h6782f716651@mail.pm.org> Message-ID: <3F0933A3.5020407@wretched.demon.co.uk> -----BEGIN PGP SIGNED MESSAGE----- Hash: SHA1 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.... I'd already found stuff in cperl-mode that sounded good whilst searching the help, so it had made the "someting to look at later list" already. I have a zillion little emacs questions, but I think they are probably best explained at a LUG meet or similar. The script is basically working as expected now, oh and Giuseppe has updated Chess::PGN::Parse a couple of times to suit my needs (how is that for free support!). 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). 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... -----BEGIN PGP SIGNATURE----- Comment: Using GnuPG with Mozilla - http://enigmail.mozdev.org iD8DBQE/CTOgGFXfHI9FVgYRAiA6AKCJM1l0eCQ3YETfHhmBC+oXriz4SwCgu94y VBpZBbQuZBU45BIXvhnz36g= =rK3i -----END PGP SIGNATURE----- From mb at matthewb.org Mon Jul 7 04:32:55 2003 From: mb at matthewb.org (Matthew Browning) Date: Thu Aug 5 00:28:48 2004 Subject: $pgn->comments was Re: DCPM: perltutopen blues.... In-Reply-To: <3F0933A3.5020407@wretched.demon.co.uk> References: <3F02E102.7090807@wretched.demon.co.uk> <200307070802.h6782f716651@mail.pm.org> <3F0933A3.5020407@wretched.demon.co.uk> Message-ID: <200307070934.h679Y0717415@mail.pm.org> -----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 ...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----- From steve at devon-it.co.uk Mon Jul 7 04:46:20 2003 From: steve at devon-it.co.uk (Steve Marvell) Date: Thu Aug 5 00:28:48 2004 Subject: $pgn->comments was Re: DCPM: perltutopen blues.... In-Reply-To: <3F0933A3.5020407@wretched.demon.co.uk> References: <3F02E102.7090807@wretched.demon.co.uk> <200307041407.h64E78t24793@mail.pm.org> <3F0601C4.7050003@wretched.demon.co.uk> <200307070802.h6782f716651@mail.pm.org> <3F0933A3.5020407@wretched.demon.co.uk> Message-ID: <20030707094620.GB4762@flumpet.demon.co.uk> Simon Waters wrote: > > Use cperl-mode, not the default perl-mode with GNU Emacs. > > Hmm okay when I figure out how to change mode.... In your ~/.emacs file: (defun modify-alist (alist-symbol key value &optional search-cdr) (let ((alist (symbol-value alist-symbol))) (while alist (if (eq (if search-cdr (cdr (car alist)) (car (car alist))) key) (setcdr (car alist) value) (setq alist (cdr alist)))))) (modify-alist 'interpreter-mode-alist 'perl-mode 'cperl-mode t) (modify-alist 'auto-mode-alist 'perl-mode 'cperl-mode t) Now you don't have to change mode. Steve From steve at devon-it.co.uk Mon Jul 7 04:43:28 2003 From: steve at devon-it.co.uk (Steve Marvell) Date: Thu Aug 5 00:28:48 2004 Subject: DCPM: perltutopen blues.... In-Reply-To: <200307070802.h6782f716651@mail.pm.org> References: <3F02E102.7090807@wretched.demon.co.uk> <200307041407.h64E78t24793@mail.pm.org> <3F0601C4.7050003@wretched.demon.co.uk> <200307070802.h6782f716651@mail.pm.org> Message-ID: <20030707094328.GA4762@flumpet.demon.co.uk> Matthew Browning wrote: > FWIW, I typically use IO::File because it allows you to treat a > filehandle as a scalar (by returning a reference to one) which is cool > if you want a painless way to build an array of them or chuck them > around various functions etc. I've never had a problem building an array of file handles. > > My script has gained a plethora of "options", including the > > ubiquitous "--debug" ;-) > > > > Maybe trying to learn emacs and perl at the same time was a bit > > ambitious, but it will be the best indented code I've written in > > ages. > > They make a good team: > > C-x h C-u M-| perl -pe 'EXPR' (Mark Jason Dominus) What does that do for you? > That is quite handy. Also, check out M-x cperl-perldoc for > context-specific help. Use cperl-mode, not the default perl-mode > with GNU Emacs. I still hve problems with even cperl-mode and certain regex, quoting or here doc indentation. > I guess the Camel is your best bet. I have read pretty much all the > books there are on Perl (seriously) and if there is one that really > made me think *wow* it is `Object-Oriented Perl' by Damien Conway > (Manning). Having said that, it does not address your problem. I wowwed at Data Munging and Computer Science and Perl Programming. Steve From mb at matthewb.org Mon Jul 7 05:09:14 2003 From: mb at matthewb.org (Matthew Browning) Date: Thu Aug 5 00:28:48 2004 Subject: DCPM: perltutopen blues.... In-Reply-To: <20030707094328.GA4762@flumpet.demon.co.uk> References: <3F02E102.7090807@wretched.demon.co.uk> <200307070802.h6782f716651@mail.pm.org> <20030707094328.GA4762@flumpet.demon.co.uk> Message-ID: <200307071010.h67AAI717619@mail.pm.org> -----BEGIN PGP SIGNED MESSAGE----- Hash: SHA1 On Monday 07 July 2003 10:43, Steve Marvell wrote: > Matthew Browning wrote: > > FWIW, I typically use IO::File because it allows you to treat a > > filehandle as a scalar (by returning a reference to one) which is > > cool if you want a painless way to build an array of them or chuck > > them around various functions etc. > > I've never had a problem building an array of file handles. > ...but this is *even* less a problem ;) Subjective comment, I suppose. Seems more natural to me. > > > My script has gained a plethora of "options", including the > > > ubiquitous "--debug" ;-) > > > > > > Maybe trying to learn emacs and perl at the same time was a bit > > > ambitious, but it will be the best indented code I've written in > > > ages. > > > > They make a good team: > > > > C-x h C-u M-| perl -pe 'EXPR' (Mark Jason Dominus) > > What does that do for you? > Inflict EXPR upon the whole buffer. > > That is quite handy. Also, check out M-x cperl-perldoc for > > context-specific help. Use cperl-mode, not the default perl-mode > > with GNU Emacs. > > I still hve problems with even cperl-mode and certain regex, quoting > or here doc indentation. > `Only perl can parse Perl'. To be fair, I think it is pretty good, but, yeah, I have had both of those problems. Also if I use square brackets in a qq thingy it sometimes b0rks. Then, if I press return a couple of times it is okay again. I think the benefits outweigh the minor problems. > > I guess the Camel is your best bet. I have read pretty much all > > the books there are on Perl (seriously) and if there is one that > > really made me think *wow* it is `Object-Oriented Perl' by Damien > > Conway (Manning). Having said that, it does not address your > > problem. > > I wowwed at Data Munging and Computer Science and Perl Programming. > I'm not being funny, truly, but I read `Data Munging with Perl' a couple of months ago expecting something really cool and was *very* disappointed - the first ~100 pages are basically a summary of some useful functions and then you get some stuff about some pretty well-known modules. I was quite interested in the stuff on Parse::RecDescent but Conway's own essay is more comprehensive. I always get in a bit of a pickle with binary data so I read Cross' chapter on it and what do you get? Pack and Unpack - yeah, I know about that. The whole book took me about three hours to read. If you like `Computer Science and Perl Programming', try `Mastering Algorithms in Perl' - Orwant et al, it gets pretty deep. I have `Games, Diversions and Perl Culture' here. Sample article: `Perl and Nuclear Weapons Don't Mix'...hilarious. Matthew Browning. - -- http://matthewb.org/public_key.txt -----BEGIN PGP SIGNATURE----- Version: GnuPG v1.2.1 (GNU/Linux) iD8DBQE/CUbKy5o0lRFL2ooRAkkKAJ42TiZqSdPHUhpNziKoORRMliMjZACgqlJy wEcuqLIlL7Ibh1Wg/OvSRIw= =74eU -----END PGP SIGNATURE----- From steve at devon-it.co.uk Mon Jul 7 05:21:06 2003 From: steve at devon-it.co.uk (Steve Marvell) Date: Thu Aug 5 00:28:48 2004 Subject: DCPM: perltutopen blues.... In-Reply-To: <200307071010.h67AAI717619@mail.pm.org> References: <3F02E102.7090807@wretched.demon.co.uk> <200307070802.h6782f716651@mail.pm.org> <20030707094328.GA4762@flumpet.demon.co.uk> <200307071010.h67AAI717619@mail.pm.org> Message-ID: <20030707102106.GA5005@flumpet.demon.co.uk> Matthew Browning wrote: > ...but this is *even* less a problem ;) > Subjective comment, I suppose. Seems more natural to me. I tend to avoid using object when native types work fine :) > > I still hve problems with even cperl-mode and certain regex, quoting > > or here doc indentation. [ ] > I think the benefits outweigh the minor problems. Oh yes. > I'm not being funny, truly, but I read `Data Munging with Perl' a > couple of months ago expecting something really cool and was *very* > disappointed - the first ~100 pages are basically a summary of some > useful functions Unlike OO programming where the first ~100 pages are a summary of some _essential_ functions :) > and then you get some stuff about some pretty well-known modules. I > was quite interested in the stuff on Parse::RecDescent but Conway's > own essay is more comprehensive. I always get in a bit of a pickle > with binary data so I read Cross' chapter on it and what do you get? > Pack and Unpack - yeah, I know about that. The whole book took me > about three hours to read. Taught me how to manage complex structured data well. I guess if you already knew that, you'd not have gained as much as I did. > I have `Games, Diversions and Perl Culture' here. Sample article: > `Perl and Nuclear Weapons Don't Mix'...hilarious. As soon as I finish my persent book review (Webmaster in a Nutshell latest ed), I'll be after one of those. Steve From Simon at wretched.demon.co.uk Mon Jul 7 07:00:55 2003 From: Simon at wretched.demon.co.uk (Simon Waters) Date: Thu Aug 5 00:28:48 2004 Subject: $pgn->comments was Re: DCPM: perltutopen blues.... In-Reply-To: <200307070934.h679Y0717415@mail.pm.org> References: <3F02E102.7090807@wretched.demon.co.uk> <200307070802.h6782f716651@mail.pm.org> <3F0933A3.5020407@wretched.demon.co.uk> <200307070934.h679Y0717415@mail.pm.org> Message-ID: <3F0960F7.3040809@wretched.demon.co.uk> -----BEGIN PGP SIGNED MESSAGE----- Hash: SHA1 Matthew Browning wrote: > >> 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; Yes, but it doesn't work if there are no comments in the original file! i.e. When the hash should be an empty hash. Since "comments" returns a reference to "GameComments2, I don't think I should be referring to GameComments (very bad vibes - depends on implementation detail). Reading the source I don't see how "comments" doesn't get initialised either, perhaps I should capture the error message for you guys ;-) Possible I've misunderstood something. > ...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). PGN is literally a simple text based notation for chess moves. The only complexity being you nearly always want to handle human typed PGN which never adheres to the standard ;-) I'm treating comments as strings (there is code in Parse.pm to handle other cases). RAV is merely comments within comments, which I'm ignoring, who cares how many brackets are inside a string ;-) Glad you find bits confusing. 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. -----BEGIN PGP SIGNATURE----- Comment: Using GnuPG with Mozilla - http://enigmail.mozdev.org iD8DBQE/CWDzGFXfHI9FVgYRAoIwAJ9xppG1fxfbKpaLoetmk+5rsNAXtgCcDeOg 6LzGH1Ld7TsAwK+r3xoCWX8= =MRFd -----END PGP SIGNATURE----- From mb at matthewb.org Wed Jul 9 08:04:12 2003 From: mb at matthewb.org (Matthew Browning) Date: Thu Aug 5 00:28:48 2004 Subject: $pgn->comments was Re: DCPM: perltutopen blues.... In-Reply-To: <3F0960F7.3040809@wretched.demon.co.uk> References: <3F02E102.7090807@wretched.demon.co.uk> <200307070934.h679Y0717415@mail.pm.org> <3F0960F7.3040809@wretched.demon.co.uk> Message-ID: <200307091305.h69D5bf06718@mail.pm.org> -----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----- From Simon at wretched.demon.co.uk Wed Jul 9 09:24:45 2003 From: Simon at wretched.demon.co.uk (Simon Waters) Date: Thu Aug 5 00:28:48 2004 Subject: $pgn->comments was Re: DCPM: perltutopen blues.... In-Reply-To: <200307091305.h69D5bf06718@mail.pm.org> References: <3F02E102.7090807@wretched.demon.co.uk> <200307070934.h679Y0717415@mail.pm.org> <3F0960F7.3040809@wretched.demon.co.uk> <200307091305.h69D5bf06718@mail.pm.org> Message-ID: <3F0C25AD.3030104@wretched.demon.co.uk> -----BEGIN PGP SIGNED MESSAGE----- Hash: SHA1 Matthew Browning wrote: > > $pgn->{GameComments}->{++$key} = 'comment'; Thanks Matthew, but as I said "I'm poking data straight into an objects private areas", as I'd already written; if (!(defined $pgn->comments)){ my %empty; $pgn->{GameComments}=\%empty; ... } Which I think was fine because a hash array happens to be the default type expected for the polymorphic chess game object, apart from breaking information hiding. I finally explained what I wanted to Giuseppe, and he added an "add_comment" method to Parse.pm version 16, so no need to break any OO rules just as soon as CPAN catches up. -----BEGIN PGP SIGNATURE----- Comment: Using GnuPG with Mozilla - http://enigmail.mozdev.org iD8DBQE/DCWrGFXfHI9FVgYRAokMAJsGUHX27QJP9P7C8621/HSfvS7KtwCghqW8 ZjzT4S3R+5oHYy9sREy3MZE= =itxi -----END PGP SIGNATURE-----