I'm writing a package in OO style which use XML::Parser and its callbacks. I put the callbacks in the 'new' subroutine.
Package Report; sub new() { my $self = {}; bless($self); [...] $self->{tree} = (); my $p = new XML::Parser( Handlers => { Start => \&handle_start, Default => \&handle_char, } ); $p->parsefile($my_file); sub handle_char() { # ... update $self->{tree} } sub handle_start() { # ... update $self->{tree} print Dumper \$self->{tree}; #dumper 1 } print Dumper \$self->{tree}; #dumper 2 return $self; }
When I want to use it, it only works for the first :
my $r = new Report('file1'); #all is good $r = new Report('file2'); # Dumper1 shows the first tree updated with the data of the second # and dumper2 shows nothing (undef)
I see that the second object uses the same tree of the first object.
How can I do that code work? Or is there a better design?

Thanks for your wisdom.


In reply to Objects, callbacks and XML::Parser by ixl

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.