Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl Monk, Perl Meditation
 
PerlMonks  

"PAR'ing" XML configs?

by guice (Scribe)
on Aug 08, 2005 at 15:24 UTC ( #481911=perlquestion: print w/ replies, xml ) Need Help??
guice has asked for the wisdom of the Perl Monks concerning the following question:

I'm going to be using PAR to help packages scripts into a single archive. The problem I'm facing:

I want to create a single script that is very self contained, but I want to use XML for the config file and I don't want to have to maintain two files on each remote system.

To you PAR users; Is there anyway to self contain the XML file within the PAR package and not have to worry about maintaining two spereate files on the remote systems? Meaning, I want to put the XML file within the PAR package and be able to use it.

-- philip
We put the 'K' in kwality!

Comment on "PAR'ing" XML configs?
Replies are listed 'Best First'.
Re: "PAR'ing" XML configs?
by rinceWind (Monsignor) on Aug 08, 2005 at 15:31 UTC

    You need to use the -a option to pp.

         -a, --addfile=FILE|DIR
             Add an extra file into the package.  If the file is a
             directory, recursively add all files inside that
             directory, with links turned into actual files.
    
             By default, files are placed under "/" inside the
             package with their original names.  You may override
             this by appending the target filename after a ";", like
             this:
    
                 % pp -a "old_filename.txt;new_filename.txt"
                 % pp -a "old_dirname;new_dirname"
    
             You may specify "-a" multiple times.
    

    --

    Oh Lord, won’t you burn me a Knoppix CD ?
    My friends all rate Windows, I must disagree.
    Your powers of persuasion will set them all free,
    So oh Lord, won’t you burn me a Knoppix CD ?
    (Missquoting Janis Joplin)

      Yes, but what OP needs is a way then, from the perl script, to know where the included file is in order to read it. This part of the FAQ led me to look at $ENV{PAR_TEMP} and see what the directory structure was and where the added test.txt was placed.
      # pp -o a.exe -a test.txt test.pl #test.pl: open FILE, $ENV{PAR_TEMP} . "/inc/" . "test.txt"; while(<FILE>){ print; } close FILE;

        That's it. Thanks!

        Everything is extraced under the $ENV{PAR_TEMP}/inc/. I was able to successfully create a test script using the following:

        test.xml
        <opt date="test> <test>This is a test</test> <company id="test" /> </opt>
        test.pl
        #!/usr/bin/perl use strict; use XML::Simple; use Data::Dumper; my $xml = new XML::Simple; my $ret = $xml->($ENV{'PAR_TEMP'} . "/inc/test.xml"); print Dumper($ret);
        Compiled with: pp -o test.exe -a test.xml test.pl

        Moved the PAR package to another directory and ran it. It ran successfully.

        I guess for testing, one can check to see if PAR_TEMP is set. If it's not, assume a command line run else assume PAR run, since the PATH to your file will definatly be different based on a PAR or command line run.

        -- philip
        We put the 'K' in kwality!

      Nice! Then do I access the file like I normally would: $xml=XMLin("config.xml")? Or do I access it via "/config.xml"?

      Thanks again though, this is one step I needed, which I actually forgot about. The problem I was facing was referencing the file...

      -- philip
      We put the 'K' in kwality!

Re: "PAR'ing" XML configs?
by holli (Monsignor) on Aug 08, 2005 at 18:07 UTC
    ...but I want to use XML for the config file
    I hope for the sake of your immortal soul, that in your xml you store something more complicated than
    <options> <section name="users"> <option name="larry" value="in0n3line?" /> <option name="dominus" value="errorme$$age!" /> </section> </options>
    Such data, which could far easier (and more robust than with XML::Simple (sometimes it's a PITA to get XML::Simple to parse or write how you want it to)) written as classic INI-Files. Using XML for this may be considered NTBI™ (Not The Best Idea). For a deeper discussion refer Configuration file design an my reply to it.


    holli, /regexed monk/
      It is far more complex than that. A small taste of it (a bit large to write it out fully)
      <opt> <tmpl_xml> <table> <pkey>key1</pkey> <pkey>ky2</pkey> </table> </tmpl_xml> <config> <bad_procs> <proc name="procd" /> <proc name="procd2"> <exempt>hostname</exempt> </proc> </bad_procs> <perm_checks> <file group="grp" path="/etc" user="user" perm="00755" /> </perm_checks> <oneliners> <command id="non-root RC scripts"> find /etc/rc?.d /etc/init.d ! -user root </command> </oneliners> </config> </opt>

      There's really much more than that, but it's just a small sample of it. It's a configuration audit check/data collector for remote systems. The config is to define all files to capture, processes to check good/bad, exceptions, port checks, etc.... It's actually a rather large XML file (443 lines--244 lines with AttrIndent off). Currently I'm using XML::Simple to parse it, but not sure if that's really the best way to parse it. But for now, it's the only possible resource I have (can't compile expact due removal of our compile tools).

      sometimes it's a PITA to get XML::Simple to parse or write how you want it to

      Yeah, fully aware of that. This is why I'm starting with the config file. I'm getting it exactly the way I want it and getting it parsed just how I want it with XML::Simple. Right now, everything's pretty much near perfect (I have to access my one-liners one more hash level than I'd like, but it's no biggy--only an additiona 8 characters to type in my code, once).

      -- philip
      We put the 'K' in kwality!

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://481911]
Approved by rinceWind
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others making s'mores by the fire in the courtyard of the Monastery: (8)
As of 2015-07-28 07:06 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    The top three priorities of my open tasks are (in descending order of likelihood to be worked on) ...









    Results (252 votes), past polls