#! perl -w $rc=is_well_formed (" self Don't forget to mow the car and wash the lawn. "); print $rc ? 'success' : 'failure'; sub is_well_formed { local $\ = "\n"; my $text = shift; # XML text to check # match patterns my $ident = qr/[:_A-Za-z][:A-Za-z0-9\-\._]*/; # identifier my $optsp = qr/\s*/; # optional whitespace my $att1 = qr/$ident$optsp=$optsp"[^"]*"/; # attribute my $att2 = qr/$ident$optsp=$optsp'[^']*'/; # attr. variant my $att = qr/$att1|$att2/; # any attribute my @elements = ( ); # stack of open elems print "Identifier $ident"; print "optsp $optsp"; print "att $att"; # loop through the string to pull out XML markup objects while( length($text) ) { print "Inside Loop"; # match an empty element if( $text =~ s/^<($ident)(\s+$att)*\s*\/>//o ) { # match an element start tag } elsif( $text =~ s/^<($ident)(\s+$att)*\s*>//o ) { push( @elements, $1 ); # match an element end tag } elsif( $text =~ s/^<\/($ident)\s*>//o ) { return unless( $1 eq pop( @elements )); # match a comment } elsif( $text =~ s/^