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

Hi Monks

I'm getting a warning (occuring frequently but not everytime)

Use of uninitialized value in numeric eq (==) at /home/jthomas/ActiveP +erl-5.10/site/lib/XML/Twig.pm line 6401 during global destruction.
and the code at 6401 is
if( $parent->{last_child} == $elt) { delete $parent->{empty}; $parent +->{last_child}=$elt->{prev_sibling}; if( $XML::Twig::weakrefs) { weak +en( $parent->{last_child});} ; }

I'm using OpenOffice::OODoc module to create some openoffice document, and getting this error. Would somebody show some light on what's happening here.

Some observations are it's happening only when PDF::API2 module is also being used. Am i missing some finish methods

Replies are listed 'Best First'.
Re: Warning from XML/Twig.pm : Use of uninitialized value in numeric eq (==)
by ikegami (Patriarch) on May 13, 2010 at 15:04 UTC

    The problem is nowhere near that line. Notice the "during global destruction". That means your data structure survived past the end of the program. At that point, Perl needs to use extraordinary measures to destroy the remaining variables, so variables aren't necessarily freed in the right order. That's what happened here, and that's why you're getting the warning.

    Your data structure was stored in a global variable or it was leaked. If it's the former, undefine the global variable before exiting. It's the latter, you'll have to hunt down the memory leak. I believe there are tools in the Devel:: namespace to help you find memory cycles and memory leaks.

      Hi all,

      Thanks for all the hints...It helped me a lot to solve the problem. Looks like it was indeed a problem with weakref and leaks.

      From XML::Twig man page

      dispose Useful only if you don't have Scalar::Util or WeakRef installed. Reclaims properly the memory used by an XML::Twig object. As the o +bject has circular references it never goes out of scope, so if you w +ant to parse lots of XML documents then the memory leak becomes a pro +blem. Use $twig->dispose to clear this problem.

      Same dispose method was available with OpenOffice::OOdoc module and problem got solved when started using that.

      Thanks a lot monks

Re: Warning from XML/Twig.pm : Use of uninitialized value in numeric eq (==)
by Corion (Patriarch) on May 13, 2010 at 09:48 UTC

    How many occurrences of == do you find on that line? See perldiag on what that warning means, then look at the values that get compared using ==. At least one of them is undefined.

      Hi Corion,

      I understand that. But the problem is it's in Twig.pm which i don't have control. Also i'm not defining/undefining any of those variables. It happens internally while calling Openoffice:OODoc. That's why i'm getting confused.

Re: Warning from XML/Twig.pm : Use of uninitialized value in numeric eq (==)
by Khen1950fx (Canon) on May 13, 2010 at 10:30 UTC
    I think that the problem is with weakrefs. Your system may not be setup to use weaken properly. I put this script together awhile back to make sure that I could handle weaken. Run this, and see if the problem goes away.
    #!/usr/local/bin/perl use strict; use warnings; use CPAN; CPAN::Shell->install( "Task::Weaken", "Cache::Weak", "Class::WeakSingleton", "Convert::Scalar", "Data::Structure::Util", "Devel::Cycle", "Devel::Monitor", "Devel::Peek", "EO::WeakArray", "Hash::NoRef", "IO::Plumbing", "Scalar::Util", "Set::Object", "Test::Memory::Cycle", "Test::NoXS", "Test::Weaken", "Tie::RefHash", "Tie::Util", "WeakRef", "WeakRef::Auto", "XML::Parser");

      Your system may not be setup to use weaken properly.

      The ability to weaken references is built into Perl itself, and none of those modules would affect that.

      I believe you are referring to the distro that shipped with a broken Scalar::Util package. It didn't cause weaken to work improperly, it caused the function weaken to throw an exception when used. This is clearly not the OP's problem. (That problem was fixed by simply reinstalling Scalar::Util from source.)

      weaken is normally found in Scalar::Util, so that's the only one that needs to be installed. A long time ago, at least on one version of RedHat, the system Scalar::Util package did not include weaken (the XS part of Scalar::Util, which provides weaken had been ommitted). I don't suppose that is the case here,

      You can check by running this (replace ' by " on windows) :

      perl -e'use Scalar::Util qw/weaken isweak/; use Test::More tests => 2; use strict; use warnings; my $foo; my $ref= \$foo; ok( ! isweak($ref), q{not weak}) ; weaken( $ref); ok( isweak($ref), q{weakened});'