If you really want to use require you have to declare the variables in your main script first with &#39;our&#39; like this:<div><br></div><div>our ($var1, $var2, $var3);</div><div>require $configfile;</div><div># now $var1, etc will have whatever values were set in the config file</div>
<meta charset="utf-8"><div><br></div><div>However, I hesitate to even point that out since logic in the config file is probably a bad idea as was pointed out. If you&#39;re hash of all possible options is not super big (megabytes) I&#39;d just go that route and not worry about the memory. You may be optimizing prematurely.</div>
<div><br></div><div>Trevor<br><br><div class="gmail_quote">On Tue, Feb 22, 2011 at 2:50 AM, John Ricker <span dir="ltr">&lt;<a href="mailto:sephtin%2Bpm-talk@gmail.com">sephtin+pm-talk@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;">
<div><div></div><div class="h5"><div class="gmail_quote">On Tue, Feb 22, 2011 at 2:08 AM, Mike South <span dir="ltr">&lt;<a href="mailto:msouth@gmail.com" target="_blank">msouth@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">

<br><br><div class="gmail_quote"><div><div></div><div>On Mon, Feb 21, 2011 at 10:21 PM, John Ricker <span dir="ltr">&lt;<a href="mailto:sephtin@gmail.com" target="_blank">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></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 face="&#39;courier new&#39;, monospace">my $config={</font></div><div><font face="&#39;courier new&#39;, monospace"><div style="font-family:arial">
<font face="&#39;courier new&#39;, monospace">    theme1=&gt;{</font></div><div style="font-family:arial"><font face="&#39;courier new&#39;, monospace">        option1=&gt;{ </font></div>
<div style="font-family:arial"><font face="&#39;courier new&#39;, monospace">            var1=&gt;&#39;theme1option1var1&#39;, </font></div><div style="font-family:arial"><div><font face="&#39;courier new&#39;, monospace">            var2=&gt;&#39;theme1option1var2&#39;, </font></div>


</div><div><font face="&#39;courier new&#39;, monospace"><div style="font-family:arial"><font face="&#39;courier new&#39;, monospace">            var3=&gt;&#39;theme1option1var3&#39;, </font></div>
<div style="font-family:arial"><font face="&#39;courier new&#39;, monospace">        },</font></div><div><font face="&#39;courier new&#39;, monospace"><div style="font-family:arial">
<font face="&#39;courier new&#39;, monospace">        option2=&gt;{ </font></div><div style="font-family:arial"><font face="&#39;courier new&#39;, monospace">            var1=&gt;&#39;theme1option2var1&#39;, </font></div>


<div style="font-family:arial"><div><font face="&#39;courier new&#39;, monospace">            var2=&gt;&#39;theme1option2var2&#39;, </font></div></div><div><font face="&#39;courier new&#39;, monospace"><div style="font-family:arial">


<font face="&#39;courier new&#39;, monospace">            var3=&gt;&#39;theme1option2var3&#39;, </font></div><div style="font-family:arial"><font face="&#39;courier new&#39;, monospace">        },</font></div>
<div style="font-family:arial"><font face="&#39;courier new&#39;, monospace">    },</font></div><div><font face="&#39;courier new&#39;, monospace"><div style="font-family:arial">
<font face="&#39;courier new&#39;, monospace">    theme2=&gt;{</font></div><div style="font-family:arial"><font face="&#39;courier new&#39;, monospace">        option1=&gt;{ </font></div>
<div style="font-family:arial"><font face="&#39;courier new&#39;, monospace">            var1=&gt;&#39;theme2option1var1&#39;, </font></div><div style="font-family:arial"><div><font face="&#39;courier new&#39;, monospace">            var2=&gt;&#39;theme2option1var2&#39;, </font></div>


</div><div><font face="&#39;courier new&#39;, monospace"><div style="font-family:arial"><font face="&#39;courier new&#39;, monospace">            var3=&gt;&#39;theme2option1var3&#39;, </font></div>
<div style="font-family:arial"><font face="&#39;courier new&#39;, monospace">        },</font></div><div><font face="&#39;courier new&#39;, monospace"><div style="font-family:arial">
<font face="&#39;courier new&#39;, monospace">        option2=&gt;{ </font></div><div style="font-family:arial"><font face="&#39;courier new&#39;, monospace">            var1=&gt;&#39;theme2option2var1&#39;, </font></div>


<div style="font-family:arial"><div><font face="&#39;courier new&#39;, monospace">            var2=&gt;&#39;theme2option2var2&#39;, </font></div></div><div><font face="&#39;courier new&#39;, monospace"><div style="font-family:arial">


<font face="&#39;courier new&#39;, monospace">            var3=&gt;&#39;theme2option2var3&#39;, </font></div><div style="font-family:arial"><font face="&#39;courier new&#39;, monospace">        },</font></div>
<div style="font-family:arial"><font 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" target="_blank">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" target="_blank">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 face="&#39;courier new&#39;, monospace">msmbp:~ msouth$ cat <a href="http://bob.pl" target="_blank">bob.pl</a></font></div><div><font face="&#39;courier new&#39;, monospace">use strict;</font></div>


<div><font face="&#39;courier new&#39;, monospace">use warnings;</font></div><div><font face="&#39;courier new&#39;, monospace"><br></font></div><div><font face="&#39;courier new&#39;, monospace">our $which_config;</font></div>


<div><font face="&#39;courier new&#39;, monospace"><br></font></div><div><font face="&#39;courier new&#39;, monospace">our $var1;</font></div><div><font face="&#39;courier new&#39;, monospace"><br>
</font></div><div><font face="&#39;courier new&#39;, monospace">our $which_dir = &#39;bob&#39;;</font></div><div><font face="&#39;courier new&#39;, monospace"><br></font></div>
<div><font face="&#39;courier new&#39;, monospace">$which_config = &#39;one&#39;;</font></div><div><font face="&#39;courier new&#39;, monospace">#$which_config = &#39;two&#39;;</font></div>
<div><font face="&#39;courier new&#39;, monospace"><br></font></div><div><font 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 face="&#39;courier new&#39;, monospace"><br></font></div><div><font face="&#39;courier new&#39;, monospace">my $full_path_to_require = &quot;/Users/msouth/$theme_sub_dir&quot;;</font></div>
<div><font 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 face="&#39;courier new&#39;, monospace">push @INC, $full_path_to_require;;</font></div>


<div><font face="&#39;courier new&#39;, monospace"><br></font></div><div><font face="&#39;courier new&#39;, monospace">require &quot;$which_dir/<a href="http://set_up_bob.pl" target="_blank">set_up_bob.pl</a>&quot;;</font></div>


<div><font face="&#39;courier new&#39;, monospace"><br></font></div><div><font face="&#39;courier new&#39;, monospace">print $var1,$/;</font></div><div><font face="&#39;courier new&#39;, monospace">msmbp:~ msouth$ cat bob/<a href="http://set_up_bob.pl" target="_blank">set_up_bob.pl</a></font></div>


<div><font face="&#39;courier new&#39;, monospace">use strict;</font></div><div><font face="&#39;courier new&#39;, monospace">use warnings;</font></div><div><font face="&#39;courier new&#39;, monospace"><br>
</font></div><div><font face="&#39;courier new&#39;, monospace">our $which_config;</font></div><div><font face="&#39;courier new&#39;, monospace">our $var1;</font></div><div>
<font face="&#39;courier new&#39;, monospace"><br></font></div><div><font face="&#39;courier new&#39;, monospace">if ($which_config eq &#39;one&#39;) {</font></div><div><font face="&#39;courier new&#39;, monospace">    $var1=&#39;the one version of var1&#39;;</font></div>


<div><font face="&#39;courier new&#39;, monospace">}</font></div><div><font face="&#39;courier new&#39;, monospace">elsif ($which_config eq &#39;two&#39; ) {</font></div><div>
<font face="&#39;courier new&#39;, monospace">    $var1=&#39;the two version of var1&#39;;</font></div><div><font face="&#39;courier new&#39;, monospace">}</font></div><div>
<font face="&#39;courier new&#39;, monospace">else {</font></div><div><font 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 face="&#39;courier new&#39;, monospace">}</font></div><div><font face="&#39;courier new&#39;, monospace">msmbp:~ msouth$ theme=bob perl <a href="http://bob.pl" target="_blank">bob.pl</a></font></div>
<div><font face="&#39;courier new&#39;, monospace">the one version of var1</font></div></div><div><br></div></div>
</blockquote></div><br></div></div><div><div>After seeing both of you (Matt/Mike) suggesting the same thing.. I may need to revisit a config file that just loads everything (hash most likely), but wouldn&#39;t that keep everything in memory?  (While maybe not that much memory... but it would add up.. no?).</div>

<div>I guess I assumed that the &quot;cleaner&quot; way to do it, was to only load the variables I was going to use, while ignoring those that I&#39;m not interested in... I&#39;m fairly new to Perl, so clueless about best practices.</div>

<div><br></div><div>Re: the &quot;bash&#39;ish&quot; way... was the only way I could come up with that didn&#39;t load everything regardless of what I needed... other than splitting up each theme into separate files, which I&#39;m trying to avoid.</div>

<div>My failure was with use(), since it required path at runtime (using lib) which I couldn&#39;t provide at compile time as the path is a variable defined in the file.</div><div>I DID attempt require, but wasn&#39;t able to pass the variable to the config file.  I must have been doing something wrong because after checking Mike&#39;s example and switching things back for require, it seems to be working...  I&#39;ll probably comment it out, and try what seems to be the most recommended method... just hashing everything, and reading it that way... good learning experience at the very least...   :)</div>

<div><br></div><div>Thanks for the quick feedback.  Have somewhere to start from at least...</div><div><br></div><font color="#888888"><div>-John</div></font></div>
<br>_______________________________________________<br>
Raleigh-talk mailing list<br>
<a href="mailto:Raleigh-talk@pm.org">Raleigh-talk@pm.org</a><br>
<a href="http://mail.pm.org/mailman/listinfo/raleigh-talk" target="_blank">http://mail.pm.org/mailman/listinfo/raleigh-talk</a><br>
<br></blockquote></div><br></div>