I've got the basic tokens/block thing going in an exploration script. One thing I'm noticing is that find() flattens out your parse-tree. Having the ability to access the parent-node may end up being nice, but I'm still feeling my way through the problem-space, so don't code anything based on what I say just yet.
It doesn't flatten it out as such, it just returned a list of pointers to the various elements you asked for in the tree.
http://search.cpan.org/~adamk/PPI-0.831/lib/PPI/Element.pm
The tree itself still exists and is not modified, and you can still call any of the "going up the tree" methods.
$Element->parent # Immediate parent $Element->top # Top of the PDOM tree (should be a ::Document) $Element->statement # Parent statement
Some documentation on what "significant" means would be nice. Also, if significant() and prune() could play together, that would be cool. I'm finding I like to do something like:
my $doc = PPI::Document->read( $filename ); $doc->prune( "PPI::Token::$_" ) for qw( Comment Whitespace );
See the find documentation at
http://search.cpan.org/~adamk/PPI-0.831/lib/PPI/Node.pm#find_$class_|_\&condition
The entire set of search methods (find, find_any and prune) accept anonymous sub references as well, the class name thing is just a shortcut.
The sub is passed the top element of the search, and the "current" element, so you can do something like the following, which will strip out everything but the significant elements.
my $Doc = PPI::Document->read( $filename );
$Doc->prune( sub { ! $_[1]->significant } );The "significant" property is defined at its method
http://search.cpan.org/~adamk/PPI-0.831/lib/PPI/Element.pm#significant
In short, perl contains a whole bunch of things you normally don't want to care about. If I want to find the "previous" or "next" element, I _probably_ don't care what whitespace or comments or pod is in the way.
And so the "s" serious of methods (snext_sibling, sprevious_sibling, schildren etc) will do what you mean, ignoring any miscellaneous things that don't matter that happen to be in the way.
This is important because when writing the perl document back to a file, it is VERY important that the document ends up exactly as it was when we read it in, in the same way something like Dreamweaver will read and write a HTML file while leaving it exactly as is (layout, broken code and all).
And whitespace does actually matter in some instances during the parsing. Yes, perl is REALLY crufty :)
It isn't line-based, but it is line-based for some things (Heredocs). Whitespace isn't important, but it does need to be there or things break.
I suppose I could be a good opensource user and provide a patch+tests, huh... :-)
I'm happy to receive patches but no guarantees that it will be merged. I'm still shuffling things around to make sure I'm 100% comfortable with the API myself.
Adam
_______________________________________________ sw-design mailing list [EMAIL PROTECTED] http://metaperl.com/cgi-bin/mailman/listinfo/sw-design
