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

can anyone tell is it possible to Globalise the xml writer object. So that I can use that object to append the data? For example : I have a code creating a basic xml defined in one package.

$XmlWriterObject = new XML::Writer( DATA_MODE => 1, DATA_INDENT => 4, +OUTPUT => $Output ); $XmlWriterObject->xmlDecl("UTF-8","1"); $XmlWriterObject->startTag("Config");
Now I want to close the tag "Config" by using the $XmlWriterObject from someother package. Please clarify?

Replies are listed 'Best First'.
Re: Globalize the XML writer object
by norbert.csongradi (Beadle) on Sep 19, 2011 at 09:54 UTC
    Declare your object as a global variable (let's say your current package is Foo):
    package Foo; our $XmlWriterObject = new XML::Writer( DATA_MODE => 1, DATA_INDENT => + 4, OUTPUT => $Output );

    So, you can access it from another package (say Bar):

    package Bar; $Foo::XmlWriterObject->endTag("Config");

      Hi, I tried by declaring it as "Our". But the case is : Lets say :

      Package foo sub create xml{ our $XmlWriterObject = new XML::Writer( DATA_MODE => 1, DATA_INDENT => + 4, OUTPUT => $Output ); } package Bar; sub { $Foo::XmlWriterObject->endTag("Config"); }
      For this the xml file is created but no data has been written into it.

      Please clarify.

        priyaviswam:

        After I added use strict and use warnings and corrected the myriad errors, I was able to make it work just fine. I'd post the (trivial) code, but given that you didn't even bother to try to make a working example, despite being here since July, I'm not terribly motivated to cut & paste my working code snippet here.

        A couple hints:

        • Subroutines need names.
        • Case matters.
        • You actually need to *call* subroutines in order for them to do things for you.

        Please make your code at least *compile* first, and then I'll be happy to give you a few more pointers.

        ...roboticus

        When your only tool is a hammer, all problems look like your thumb.