[Raleigh-talk] Issues after converting bash to Perl - way to call a config file that contains if/then logic

Matt Nash mattnashbrowns at gmail.com
Mon Feb 21 23:05:25 PST 2011


How about this?  Build a giant hash containing all of your config options
(using Config::File) and move the current config's logic to the main script
to choose the branch of the hash that corresponds to your particular set of
options.

%Options = (
  first => { opt1 => val1, opt2 => val2},
  second => {opt1 => val3, opt2 => val4}
);

if ($selection eq 'first') {
 %RelevantOptions = %{ $Options{'first'} };
}

etc.

On Tue, Feb 22, 2011 at 1:57 AM, Matt Nash <mattnashbrowns at gmail.com> wrote:

> Oops, now I see that the real problem is conditional logic in the config
> file.  Without knowing all the details of what you are trying to do, that
> kind of seems like a bad idea.  I'm sure you have a good reason for it.
>
>
> On Tue, Feb 22, 2011 at 1:56 AM, Matt Nash <mattnashbrowns at gmail.com>wrote:
>
>> Would Config::File work for you?
>> http://search.cpan.org/~gwolf/Config-File-1.50/lib/Config/File.pm
>>
>>  <http://search.cpan.org/~gwolf/Config-File-1.50/lib/Config/File.pm>There
>> is a well-respected "Perl Best Practices" book which some jerk seems to have
>> put up on the web in its entirety.  If you search for the title and "config
>> file" you will find some helpful guidelines to reading and writing config
>> files in Perl, but I'm not linking to it.
>>
>> On Mon, Feb 21, 2011 at 11:21 PM, John Ricker <sephtin at gmail.com> wrote:
>>
>>> 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.. :)
>>>
>>> The issue I'm currently trying to tackle is pulling in config info.
>>> I have a config information I want to include, but it's dependent on a
>>> variable(s) set in my script.  In bash, I'd just source the file, and it'd
>>> go through the if statements, and all is well.  In Perl, I can't seem to
>>> tackle it...
>>>
>>> Example:
>>> #main.pl
>>> if (stuff) {
>>>     $varsneeded = "first";
>>> }
>>> elsif (otherstuff) {
>>>     $varsneeded = "second";
>>> }
>>> else {
>>>     $varsneeded = "last";
>>> }
>>>
>>> <need to read in variables here>
>>> <then do stuff with variables>
>>>
>>> ---x---
>>> #configFile:
>>> if ($varsneeded eq "first") {
>>>     $var1 = "1";
>>>     $var2 = "2";
>>>     ...
>>> }
>>> elseif ($varsneeded eq "second") {
>>>     $var1 = "somethingelse1";
>>>     $var2 = "somethingelse2";
>>>     ...
>>> }
>>>
>>> You get the idea.
>>>
>>> So, I've tried:
>>> --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't defined until after compile time, so use
>>> doesn't seem to be an option.
>>> --do/require:  Can'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's an example out there of this, then somehow google has not been kind
>>> enough to enlighten me.  :P
>>> --eval `config file`: variable to the config file, same issue as
>>> do/require attempts.
>>>
>>> Obviously I'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.
>>>
>>> More info--
>>> After some thought, and after re-reading my question.. thought some
>>> additional info might be helpful.
>>>
>>> The script is for creating custom themes.  I'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
>>> Each section is approx. 26 variables right now, and there are 19
>>> different possible options with another dozen or two coming soon.
>>>
>>> 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'd REALLY like the convenience of a find/replace in ONE file when
>>> something changes, instead of 50 or more!
>>>
>>> Thanks in advance for any assistance!
>>> -John (sephtin @gmail)
>>>
>>>
>>> _______________________________________________
>>> Raleigh-talk mailing list
>>> Raleigh-talk at pm.org
>>> http://mail.pm.org/mailman/listinfo/raleigh-talk
>>>
>>>
>>
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.pm.org/pipermail/raleigh-talk/attachments/20110222/d8e5a385/attachment.html>


More information about the Raleigh-talk mailing list