Hello, I have an XML file and a Perl script that uses XML::Simple. I am trying to grab a command line parameter, and use it to select a certain section of the XML file for configuration options...


The XML, saved as script.xml:
<config> <profile name="Test"> <dat_file_location>e:\</dat_file_location> <logdir>Test</logdir> <drop_script>n_a</drop_script> <database_type>MSSQL</database_type> <scripts_sql_dlt>blah</scripts_sql_dlt> <TNS-DSN_name>SwissTest</TNS-DSN_name> <db_user>db_builder</db_user> <password>pass</password> <log_file>Test.log</log_file> <single_sql_file>create_db.sql</single_sql_file> <single_dlt_file>blah</single_dlt_file> </profile> <profile name="Live"> <dat_file_location>e:\</dat_file_location> <logdir>Live</logdir> <drop_script>n_a</drop_script> <database_type>MSSQL</database_type> <scripts_sql_dlt>blah</scripts_sql_dlt> <TNS-DSN_name>Live</TNS-DSN_name> <db_user>db_builder</db_user> <password>pass</password> <log_file>Live.log</log_file> <single_sql_file>create_db.sql</single_sql_file> <single_dlt_file>blah</single_dlt_file> </profile> </config>
The code, saved as script.pl:
# Load XML Config file... use XML::Simple; my $config = XMLin(); # Find the right config... # This should be the only commandline argument my $desired_config = $ARGV[0]; # Dump the config file.... use Data::Dumper; print Dumper($config); print $config->{$desired_config}->{database_type};
And calling it as "script.pl Test" I am not able to get it to work...

I have tried all of the following:
print $config->{profile}->{$desired_config}->{database_type}; print "\nTest1\n"; print $config->{profile}->{config}=>$desired_config->{database_type}; print "\nTest2\n"; print $config->{profile}->{config => $desired_config}->{database_type} +; print "\nTest3\n"; # Below is the only one that works.... # It is hardcoded... print $config->{profile}->{Test}->{database_type}; print "\nTest4\n"; print $config->{$desired_config}->{database_type}; print "\nTest5\n"; print $config->{profile}->{$desired_config}->{database_type}; print "\nTest6\n"; print $config->{profile}=>{$desired_config}->{database_type}; print "\nTest7\n";
I need to be able to specify the config from the command line...

Thanks,
amonotod

In reply to XML::Simple and variable interpolation by amonotod

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.