[Raleigh-talk] Issues after converting bash to Perl - way to call a config file that contains if/then logic
Mike South
msouth at gmail.com
Mon Feb 21 23:08:06 PST 2011
On Mon, Feb 21, 2011 at 10: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!
>
I don't understand what you're saying here--do you mean that you would like
all the configuration to be in one file?
You could have one single perl data structure with all the data:
my $config={
theme1=>{
option1=>{
var1=>'theme1option1var1',
var2=>'theme1option1var2',
var3=>'theme1option1var3',
},
option2=>{
var1=>'theme1option2var1',
var2=>'theme1option2var2',
var3=>'theme1option2var3',
},
},
theme2=>{
option1=>{
var1=>'theme2option1var1',
var2=>'theme2option1var2',
var3=>'theme2option1var3',
},
option2=>{
var1=>'theme2option2var1',
var2=>'theme2option2var2',
var3=>'theme2option2var3',
},
},
};
my $theme_choice = shift;
my $option_choice = shift;
my $theme_key = 'theme'.$theme_choice;
my $option_key = 'option'.$option_choice;
my $config_to_use = $config->{$theme_key}{$option_key};
foreach my $var_name (qw/var1 var2 var3/) {
print "var1: $config_to_use->{$var_name}\n";
}
msmbp:~ msouth$ perl glurg.pl 1 a
var1: theme1,optiona,var1
var1: theme1,optiona,var2
var1: theme1,optiona,var3
msmbp:~ msouth$ perl glurg.pl 2 b
var1: theme2,optionb,var1
var1: theme2,optionb,var2
var1: theme2,optionb,var3
That would give you one config file to edit.
If you want to do it per your original description, bash-ish-ly (I don't
recommend this), below is how (I used the environment (accessed via %ENV) to
simulate data coming in that you don't know until runtime--should have used
"shift" to make it easier). There are some techniques (requiring a file you
didn't know the location of until runtime, sharing a variable via "our")
that might be of interest.
mike
msmbp:~ msouth$ cat bob.pl
use strict;
use warnings;
our $which_config;
our $var1;
our $which_dir = 'bob';
$which_config = 'one';
#$which_config = 'two';
my $theme_sub_dir = $ENV{theme} || die "set theme in the env like this:
theme='test' $0\n";
my $full_path_to_require = "/Users/msouth/$theme_sub_dir";
die "there is not a directory called $full_path_to_require" unless -d
$full_path_to_require;
push @INC, $full_path_to_require;;
require "$which_dir/set_up_bob.pl";
print $var1,$/;
msmbp:~ msouth$ cat bob/set_up_bob.pl
use strict;
use warnings;
our $which_config;
our $var1;
if ($which_config eq 'one') {
$var1='the one version of var1';
}
elsif ($which_config eq 'two' ) {
$var1='the two version of var1';
}
else {
die "you didn't tell me which config in the 'which_config' variable, or
maybe you didn't declare it with 'our'";
}
msmbp:~ msouth$ theme=bob perl bob.pl
the one version of var1
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.pm.org/pipermail/raleigh-talk/attachments/20110222/a102a64d/attachment-0001.html>
More information about the Raleigh-talk
mailing list