Slash Boxes
NOTE: use Perl; is on undef hiatus. You can read content, but you can't post it. More info will be forthcoming forthcomingly.

All the Perl that's Practical to Extract and Report

use Perl Log In

Log In

[ Create a new account ]

Ovid (2709)

  (email not shown publicly)
AOL IM: ovidperl (Add Buddy, Send Message)

Stuff with the Perl Foundation. A couple of patches in the Perl core. A few CPAN modules. That about sums it up.

Journal of Ovid (2709)

Wednesday April 05, 2006
07:42 PM

True OO Joy (Model::World)

[ #29231 ]

I haven't touched Model::World in months. I got sidetracked, but this is one of many things I need to get off my plate (along with three CPAN updates today) so I returned to it with a sense of trepidation.

One of the first things I noticed is that I left a TODO file. I started off by reading that and many of my older posts about this module and decided to see how much work it would take to implement this:


After writing the tests, I wrote this method:

sub walk : Verb(qw/move go/) {
    my ( $self, $direction ) = @_;
    my $room = $self->location;
    if ( my $destination = $room->exit($direction) ) {
        return $self->location($destination);
    $self->_croak("You cannot go $direction");

It worked the first time. That's true OO joy. Decently designed classes, reasonable documentation and, of course, a full test suite have let me return to this with a minimum of pain.

The attribute, by the way, means that all of the following work the same way:


There are many other things I need to take care of, but I think the hard part will be the grammar. I need to have one built automatically so that the end user doesn't need to know about grammars unless necessary. Unfortunately, that makes for a serious problem that I don't know how to deal with just yet:

"Does a bear write neocon propoganda in the woods?"
"Can Tom Delay bear the weight of accusations against him?"

The word "bear" is used as both an noun and a verb. The simplest solution is to write a tokenizer which forbids words with more than one meaning. That makes the parser relatively straight forward for a first pass:

concatenate( $subject, $verb, optional( $object ) );

Later I can expand that, but for now, it's simple.

I think, reluctantly, that I will go this route. The alternative is to simple lex every sentence into a list of WORD tokens and let the grammar figure out the parts of speech based on context. This should not be too hard, but it does complicate things and will introduce bugs. It's my understanding that this is also the reason why people claim that only perl can parse Perl: the meanings of many things are tremendously overloaded and a straight-forward lexing of programs is not possible, so much work is pushed into the parser. Pop quiz: how many different uses for curly braces are there in Perl?

Of course, even the simple version is easier said than done.

The Fine Print: The following comments are owned by whoever posted them. We are not responsible for them in any way.
More | Login | Reply
Loading... please wait.
  • My Perl is getting rustier by the day, but are 'move' and 'go' just inline aliases? Or does that bit of syntax do more than that?
    • I wrote an attribute handler which would take the attributes and create aliases with them. However, it also registers them as parts of speech in the world vocabulary so that later I can build a grammar with it.