mertserger has asked for the wisdom of the Perl Monks concerning the following question:

I have a problem with a piece of code using XML::Twig. As I don't seem to be able to access the XML::Twig site at the moment I thought I'd ask for some help from my fellow monks.

I maintain some perl code which uses XML::Twig to run checks on dictionary entries written in XML. One check raises a warning if an entry is labelled as "rare" but also contains more than three example quotations.

The rare label would usually look like this:
but more complex examples could be like:
<header>Now <la>rare</la></header>
or even:
<header>Now <la>hist.</la> and <la>rare</la></header>

For the purposes of the check, the first example is truly rare; the others could be allowed more than three quotes as they were once not-rare. The Perl subroutine which works out whether an entry is to be treated as rare for this check is:

sub is_la_rare { my $elt = shift; foreach my $label ($elt->children('la') ) { next unless $label->text eq "rare"; my $isNowRare = 0; if ( $label->prev_sibling && $label->prev_sibling->text =~ m/[ +nN]ow $/ ) { $isNowRare = 1; } if ( !$isNowRare ) { return 1; } } return 0; }

This works correctly for examples 1 (which it treats as "rare") and example 2 (which it treats as "not-rare"), but it treats example 3 as "rare", which is wrong.

I thought that the prev_sibling->text should pick up the bit of text that does match "Now" even though it is not the nearest prev_sibling to the "<la>rare</la>" but that does not seem to be happening. Have I misunderstood how prev_sibling works or is there some other error with this code?