Re: Perl-XML Digest, Vol 104, Issue 4

Kiem-Hieu Nguyen <[email protected]> Sat, 16 Mar 2013 17:42:57 +0100
Newsgroups gmane.comp.lang.perl.xml
Message-ID <[email protected]>
> Message: 1
> Date: Thu, 14 Mar 2013 21:55:19 +0100
> From: "Jenda Krynicky" <[email protected]>
> Subject: Re: [XML::Parser] Global variable doesn't change
> To: <[email protected]>
> Message-ID: <[email protected]>
> Content-Type: text/plain; charset=US-ASCII
>
> From:           	Kiem-Hieu Nguyen <[email protected]>
>> Hi,
>> I hope you could help me with this issue: I have a global variable,
>> says a hash. In a handler of the parser I make change to the 
>> variable
>> and its values changes indeed, says adding element to the hash. 
>> However,
>> after finishing the parsing, the hash returns to emty. What I want 
>> is
>> keeping the changes made during the parsing.
>> Thanks in advance! And it's great using XML::Parser!
>> Hieu.
>>
>> --
>> Kiem-Hieu Nguyen
>
> My crystal ball is malfunctioning, I can't help you without seeing
> the code.
>
> A few questions and wild guesses ...
> You are using "use strict", right?
> Are you sure you are using the global hash in the handlers? If you
> add something into the hash before the parsing, do you see it from
> within the handlers? Any "package" statements? Any "local"?
>

Hi,
Thank you. It was fixed now.
I just put the hash declaration before the new xml parser is created. 
Before I put it after. I don't know why it works like that.
Yes, I "use strict" and I can see it within the handlers, seeing the 
change.

use strict;
use IO::File;
use XML::Parser;

our %myhash = ();

my $parser = new XML::Parser
    ( Handlers =>
       {  # Creates our parser object
          Start   => \&hdl_start,
          End     => \&hdl_end,
          Char    => \&hdl_char
       }
    );

# before I put here and it didn't work
# our %myhash = ();

$parser->parse($fileStream);

# check the final hash
print scalar keys %myhash, "\n";

sub hdl_start() {
...
}

sub hdl_end() {
...
}

sub hdl_char() {
...
# update myhash here
$myhash{$somethihg} = $somevalue;
# and can see change
print scalar keys %myhash, "\n";
}
_______________________________________________
Perl-XML mailing list
[email protected]
To unsubscribe: http://listserv.ActiveState.com/mailman/mysubs