On Sat, 6 Dec 2003, Kurt Starsinic wrote:

>
>     Did you try using delegation instead of subclassing?  See, for
> example, Class::Delegate on CPAN.

I tried to figure out how to use Class::Delegate in this case (or in any
case for what matters ;--( and utterly failed to understand it.

This little piece of code shows what Class::SubclassDeep (definitely a bad
name!) does, is there any chance you could show me how Class::Delegate
would help here? (you need version 0.02 of the module to run it, at
http://xmltwig.com/class-subclassdeep/ ):

#!/usr/bin/perl -w
use strict;

use Class::SubclassDeep;
use XML::DOM;

my $doc=q{<doc><elt>element</elt><no_id>no id</no_id></doc>};

{ # with regular nodes
  my $parser = new XML::DOM::Parser;
  my $dom = $parser->parse ( $doc);
  my $i;
  foreach my $elt ($dom->getElementsByTagName( 'elt'))
    { $elt->setAttribute( id => "id" . ++$i); }
  print "regular DOM:                          ", $dom->toString;
}

{ # with subclasses
  my $parser = new XML::DOM::Parser;
  subclass( 'XML::DOM::Element' => "MyElement");
  my $dom = $parser->parse ( $doc);
  print "DOM modified by Class::SubclassDeep:  ", $dom->toString;
}

package MyElement;

use base 'XML::DOM::Element';
my $i;

sub new
  { shift @_;
    my $elt= MyElement->SUPER::new( @_);
    if( $elt->getTagName eq 'elt')
      { $elt->setAttribute( id => "id" . ++$i); }
    return $elt;
  }

--
Michel Rodriguez
Perl &amp; XML
http://www.xmltwig.com




Reply via email to