<br><br><div class="gmail_quote">On Mon, Feb 21, 2011 at 10:21 PM, John Ricker <span dir="ltr">&lt;<a href="mailto:sephtin@gmail.com">sephtin@gmail.com</a>&gt;</span> wrote:<br><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex;">
Just having converted a bash script to Perl, ran across a couple issues, and thought maybe sending to the list might obtain a nudge in the right direction.. :)<div><br></div><div>The issue I&#39;m currently trying to tackle is pulling in config info.</div>

<div>I have a config information I want to include, but it&#39;s dependent on a variable(s) set in my script.  In bash, I&#39;d just source the file, and it&#39;d go through the if statements, and all is well.  In Perl, I can&#39;t seem to tackle it...</div>

<div><br></div><div>Example:</div><div>#<a href="http://main.pl" target="_blank">main.pl</a></div><div>if (stuff) {</div><div>    $varsneeded = &quot;first&quot;;</div><div>}</div><div>elsif (otherstuff) {</div><div>    $varsneeded = &quot;second&quot;;</div>

<div>}</div><div>else {</div><div>    $varsneeded = &quot;last&quot;;</div><div>}</div><div><br></div><div>&lt;need to read in variables here&gt;</div><div>&lt;then do stuff with variables&gt;</div><div><br></div><div>---x---</div>

<div>#configFile:</div><div>if ($varsneeded eq &quot;first&quot;) {</div><div>    $var1 = &quot;1&quot;;</div><div>    $var2 = &quot;2&quot;;</div><div>    ...</div><div>}</div><div>elseif ($varsneeded eq &quot;second&quot;) {</div>

<div>    $var1 = &quot;somethingelse1&quot;;</div><div>    $var2 = &quot;somethingelse2&quot;;</div><div>    ...</div><div>}</div><div><br></div><div>You get the idea.</div><div><br></div><div>So, I&#39;ve tried:</div><div>

--use:  Did testing with use, and thought it was my answer, but unfortunately the directory that I need to grab the config file(s) from is set from a variable... which isn&#39;t defined until after compile time, so use doesn&#39;t seem to be an option.</div>

<div>--do/require:  Can&#39;t seem to find a way to pass the $varsneeded variable into the config file(s) (.pl, .pm, .whatever) when using do or require.  If there&#39;s an example out there of this, then somehow google has not been kind enough to enlighten me.  :P</div>

<div>--eval `config file`: variable to the config file, same issue as do/require attempts.</div><div><br></div><div>Obviously I&#39;ve oversimplified the example.  The script is over 1200 lines, so simplified for good reason.  Looking for best practice kind of thing here.. if more detail is needed (I expect it may come to that).. I can fill in a lot of blanks, just need to know what info might be helpful.</div>

<div><br></div><div>More info--</div><div>After some thought, and after re-reading my question.. thought some additional info might be helpful.</div><div><br></div><div>The script is for creating custom themes.  I&#39;m taking choices made on a php page that are passed to the script, and the script is pulling in variables based on those choices... and some other factors.. :P</div>

<div>Each section is approx. 26 variables right now, and there are 19 different possible options with another dozen or two coming soon.</div><div><br></div><div>MY answer to this all, is to break up the config file into a file for each option, and just pull in the appropriate file... But as this beast grows, I&#39;d REALLY like the convenience of a find/replace in ONE file when something changes, instead of 50 or more!</div>
</blockquote><div><br></div><div>I don&#39;t understand what you&#39;re saying here--do you mean that you would like all the configuration to be in one file?</div><div><br></div><div>You could have one single perl data structure with all the data:</div>
<div><br></div><div><font class="Apple-style-span" face="&#39;courier new&#39;, monospace">my $config={</font></div><div><font class="Apple-style-span" face="&#39;courier new&#39;, monospace"><meta charset="utf-8"><div style="font-family: arial; ">
<font class="Apple-style-span" face="&#39;courier new&#39;, monospace">    theme1=&gt;{</font></div><div style="font-family: arial; "><font class="Apple-style-span" face="&#39;courier new&#39;, monospace">        option1=&gt;{ </font></div>
<div style="font-family: arial; "><font class="Apple-style-span" face="&#39;courier new&#39;, monospace">            var1=&gt;&#39;theme1option1var1&#39;, </font></div><div style="font-family: arial; "><div><font class="Apple-style-span" face="&#39;courier new&#39;, monospace">            var2=&gt;&#39;theme1option1var2&#39;, </font></div>
</div><div><font class="Apple-style-span" face="&#39;courier new&#39;, monospace"><div style="font-family: arial; "><font class="Apple-style-span" face="&#39;courier new&#39;, monospace">            var3=&gt;&#39;theme1option1var3&#39;, </font></div>
<div style="font-family: arial; "><font class="Apple-style-span" face="&#39;courier new&#39;, monospace">        },</font></div><div><font class="Apple-style-span" face="&#39;courier new&#39;, monospace"><div style="font-family: arial; ">
<font class="Apple-style-span" face="&#39;courier new&#39;, monospace">        option2=&gt;{ </font></div><div style="font-family: arial; "><font class="Apple-style-span" face="&#39;courier new&#39;, monospace">            var1=&gt;&#39;theme1option2var1&#39;, </font></div>
<div style="font-family: arial; "><div><font class="Apple-style-span" face="&#39;courier new&#39;, monospace">            var2=&gt;&#39;theme1option2var2&#39;, </font></div></div><div><font class="Apple-style-span" face="&#39;courier new&#39;, monospace"><div style="font-family: arial; ">
<font class="Apple-style-span" face="&#39;courier new&#39;, monospace">            var3=&gt;&#39;theme1option2var3&#39;, </font></div><div style="font-family: arial; "><font class="Apple-style-span" face="&#39;courier new&#39;, monospace">        },</font></div>
<div style="font-family: arial; "><font class="Apple-style-span" face="&#39;courier new&#39;, monospace">    },</font></div><div><font class="Apple-style-span" face="&#39;courier new&#39;, monospace"><meta charset="utf-8"><div style="font-family: arial; ">
<font class="Apple-style-span" face="&#39;courier new&#39;, monospace">    theme2=&gt;{</font></div><div style="font-family: arial; "><font class="Apple-style-span" face="&#39;courier new&#39;, monospace">        option1=&gt;{ </font></div>
<div style="font-family: arial; "><font class="Apple-style-span" face="&#39;courier new&#39;, monospace">            var1=&gt;&#39;theme2option1var1&#39;, </font></div><div style="font-family: arial; "><div><font class="Apple-style-span" face="&#39;courier new&#39;, monospace">            var2=&gt;&#39;theme2option1var2&#39;, </font></div>
</div><div><font class="Apple-style-span" face="&#39;courier new&#39;, monospace"><div style="font-family: arial; "><font class="Apple-style-span" face="&#39;courier new&#39;, monospace">            var3=&gt;&#39;theme2option1var3&#39;, </font></div>
<div style="font-family: arial; "><font class="Apple-style-span" face="&#39;courier new&#39;, monospace">        },</font></div><div><font class="Apple-style-span" face="&#39;courier new&#39;, monospace"><div style="font-family: arial; ">
<font class="Apple-style-span" face="&#39;courier new&#39;, monospace">        option2=&gt;{ </font></div><div style="font-family: arial; "><font class="Apple-style-span" face="&#39;courier new&#39;, monospace">            var1=&gt;&#39;theme2option2var1&#39;, </font></div>
<div style="font-family: arial; "><div><font class="Apple-style-span" face="&#39;courier new&#39;, monospace">            var2=&gt;&#39;theme2option2var2&#39;, </font></div></div><div><font class="Apple-style-span" face="&#39;courier new&#39;, monospace"><div style="font-family: arial; ">
<font class="Apple-style-span" face="&#39;courier new&#39;, monospace">            var3=&gt;&#39;theme2option2var3&#39;, </font></div><div style="font-family: arial; "><font class="Apple-style-span" face="&#39;courier new&#39;, monospace">        },</font></div>
<div style="font-family: arial; "><font class="Apple-style-span" face="&#39;courier new&#39;, monospace">    },</font></div><div>};</div><div><br></div><div>my $theme_choice = shift;</div><div>my $option_choice = shift;</div>
<div>my $theme_key = &#39;theme&#39;.$theme_choice;</div><div>my $option_key = &#39;option&#39;.$option_choice;</div><div><br></div><div>my $config_to_use = $config-&gt;{$theme_key}{$option_key};</div><div><br></div><div>
foreach my $var_name (qw/var1 var2 var3/) {</div><div>    print &quot;var1: $config_to_use-&gt;{$var_name}\n&quot;;</div><div>}    </div></font></div></font></div></font></div></font></div></font></div></font></div></font></div>
</font></div><div><div><br></div><div>msmbp:~ msouth$ perl <a href="http://glurg.pl">glurg.pl</a> 1 a</div><div>var1: theme1,optiona,var1</div><div>var1: theme1,optiona,var2</div><div>var1: theme1,optiona,var3</div><div>msmbp:~ msouth$ perl <a href="http://glurg.pl">glurg.pl</a> 2 b</div>
<div>var1: theme2,optionb,var1</div><div>var1: theme2,optionb,var2</div><div>var1: theme2,optionb,var3</div></div><div><br></div><div>That would give you one config file to edit.</div><div><br></div><div>If you want to do it per your original description, bash-ish-ly (I don&#39;t recommend this), below is how (I used the environment (accessed via %ENV) to simulate data coming in that you don&#39;t know until runtime--should have used &quot;shift&quot; to make it easier).  There are some techniques (requiring a file you didn&#39;t know the location of until runtime, sharing a variable via &quot;our&quot;) that might be of interest.</div>
<div><br></div><div>mike</div><div><br></div><div><div><font class="Apple-style-span" face="&#39;courier new&#39;, monospace">msmbp:~ msouth$ cat <a href="http://bob.pl">bob.pl</a></font></div><div><font class="Apple-style-span" face="&#39;courier new&#39;, monospace">use strict;</font></div>
<div><font class="Apple-style-span" face="&#39;courier new&#39;, monospace">use warnings;</font></div><div><font class="Apple-style-span" face="&#39;courier new&#39;, monospace"><br></font></div><div><font class="Apple-style-span" face="&#39;courier new&#39;, monospace">our $which_config;</font></div>
<div><font class="Apple-style-span" face="&#39;courier new&#39;, monospace"><br></font></div><div><font class="Apple-style-span" face="&#39;courier new&#39;, monospace">our $var1;</font></div><div><font class="Apple-style-span" face="&#39;courier new&#39;, monospace"><br>
</font></div><div><font class="Apple-style-span" face="&#39;courier new&#39;, monospace">our $which_dir = &#39;bob&#39;;</font></div><div><font class="Apple-style-span" face="&#39;courier new&#39;, monospace"><br></font></div>
<div><font class="Apple-style-span" face="&#39;courier new&#39;, monospace">$which_config = &#39;one&#39;;</font></div><div><font class="Apple-style-span" face="&#39;courier new&#39;, monospace">#$which_config = &#39;two&#39;;</font></div>
<div><font class="Apple-style-span" face="&#39;courier new&#39;, monospace"><br></font></div><div><font class="Apple-style-span" face="&#39;courier new&#39;, monospace">my $theme_sub_dir = $ENV{theme} || die &quot;set theme in the env like this: theme=&#39;test&#39; $0\n&quot;;</font></div>
<div><font class="Apple-style-span" face="&#39;courier new&#39;, monospace"><br></font></div><div><font class="Apple-style-span" face="&#39;courier new&#39;, monospace">my $full_path_to_require = &quot;/Users/msouth/$theme_sub_dir&quot;;</font></div>
<div><font class="Apple-style-span" face="&#39;courier new&#39;, monospace">die &quot;there is not a directory called $full_path_to_require&quot; unless -d $full_path_to_require;</font></div><div><font class="Apple-style-span" face="&#39;courier new&#39;, monospace">push @INC, $full_path_to_require;;</font></div>
<div><font class="Apple-style-span" face="&#39;courier new&#39;, monospace"><br></font></div><div><font class="Apple-style-span" face="&#39;courier new&#39;, monospace">require &quot;$which_dir/<a href="http://set_up_bob.pl">set_up_bob.pl</a>&quot;;</font></div>
<div><font class="Apple-style-span" face="&#39;courier new&#39;, monospace"><br></font></div><div><font class="Apple-style-span" face="&#39;courier new&#39;, monospace">print $var1,$/;</font></div><div><font class="Apple-style-span" face="&#39;courier new&#39;, monospace">msmbp:~ msouth$ cat bob/<a href="http://set_up_bob.pl">set_up_bob.pl</a></font></div>
<div><font class="Apple-style-span" face="&#39;courier new&#39;, monospace">use strict;</font></div><div><font class="Apple-style-span" face="&#39;courier new&#39;, monospace">use warnings;</font></div><div><font class="Apple-style-span" face="&#39;courier new&#39;, monospace"><br>
</font></div><div><font class="Apple-style-span" face="&#39;courier new&#39;, monospace">our $which_config;</font></div><div><font class="Apple-style-span" face="&#39;courier new&#39;, monospace">our $var1;</font></div><div>
<font class="Apple-style-span" face="&#39;courier new&#39;, monospace"><br></font></div><div><font class="Apple-style-span" face="&#39;courier new&#39;, monospace">if ($which_config eq &#39;one&#39;) {</font></div><div><font class="Apple-style-span" face="&#39;courier new&#39;, monospace">    $var1=&#39;the one version of var1&#39;;</font></div>
<div><font class="Apple-style-span" face="&#39;courier new&#39;, monospace">}</font></div><div><font class="Apple-style-span" face="&#39;courier new&#39;, monospace">elsif ($which_config eq &#39;two&#39; ) {</font></div><div>
<font class="Apple-style-span" face="&#39;courier new&#39;, monospace">    $var1=&#39;the two version of var1&#39;;</font></div><div><font class="Apple-style-span" face="&#39;courier new&#39;, monospace">}</font></div><div>
<font class="Apple-style-span" face="&#39;courier new&#39;, monospace">else {</font></div><div><font class="Apple-style-span" face="&#39;courier new&#39;, monospace">    die &quot;you didn&#39;t tell me which config in the &#39;which_config&#39; variable, or maybe you didn&#39;t declare it with &#39;our&#39;&quot;;</font></div>
<div><font class="Apple-style-span" face="&#39;courier new&#39;, monospace">}</font></div><div><font class="Apple-style-span" face="&#39;courier new&#39;, monospace">msmbp:~ msouth$ theme=bob perl <a href="http://bob.pl">bob.pl</a></font></div>
<div><font class="Apple-style-span" face="&#39;courier new&#39;, monospace">the one version of var1</font></div></div><div><br></div></div>