Re: What would you like to see in XML::LibXML ?
Shlomi Fish <[email protected]> Sun, 29 Jan 2012 22:11:44 +0200
| Newsgroups | gmane.comp.lang.perl.xml |
|---|---|
| Message-ID | <[email protected]> |
Hi Aaron, On Sun, 29 Jan 2012 14:49:36 +0000 Aaron Crane <[email protected]> wrote: > Shlomi Fish <[email protected]> wrote: > > 2. Are there features in the more Perly parts that you wish to see implemented? > > Sorry for the delay in responding. > No problem. > I'd particularly value the ability to annotate a node with a Perl > scalar, and retrieve that scalar later — much like the per-node > `_private` data in libxml2, but accessible to Perl code. Straw-man > API: > > for my $node ($doc->findnodes($xpath1)) { > $node->setAnnotation(some_slow_computation_on($node)); > } > > # later... > for my $node ($doc->findnodes($xpath2)) { > my $annotation = $node->annotation or next; > say $annotation->some_method; > } > > # once we're done with the annotations... > $_->removeAnnotation for $doc->findnodes('//node()'); > > Alternatively, it might perhaps be better to always use a hash for the > annotations, and require a hash key to be passed to the relevant > methods: > > for my $node ($doc->findnodes($xpath1)) { > $node->setAnnotation(cached => some_slow_computation_on($node)); > } > > # later... > for my $node ($doc->findnodes($xpath2)) { > my $cached = $node->annotation('cached') or next; > say $cached->some_method; > } > > # annotation names for a particular node: > my @names = $node->annotationNames; > > # once we're done with the annotations... > for my $node ($doc->findnodes('//node()')) { > $node->removeAnnotation($_) for $node->annotationNames; > # Maybe there should also be $node->removeAllAnnotations() > } > > I believe the tricky bit is ensuring that the annotations always have > their reference counts updated at precisely the right time. > Yes, it would be very tricky. One option to overcome this may be to make these annotations be “tokens” which are arbitrary perl strings or numbers, but not arbitrary perl scalars. The XML-LibXML programmer will be able to map or unpack these tokens into something more complex, but it would be their responsibility to allocate unique tokens and to free their resources later. We can potentially make these annotations be dictionaries mapping «strings => strings» (using perl's built-in hashes, or balanced binary trees or whatever), but I guess we can stop there. I guess that would be the case of a leaky abstraction (see http://en.wikipedia.org/wiki/Leaky_abstraction , http://www.joelonsoftware.com/articles/LeakyAbstractions.html ) or a quirky abstraction (see http://shlomif-tech.livejournal.com/49510.html ), but if it makes our life easier…. > As far as I can tell, there are deficiencies in all the alternative > approaches that don't require additional support from XML::LibXML. > The refaddr of a Node instance isn't stable during the lifetime of a > document, so it can't be used as a key in an external data structure. > The same also appears to be true of the value obtained by > dereferencing a node instance, `$$node`. Currently, I do something > similar using attributes, but this has several deficiencies: > > - Only element nodes can be annotated > - You have to select attribute names so as not to conflict with > attributes used in the document > - Storing any annotation data other than a string requires an > additional external data structure > - If the annotated document must be passed to other code (either > in-memory, or by serializing), and that code would be confused by > additional attributes, the pseudo-annotation attributes must be > carefully removed first > Regards, Shlomi Fish -- ----------------------------------------------------------------- Shlomi Fish http://www.shlomifish.org/ What Makes Software Apps High Quality - http://shlom.in/sw-quality Q: I’ll be about 6.5 milliard Terran years next September. — Star Trek, “We, the Living Dead” by Shlomi Fish Please reply to list if it's a mailing list post - http://shlom.in/reply . _______________________________________________ Perl-XML mailing list [email protected] To unsubscribe: http://listserv.ActiveState.com/mailman/mysubs