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

I'm trying to recover the XML filename from its propagated XML::Simple object reference.

Here's a prototype script of the issue.

------------------------------------------ #!/usr/bin/perl use strict; use warnings; use XML::Simple; &main(@ARGV); exit; sub main { my $xml = XMLin(shift); &printName($xml); } sub printName { my $xml = shift; print $xml->{name}; # This is the line that needs help } ------------------------------------------

Looking at the implementation of the class, I know the filename is stored within the object. (I know, it's wrong to access private elements.)

I need to access the name in order to reduce the number of parameters I'm using between the program's routines.

Thanks,
Solostian

Replies are listed 'Best First'.
Re: XML::Simple private members
by pg (Canon) on Sep 14, 2005 at 15:08 UTC
    "I need to access the name in order to reduce the number of parameters I'm using between the program's routines."

    Just because of this? even you make the code work today, I believe it will stop working not very long after, once Perl's OO has more control over private variables, or the author of the module changes his design.

    If the author changes the name of a public method, you can blame everything on him; but if he changes the identifier of a private variable, you have no reason to blame him at all.

      The actual program is much more complex than the prototype I've included here. The filename is required many sub calls away. I know about the risks of using private elements directly and am willing to run the risk at this point. The only other "clean" option left to me would be to make the filename global and I'd rather avoid that if possible.
        "The filename is required many sub calls away."

        If your own program is OO, and store filename as a property of your class, you can access it without pass it as a parameter, the real parameter you need to pass is the class (a blessed hash ref or whatever).

        Then you will not care the many calls in between any more.

Re: XML::Simple private members
by osunderdog (Deacon) on Sep 14, 2005 at 16:44 UTC

    This may be just as objectionable as accessing a private variable, but thought I would throw it out there.

    Add the filename as a private part of the XML:

    #!/usr/bin/perl use strict; use warnings; use XML::Simple; &main(@ARGV); sub main { my $filename = shift; my $xml = XMLin($filename); $xml->{__SUPER_SECRET_FILENAME_PARAMETER__} = $filename; &printName($xml); } sub printName { my $xml = shift; print $xml->{__SUPER_SECRET_FILENAME_PARAMETER__}; }

    Also, I don't think that it is wise to use an exit at the end of a script. I believe that it is better to just return.

    UPDATE

    Further, the return of XMLin isn't a XML::Simple instance. It's just a hash. So if the item you're looking for isn't in the hash, then it isn't available. There isn't any private data.

    Hazah! I'm Employed!

      You are absolutely right, I had completely forgotten about that! Thanks for the tip, it will help me.

      I wish I could ++ more than once sometimes. Here is one of those: ++ for thinking outside the box! And ++ for giving applicable warnings for doing something weird ;-) (And maybe even ++ for your hash key name ;-})