From sephtin at gmail.com Mon Feb 21 20:21:03 2011 From: sephtin at gmail.com (John Ricker) Date: Mon, 21 Feb 2011 23:21:03 -0500 Subject: [Raleigh-talk] Issues after converting bash to Perl - way to call a config file that contains if/then logic Message-ID: 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"; } ---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) -------------- next part -------------- An HTML attachment was scrubbed... URL: From mattnashbrowns at gmail.com Mon Feb 21 22:56:09 2011 From: mattnashbrowns at gmail.com (Matt Nash) Date: Tue, 22 Feb 2011 01:56:09 -0500 Subject: [Raleigh-talk] Issues after converting bash to Perl - way to call a config file that contains if/then logic In-Reply-To: References: Message-ID: Would Config::File work for you? 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 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"; > } > > > > > ---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: From mattnashbrowns at gmail.com Mon Feb 21 22:57:43 2011 From: mattnashbrowns at gmail.com (Matt Nash) Date: Tue, 22 Feb 2011 01:57:43 -0500 Subject: [Raleigh-talk] Issues after converting bash to Perl - way to call a config file that contains if/then logic In-Reply-To: References: Message-ID: 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 wrote: > Would Config::File work for you? > 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 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"; >> } >> >> >> >> >> ---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: From mattnashbrowns at gmail.com Mon Feb 21 23:05:25 2011 From: mattnashbrowns at gmail.com (Matt Nash) Date: Tue, 22 Feb 2011 02:05:25 -0500 Subject: [Raleigh-talk] Issues after converting bash to Perl - way to call a config file that contains if/then logic In-Reply-To: References: Message-ID: 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 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 wrote: > >> Would Config::File work for you? >> 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 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"; >>> } >>> >>> >>> >>> >>> ---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: From msouth at gmail.com Mon Feb 21 23:08:06 2011 From: msouth at gmail.com (Mike South) Date: Tue, 22 Feb 2011 01:08:06 -0600 Subject: [Raleigh-talk] Issues after converting bash to Perl - way to call a config file that contains if/then logic In-Reply-To: References: Message-ID: On Mon, Feb 21, 2011 at 10:21 PM, John Ricker 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"; > } > > > > > ---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: From sephtin+pm-talk at gmail.com Mon Feb 21 23:50:43 2011 From: sephtin+pm-talk at gmail.com (John Ricker) Date: Tue, 22 Feb 2011 02:50:43 -0500 Subject: [Raleigh-talk] Issues after converting bash to Perl - way to call a config file that contains if/then logic In-Reply-To: References: Message-ID: On Tue, Feb 22, 2011 at 2:08 AM, Mike South wrote: > > > On Mon, Feb 21, 2011 at 10:21 PM, John Ricker 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"; >> } >> >> >> >> >> ---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 > > 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't that keep everything in memory? (While maybe not that much memory... but it would add up.. no?). I guess I assumed that the "cleaner" way to do it, was to only load the variables I was going to use, while ignoring those that I'm not interested in... I'm fairly new to Perl, so clueless about best practices. Re: the "bash'ish" way... was the only way I could come up with that didn't load everything regardless of what I needed... other than splitting up each theme into separate files, which I'm trying to avoid. My failure was with use(), since it required path at runtime (using lib) which I couldn't provide at compile time as the path is a variable defined in the file. I DID attempt require, but wasn't able to pass the variable to the config file. I must have been doing something wrong because after checking Mike's example and switching things back for require, it seems to be working... I'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... :) Thanks for the quick feedback. Have somewhere to start from at least... -John -------------- next part -------------- An HTML attachment was scrubbed... URL: From trevormg19 at gmail.com Tue Feb 22 06:28:51 2011 From: trevormg19 at gmail.com (Trevor Little) Date: Tue, 22 Feb 2011 09:28:51 -0500 Subject: [Raleigh-talk] Issues after converting bash to Perl - way to call a config file that contains if/then logic In-Reply-To: References: Message-ID: If you really want to use require you have to declare the variables in your main script first with 'our' like this: our ($var1, $var2, $var3); require $configfile; # now $var1, etc will have whatever values were set in the config file 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're hash of all possible options is not super big (megabytes) I'd just go that route and not worry about the memory. You may be optimizing prematurely. Trevor On Tue, Feb 22, 2011 at 2:50 AM, John Ricker wrote: > On Tue, Feb 22, 2011 at 2:08 AM, Mike South wrote: > >> >> >> On Mon, Feb 21, 2011 at 10:21 PM, John Ricker 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"; >>> } >>> >>> >>> >>> >>> ---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 >> >> > 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't that keep everything in memory? (While maybe not that much > memory... but it would add up.. no?). > I guess I assumed that the "cleaner" way to do it, was to only load the > variables I was going to use, while ignoring those that I'm not interested > in... I'm fairly new to Perl, so clueless about best practices. > > Re: the "bash'ish" way... was the only way I could come up with that didn't > load everything regardless of what I needed... other than splitting up each > theme into separate files, which I'm trying to avoid. > My failure was with use(), since it required path at runtime (using lib) > which I couldn't provide at compile time as the path is a variable defined > in the file. > I DID attempt require, but wasn't able to pass the variable to the config > file. I must have been doing something wrong because after checking Mike's > example and switching things back for require, it seems to be working... > I'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... :) > > Thanks for the quick feedback. Have somewhere to start from at least... > > -John > > _______________________________________________ > 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: From mpeters at plusthree.com Tue Feb 22 06:32:06 2011 From: mpeters at plusthree.com (Michael Peters) Date: Tue, 22 Feb 2011 09:32:06 -0500 Subject: [Raleigh-talk] Issues after converting bash to Perl - way to call a config file that contains if/then logic In-Reply-To: References: Message-ID: <4D63C8E6.6050105@plusthree.com> On 02/22/2011 02:50 AM, John Ricker wrote: > 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't that keep everything in memory? (While maybe not > that much memory... but it would add up.. no?). How many config variables are we talking about? If it's less than a thousand I doubt you'll event notice the memory usage. -- Michael Peters Plus Three, LP From msouth at gmail.com Tue Feb 22 06:48:58 2011 From: msouth at gmail.com (Mike South) Date: Tue, 22 Feb 2011 08:48:58 -0600 Subject: [Raleigh-talk] Issues after converting bash to Perl - way to call a config file that contains if/then logic In-Reply-To: References: Message-ID: On Tue, Feb 22, 2011 at 1:50 AM, John Ricker wrote: > On Tue, Feb 22, 2011 at 2:08 AM, Mike South wrote: > >> >> >> On Mon, Feb 21, 2011 at 10:21 PM, John Ricker wrote: >> >>> [snip] > Re: the "bash'ish" way... was the only way I could come up with that didn't > load everything regardless of what I needed... other than splitting up each > theme into separate files, which I'm trying to avoid. > My failure was with use(), since it required path at runtime (using lib) > which I couldn't provide at compile time as the path is a variable defined > in the file. > I DID attempt require, but wasn't able to pass the variable to the config > file. I must have been doing something wrong because after checking Mike's > example and switching things back for require, it seems to be working... > I'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... :) > > I just looked through the example and realized I had forgotten to cut part of it out. Sorry if this was confusing. The "$which_dir" variable should be removed completely, in favor $theme_sub_dir. The idea was to show that $theme_sub_dir wasn't known until runtime (since it's being pulled in from the environment), and demonstrate how to require() something from that dynamically-determined dir. The $which_dir variable was from when I was making sure I was right about how I thought it worked :). Change that section to look like this: #comment this out #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;; #just use the file name since @INC has been modified #require "$which_dir/set_up_bob.pl"; require "set_up_bob.pl"; # require "$full_path_to_require/set_up_bob.pl" would work here, too mike -------------- next part -------------- An HTML attachment was scrubbed... URL: From mpeters at plusthree.com Tue Feb 22 07:00:05 2011 From: mpeters at plusthree.com (Michael Peters) Date: Tue, 22 Feb 2011 10:00:05 -0500 Subject: [Raleigh-talk] Issues after converting bash to Perl - way to call a config file that contains if/then logic In-Reply-To: References: Message-ID: <4D63CF75.6030006@plusthree.com> On 02/22/2011 09:28 AM, Trevor Little wrote: > However, I hesitate to even point that out since logic in the config > file is probably a bad idea as was pointed out. In fact, here's nice little article about why it's usually a bad idea: http://taint.org/2011/02/18/001527a.html -- Michael Peters Plus Three, LP From sephtin+pm-talk at gmail.com Tue Feb 22 07:41:20 2011 From: sephtin+pm-talk at gmail.com (John Ricker) Date: Tue, 22 Feb 2011 10:41:20 -0500 Subject: [Raleigh-talk] Issues after converting bash to Perl - way to call a config file that contains if/then logic In-Reply-To: <4D63CF75.6030006@plusthree.com> References: <4D63CF75.6030006@plusthree.com> Message-ID: Trevor said > 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're hash of all possible options is not super big (megabytes) I'd just go that route and not worry about the memory. You may be optimizing prematurely. Michael said > How many config variables are we talking about? If it's less than a thousand I doubt you'll event notice the memory usage. I totally acknowledge that it's probably not a good idea, but it would simplify things for me a bit.. Hash would as well (copy pasting, instead of editing a ton of different files).. The variables are all 8digit hex color codes, there are about 26 vars that I'm expecting to grab from the config file (in hash scenario, would be 26 members), and currently 19 different possibilities, will be 50 by the end of the month, and I expect it to grow as themes/ROMs/probably devices are added later. This whole thing takes place on a web server (including zipping, running binaries to zipalig, and decompile some components), where I KNOW I have a very limited amount of memory (256 with 384 burst, iirc), not to mention there could be 50 people trying to run this at the same time.. :P (Maybe not now.. but in theory). >From the numbers, the variables are pretty insignificant... ;P Thanks for the example code Mike, from it I did find what I was doing wrong last night... and got require working... And Michael for the article... In my case, this will sit on a webserver and users will never touch the config file... It's for me. The script is for android theming... Currently, I have working a 'require', with 2 files. one holds info on the ROM (the stuff on the phone), and one holds info on the theme that the user selected to apply. The vars in each are compared, and if they don't match up, then the file that holds that text is modified and marked for inclusion (that's essentially how it works, anyway). I'd have to re-work a good bit of the script if I change over to hash, so I'll add it to my todo list, but it works well now. thanks again! -John -------------- next part -------------- An HTML attachment was scrubbed... URL: From trevormg19 at gmail.com Tue Feb 22 07:47:07 2011 From: trevormg19 at gmail.com (Trevor Little) Date: Tue, 22 Feb 2011 10:47:07 -0500 Subject: [Raleigh-talk] Issues after converting bash to Perl - way to call a config file that contains if/then logic In-Reply-To: References: <4D63CF75.6030006@plusthree.com> Message-ID: Glad you got it working. Thanks for posting to the list. Good to see some chatter =) Trevor On Tue, Feb 22, 2011 at 10:41 AM, John Ricker wrote: > Trevor said > 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're hash of > all possible options is not super big (megabytes) I'd just go that route and > not worry about the memory. You may be optimizing prematurely. > Michael said > How many config variables are we talking about? If it's > less than a thousand I doubt you'll event notice the memory usage. > > I totally acknowledge that it's probably not a good idea, but it would > simplify things for me a bit.. Hash would as well (copy pasting, instead of > editing a ton of different files).. > The variables are all 8digit hex color codes, there are about 26 vars that > I'm expecting to grab from the config file (in hash scenario, would be 26 > members), and currently 19 different possibilities, will be 50 by the end of > the month, and I expect it to grow as themes/ROMs/probably devices are added > later. > > This whole thing takes place on a web server (including zipping, running > binaries to zipalig, and decompile some components), where I KNOW I have a > very limited amount of memory (256 with 384 burst, iirc), not to mention > there could be 50 people trying to run this at the same time.. :P (Maybe > not now.. but in theory). > From the numbers, the variables are pretty insignificant... ;P > > Thanks for the example code Mike, from it I did find what I was doing wrong > last night... and got require working... > And Michael for the article... In my case, this will sit on a webserver and > users will never touch the config file... It's for me. > > The script is for android theming... > Currently, I have working a 'require', with 2 files. one holds info on the > ROM (the stuff on the phone), and one holds info on the theme that the user > selected to apply. The vars in each are compared, and if they don't match > up, then the file that holds that text is modified and marked for inclusion > (that's essentially how it works, anyway). > > I'd have to re-work a good bit of the script if I change over to hash, so > I'll add it to my todo list, but it works well now. thanks again! > > -John > > _______________________________________________ > 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: From msouth at gmail.com Tue Feb 22 08:00:59 2011 From: msouth at gmail.com (Mike South) Date: Tue, 22 Feb 2011 10:00:59 -0600 Subject: [Raleigh-talk] Issues after converting bash to Perl - way to call a config file that contains if/then logic In-Reply-To: References: <4D63CF75.6030006@plusthree.com> Message-ID: On Tue, Feb 22, 2011 at 9:47 AM, Trevor Little wrote: > Glad you got it working. Thanks for posting to the list. Good to see some > chatter =) Wait, you want chatter? You should have said something! That I can definitely help with. mike > > Trevor > > On Tue, Feb 22, 2011 at 10:41 AM, John Ricker wrote: > >> Trevor said > 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're hash of >> all possible options is not super big (megabytes) I'd just go that route and >> not worry about the memory. You may be optimizing prematurely. >> Michael said > How many config variables are we talking about? If it's >> less than a thousand I doubt you'll event notice the memory usage. >> >> I totally acknowledge that it's probably not a good idea, but it would >> simplify things for me a bit.. Hash would as well (copy pasting, instead of >> editing a ton of different files).. >> The variables are all 8digit hex color codes, there are about 26 vars that >> I'm expecting to grab from the config file (in hash scenario, would be 26 >> members), and currently 19 different possibilities, will be 50 by the end of >> the month, and I expect it to grow as themes/ROMs/probably devices are added >> later. >> >> This whole thing takes place on a web server (including zipping, running >> binaries to zipalig, and decompile some components), where I KNOW I have a >> very limited amount of memory (256 with 384 burst, iirc), not to mention >> there could be 50 people trying to run this at the same time.. :P (Maybe >> not now.. but in theory). >> From the numbers, the variables are pretty insignificant... ;P >> >> Thanks for the example code Mike, from it I did find what I was doing >> wrong last night... and got require working... >> And Michael for the article... In my case, this will sit on a webserver >> and users will never touch the config file... It's for me. >> >> The script is for android theming... >> Currently, I have working a 'require', with 2 files. one holds info on >> the ROM (the stuff on the phone), and one holds info on the theme that the >> user selected to apply. The vars in each are compared, and if they don't >> match up, then the file that holds that text is modified and marked for >> inclusion (that's essentially how it works, anyway). >> >> I'd have to re-work a good bit of the script if I change over to hash, so >> I'll add it to my todo list, but it works well now. thanks again! >> >> -John >> >> _______________________________________________ >> Raleigh-talk mailing list >> Raleigh-talk at pm.org >> http://mail.pm.org/mailman/listinfo/raleigh-talk >> >> > > _______________________________________________ > 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: From trevormg19 at gmail.com Tue Feb 22 08:02:54 2011 From: trevormg19 at gmail.com (Trevor Little) Date: Tue, 22 Feb 2011 11:02:54 -0500 Subject: [Raleigh-talk] Issues after converting bash to Perl - way to call a config file that contains if/then logic In-Reply-To: References: <4D63CF75.6030006@plusthree.com> Message-ID: "chatter" ne "mindless chatter" On Tue, Feb 22, 2011 at 11:00 AM, Mike South wrote: > > > On Tue, Feb 22, 2011 at 9:47 AM, Trevor Little wrote: > >> Glad you got it working. Thanks for posting to the list. Good to see some >> chatter =) > > > Wait, you want chatter? You should have said something! That I can > definitely help with. > > mike > > >> >> Trevor >> >> On Tue, Feb 22, 2011 at 10:41 AM, John Ricker wrote: >> >>> Trevor said > 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're hash of >>> all possible options is not super big (megabytes) I'd just go that route and >>> not worry about the memory. You may be optimizing prematurely. >>> Michael said > How many config variables are we talking about? If it's >>> less than a thousand I doubt you'll event notice the memory usage. >>> >>> I totally acknowledge that it's probably not a good idea, but it would >>> simplify things for me a bit.. Hash would as well (copy pasting, instead of >>> editing a ton of different files).. >>> The variables are all 8digit hex color codes, there are about 26 vars >>> that I'm expecting to grab from the config file (in hash scenario, would be >>> 26 members), and currently 19 different possibilities, will be 50 by the end >>> of the month, and I expect it to grow as themes/ROMs/probably devices are >>> added later. >>> >>> This whole thing takes place on a web server (including zipping, running >>> binaries to zipalig, and decompile some components), where I KNOW I have a >>> very limited amount of memory (256 with 384 burst, iirc), not to mention >>> there could be 50 people trying to run this at the same time.. :P (Maybe >>> not now.. but in theory). >>> From the numbers, the variables are pretty insignificant... ;P >>> >>> Thanks for the example code Mike, from it I did find what I was doing >>> wrong last night... and got require working... >>> And Michael for the article... In my case, this will sit on a webserver >>> and users will never touch the config file... It's for me. >>> >>> The script is for android theming... >>> Currently, I have working a 'require', with 2 files. one holds info on >>> the ROM (the stuff on the phone), and one holds info on the theme that the >>> user selected to apply. The vars in each are compared, and if they don't >>> match up, then the file that holds that text is modified and marked for >>> inclusion (that's essentially how it works, anyway). >>> >>> I'd have to re-work a good bit of the script if I change over to hash, so >>> I'll add it to my todo list, but it works well now. thanks again! >>> >>> -John >>> >>> _______________________________________________ >>> Raleigh-talk mailing list >>> Raleigh-talk at pm.org >>> http://mail.pm.org/mailman/listinfo/raleigh-talk >>> >>> >> >> _______________________________________________ >> Raleigh-talk mailing list >> Raleigh-talk at pm.org >> http://mail.pm.org/mailman/listinfo/raleigh-talk >> >> > > _______________________________________________ > 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: From msouth at gmail.com Tue Feb 22 08:05:13 2011 From: msouth at gmail.com (Mike South) Date: Tue, 22 Feb 2011 10:05:13 -0600 Subject: [Raleigh-talk] Issues after converting bash to Perl - way to call a config file that contains if/then logic In-Reply-To: References: <4D63CF75.6030006@plusthree.com> Message-ID: On Tue, Feb 22, 2011 at 10:02 AM, Trevor Little wrote: > "chatter" ne "mindless chatter" You really don't need the double quotes if you're not interpolating anything... mike > > > On Tue, Feb 22, 2011 at 11:00 AM, Mike South wrote: > >> >> >> On Tue, Feb 22, 2011 at 9:47 AM, Trevor Little wrote: >> >>> Glad you got it working. Thanks for posting to the list. Good to see some >>> chatter =) >> >> >> Wait, you want chatter? You should have said something! That I can >> definitely help with. >> >> mike >> >> >>> >>> Trevor >>> >>> On Tue, Feb 22, 2011 at 10:41 AM, John Ricker < >>> sephtin+pm-talk at gmail.com> wrote: >>> >>>> Trevor said > 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're hash >>>> of all possible options is not super big (megabytes) I'd just go that route >>>> and not worry about the memory. You may be optimizing prematurely. >>>> Michael said > How many config variables are we talking about? If it's >>>> less than a thousand I doubt you'll event notice the memory usage. >>>> >>>> I totally acknowledge that it's probably not a good idea, but it would >>>> simplify things for me a bit.. Hash would as well (copy pasting, instead of >>>> editing a ton of different files).. >>>> The variables are all 8digit hex color codes, there are about 26 vars >>>> that I'm expecting to grab from the config file (in hash scenario, would be >>>> 26 members), and currently 19 different possibilities, will be 50 by the end >>>> of the month, and I expect it to grow as themes/ROMs/probably devices are >>>> added later. >>>> >>>> This whole thing takes place on a web server (including zipping, running >>>> binaries to zipalig, and decompile some components), where I KNOW I have a >>>> very limited amount of memory (256 with 384 burst, iirc), not to mention >>>> there could be 50 people trying to run this at the same time.. :P (Maybe >>>> not now.. but in theory). >>>> From the numbers, the variables are pretty insignificant... ;P >>>> >>>> Thanks for the example code Mike, from it I did find what I was doing >>>> wrong last night... and got require working... >>>> And Michael for the article... In my case, this will sit on a webserver >>>> and users will never touch the config file... It's for me. >>>> >>>> The script is for android theming... >>>> Currently, I have working a 'require', with 2 files. one holds info on >>>> the ROM (the stuff on the phone), and one holds info on the theme that the >>>> user selected to apply. The vars in each are compared, and if they don't >>>> match up, then the file that holds that text is modified and marked for >>>> inclusion (that's essentially how it works, anyway). >>>> >>>> I'd have to re-work a good bit of the script if I change over to hash, >>>> so I'll add it to my todo list, but it works well now. thanks again! >>>> >>>> -John >>>> >>>> _______________________________________________ >>>> Raleigh-talk mailing list >>>> Raleigh-talk at pm.org >>>> http://mail.pm.org/mailman/listinfo/raleigh-talk >>>> >>>> >>> >>> _______________________________________________ >>> Raleigh-talk mailing list >>> Raleigh-talk at pm.org >>> http://mail.pm.org/mailman/listinfo/raleigh-talk >>> >>> >> >> _______________________________________________ >> Raleigh-talk mailing list >> Raleigh-talk at pm.org >> http://mail.pm.org/mailman/listinfo/raleigh-talk >> >> > > _______________________________________________ > 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: From trevormg19 at gmail.com Tue Feb 22 09:56:54 2011 From: trevormg19 at gmail.com (Trevor Little) Date: Tue, 22 Feb 2011 12:56:54 -0500 Subject: [Raleigh-talk] Issues after converting bash to Perl - way to call a config file that contains if/then logic In-Reply-To: References: <4D63CF75.6030006@plusthree.com> Message-ID: Ha. I used single quotes in an essay I was writing the other day and Anna complained about them when she was proofreading. I realized that I had used them for exactly that reason. Trevor On Tue, Feb 22, 2011 at 11:05 AM, Mike South wrote: > > > On Tue, Feb 22, 2011 at 10:02 AM, Trevor Little wrote: > >> "chatter" ne "mindless chatter" > > > You really don't need the double quotes if you're not interpolating > anything... > > mike > > >> >> >> On Tue, Feb 22, 2011 at 11:00 AM, Mike South wrote: >> >>> >>> >>> On Tue, Feb 22, 2011 at 9:47 AM, Trevor Little wrote: >>> >>>> Glad you got it working. Thanks for posting to the list. Good to see >>>> some chatter =) >>> >>> >>> Wait, you want chatter? You should have said something! That I can >>> definitely help with. >>> >>> mike >>> >>> >>>> >>>> Trevor >>>> >>>> On Tue, Feb 22, 2011 at 10:41 AM, John Ricker < >>>> sephtin+pm-talk at gmail.com> wrote: >>>> >>>>> Trevor said > 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're hash >>>>> of all possible options is not super big (megabytes) I'd just go that route >>>>> and not worry about the memory. You may be optimizing prematurely. >>>>> Michael said > How many config variables are we talking about? If it's >>>>> less than a thousand I doubt you'll event notice the memory usage. >>>>> >>>>> I totally acknowledge that it's probably not a good idea, but it would >>>>> simplify things for me a bit.. Hash would as well (copy pasting, instead of >>>>> editing a ton of different files).. >>>>> The variables are all 8digit hex color codes, there are about 26 vars >>>>> that I'm expecting to grab from the config file (in hash scenario, would be >>>>> 26 members), and currently 19 different possibilities, will be 50 by the end >>>>> of the month, and I expect it to grow as themes/ROMs/probably devices are >>>>> added later. >>>>> >>>>> This whole thing takes place on a web server (including zipping, >>>>> running binaries to zipalig, and decompile some components), where I KNOW I >>>>> have a very limited amount of memory (256 with 384 burst, iirc), not to >>>>> mention there could be 50 people trying to run this at the same time.. :P >>>>> (Maybe not now.. but in theory). >>>>> From the numbers, the variables are pretty insignificant... ;P >>>>> >>>>> Thanks for the example code Mike, from it I did find what I was doing >>>>> wrong last night... and got require working... >>>>> And Michael for the article... In my case, this will sit on a webserver >>>>> and users will never touch the config file... It's for me. >>>>> >>>>> The script is for android theming... >>>>> Currently, I have working a 'require', with 2 files. one holds info on >>>>> the ROM (the stuff on the phone), and one holds info on the theme that the >>>>> user selected to apply. The vars in each are compared, and if they don't >>>>> match up, then the file that holds that text is modified and marked for >>>>> inclusion (that's essentially how it works, anyway). >>>>> >>>>> I'd have to re-work a good bit of the script if I change over to hash, >>>>> so I'll add it to my todo list, but it works well now. thanks again! >>>>> >>>>> -John >>>>> >>>>> _______________________________________________ >>>>> Raleigh-talk mailing list >>>>> Raleigh-talk at pm.org >>>>> http://mail.pm.org/mailman/listinfo/raleigh-talk >>>>> >>>>> >>>> >>>> _______________________________________________ >>>> Raleigh-talk mailing list >>>> Raleigh-talk at pm.org >>>> http://mail.pm.org/mailman/listinfo/raleigh-talk >>>> >>>> >>> >>> _______________________________________________ >>> Raleigh-talk mailing list >>> Raleigh-talk at pm.org >>> http://mail.pm.org/mailman/listinfo/raleigh-talk >>> >>> >> >> _______________________________________________ >> Raleigh-talk mailing list >> Raleigh-talk at pm.org >> http://mail.pm.org/mailman/listinfo/raleigh-talk >> >> > > _______________________________________________ > 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: From sephtin+pm-talk at gmail.com Tue Feb 22 17:31:55 2011 From: sephtin+pm-talk at gmail.com (John Ricker) Date: Tue, 22 Feb 2011 20:31:55 -0500 Subject: [Raleigh-talk] [Perl help] - Multiple substitutions in a file Message-ID: Received so much help with the last question (which seems to be functioning great, btw.. thanks all)... thought I'd try again. ;) Have a script that was recently migrated to Perl from shell/bash, and am wondering if there's an easy way in Perl to do the following- File(s) being modified are hex, and look something like: ---x--- ... 08 00 00 00 FF FF FF FF FF FF FF FF 1B 00 00 00 02 01 10 00 28 01 00 00 09 00 00 00 FF FF FF FF FF FF FF FF 1C 00 00 00 14 00 14 00 0D 00 00 00 00 00 0D 00 18 00 00 00 11 00 00 00 FF FF FF FF 08 00 00 05 02 0E 00 00 18 00 00 00 0E 00 00 00 FF FF FF FF 08 00 00 1C 00 00 00 FF 18 00 00 00 09 00 00 00 FF FF FF FF 08 00 00 11 10 00 00 00 18 00 00 00 0F 00 00 00 FF FF FF FF 08 00 00 01 11 02 02 01 18 00 00 00 03 00 00 00 FF FF FF FF 08 00 00 01 05 00 08 01 18 00 00 00 10 00 00 00 FF FF FF FF 08 00 00 05 01 0F 00 00 18 00 00 00 05 00 00 00 FF FF FF FF 08 00 00 05 01 0F 00 00 18 00 00 ... ---x--- File(s) contain several occurrences of "08 00 00 1c ## ## ## ##" or more appropriately for this list: "08\s00\s00\s1C\s\d\d\s\d\d\s\d\d\s\d\d" Now on to the good stuff... I would like to replace the ## ## ## ## with my chosen colors, that are being provided by vars... Example: colorChange1="FF 00 00 FF" colorChange2="FF FF 00 FF" colorChange3="FF 00 FF FF" ... I'm wondering how it might be possible, to replace the FIRST occurrence (in the file, NOT in a line) of "08 00 00 1C ## ## ## ##" with "08 00 00 1C $colorChange1", the second with "08 00 00 1C $colorChange2", etc. More info: --Script modifies files to theme them, in this case, I'm taking a BINARY (compiled XML) file, converting it to HEX via xxd, and then changing the text color for a theme via substitution. --I originally thought it could be done similar to what sed does... with s|(08\s00\s00\s1C)\s\d\d\s\d\d\s\d\d\s\d\d|$1$colorChange1|1 (note last digit...), but testing has shown that this doesn't work as expected. --Not really related, but in the binary, the colors are actually backwards, so color - FF AB CD EF becomes binary - EF CD AB FF, but that's an easy change. --CURRENTLY in my script, I'm doing this via a sub, passing an array of the colors, the file, and the file location (directory), and pulling the file, making the changes, and saving it back out via loop... BUT, because I couldn't figure out how to just do the multiple replaces... I cheated and made template files that contain "08 00 00 1C 11 11 11 11" and "08 00 00 1C 22 22 22 22", so when I iterate through the loop, I just change like so: for ( $i = 1 ; $i <= $COUNT ; $i++ ) { ... $line =~ s|08\s00\s00\s1c\s$i$i\s$i$i\s$i$i\s$i$i\s|08 00 00 1c $array_ref[$i]|; ... } Code works, but I'd much rather be able to pull native files straight out of the .zip, and change them that way... :P Anyway, again, if there are specifics I missed, happy to provide them. I keep thinking there should be an easy way to do this... but my google-fu is failing me.. Thanks again for the help with the previous problem, and in advance for any assistance on this one. :) At the very least, I guess I can provide some chatter to the group. -John (sephtin @gmail) -------------- next part -------------- An HTML attachment was scrubbed... URL: From mattnashbrowns at gmail.com Tue Feb 22 18:56:54 2011 From: mattnashbrowns at gmail.com (Matt Nash) Date: Tue, 22 Feb 2011 21:56:54 -0500 Subject: [Raleigh-talk] [Perl help] - Multiple substitutions in a file In-Reply-To: References: Message-ID: Hi John, You are reading the entire file into an array of lines, then processing that array using a C-style for loop and interpolating the index var into the search string... and that is the LEAST crazy thing. :) It works, of course, because it turns out that there are infinitely many ways to do it. For your immediate problem of the search-and-replace, I recommend reading the whole file into a single string, newlines and all, then using the /gc modifiers in the regex. Maybe put your colorChange stuff into an array that you loop through, checking the regex for the nth match: @changes = ($change1, $change2, $change3); foreach $change (@changes) { $wholefile =~ s|08\s00\s00\s1C\s\d\d\s\d\d\s\d\d\s\d\d|08\s00\s00\s1C\s$change|gc; } see http://perldoc.perl.org/perlre.html and http://perldoc.perl.org/perlretut.htm for lots of details, but the upshot of the modifiers is: g makes it keep looking for matches; c tells it to remember where it last matched, and start from there on the next match. You don't have to care what n is, because you are matching your search string exactly as many times as you have changes to make. ...but this project sounds ripe for refactoring, if I may be so bold. Could it be that what you really need is a templating system? Thanks for bringing some much-needed questions to this list! Matt On Tue, Feb 22, 2011 at 8:31 PM, John Ricker wrote: > Received so much help with the last question (which seems to be functioning > great, btw.. thanks all)... thought I'd try again. ;) > > Have a script that was recently migrated to Perl from shell/bash, and am > wondering if there's an easy way in Perl to do the following- > > File(s) being modified are hex, and look something like: > ---x--- > ... > 08 00 00 00 FF FF FF FF FF FF FF FF 1B 00 00 00 02 01 10 00 28 01 00 00 09 > 00 00 00 FF FF FF FF FF FF FF FF 1C 00 00 00 14 00 14 00 0D 00 00 00 00 00 > 0D 00 18 00 00 00 11 00 00 00 FF FF FF FF 08 00 00 05 02 0E 00 00 18 00 00 > 00 0E 00 00 00 FF FF FF FF 08 00 00 1C 00 00 00 FF 18 00 00 00 09 00 00 00 > FF FF FF FF 08 00 00 11 10 00 00 00 18 00 00 00 0F 00 00 00 FF FF FF FF 08 > 00 00 01 11 02 02 01 18 00 00 00 03 00 00 00 FF FF FF FF 08 00 00 01 05 00 > 08 01 18 00 00 00 10 00 00 00 FF FF FF FF 08 00 00 05 01 0F 00 00 18 00 00 > 00 05 00 00 00 FF FF FF FF 08 00 00 05 01 0F 00 00 18 00 00 > ... > ---x--- > File(s) contain several occurrences of "08 00 00 1c ## ## ## ##" or more > appropriately for this list: "08\s00\s00\s1C\s\d\d\s\d\d\s\d\d\s\d\d" > > Now on to the good stuff... I would like to replace the ## ## ## ## with > my chosen colors, that are being provided by vars... Example: > colorChange1="FF 00 00 FF" > colorChange2="FF FF 00 FF" > colorChange3="FF 00 FF FF" > ... > > I'm wondering how it might be possible, to replace the FIRST occurrence (in > the file, NOT in a line) of "08 00 00 1C ## ## ## ##" with "08 00 00 1C > $colorChange1", the second with "08 00 00 1C $colorChange2", etc. > > More info: > --Script modifies files to theme them, in this case, I'm taking a BINARY > (compiled XML) file, converting it to HEX via xxd, and then changing the > text color for a theme via substitution. > --I originally thought it could be done similar to what sed does... with > s|(08\s00\s00\s1C)\s\d\d\s\d\d\s\d\d\s\d\d|$1$colorChange1|1 (note last > digit...), but testing has shown that this doesn't work as expected. > --Not really related, but in the binary, the colors are actually backwards, > so color - FF AB CD EF becomes binary - EF CD AB FF, but that's an easy > change. > --CURRENTLY in my script, I'm doing this via a sub, passing an array of the > colors, the file, and the file location (directory), and pulling the file, > making the changes, and saving it back out via loop... BUT, because I > couldn't figure out how to just do the multiple replaces... I cheated and > made template files that contain "08 00 00 1C 11 11 11 11" and "08 00 00 1C > 22 22 22 22", so when I iterate through the loop, I just change like so: > for ( $i = 1 ; $i <= $COUNT ; $i++ ) { > ... > $line =~ s|08\s00\s00\s1c\s$i$i\s$i$i\s$i$i\s$i$i\s|08 00 00 1c > $array_ref[$i]|; > ... > } > > Code works, but I'd much rather be able to pull native files straight out > of the .zip, and change them that way... :P > > Anyway, again, if there are specifics I missed, happy to provide them. > I keep thinking there should be an easy way to do this... but my google-fu > is failing me.. > > Thanks again for the help with the previous problem, and in advance for any > assistance on this one. :) > At the very least, I guess I can provide some chatter to the group. > > -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: From mattnashbrowns at gmail.com Tue Feb 22 20:58:46 2011 From: mattnashbrowns at gmail.com (Matt Nash) Date: Tue, 22 Feb 2011 23:58:46 -0500 Subject: [Raleigh-talk] [Perl help] - Multiple substitutions in a file In-Reply-To: References: Message-ID: Of course, you want actual spaces (not '\s') in the replace string, but you knew that. On Tue, Feb 22, 2011 at 9:56 PM, Matt Nash wrote: > Hi John, > > You are reading the entire file into an array of lines, then processing > that array using a C-style for loop and interpolating the index var into the > search string... and that is the LEAST crazy thing. :) It works, of course, > because it turns out that there are infinitely many ways to do it. > > For your immediate problem of the search-and-replace, I recommend reading > the whole file into a single string, newlines and all, then using the /gc > modifiers in the regex. Maybe put your colorChange stuff into an array that > you loop through, checking the regex for the nth match: > > @changes = ($change1, $change2, $change3); > > foreach $change (@changes) { > $wholefile =~ > s|08\s00\s00\s1C\s\d\d\s\d\d\s\d\d\s\d\d|08\s00\s00\s1C\s$change|gc; > } > > see http://perldoc.perl.org/perlre.html and > http://perldoc.perl.org/perlretut.htm for > lots of details, but the upshot of the modifiers is: g makes it keep looking > for matches; c tells it to remember where it last matched, and start from > there on the next match. You don't have to care what n is, because you are > matching your search string exactly as many times as you have changes to > make. > > ...but this project sounds ripe for refactoring, if I may be so bold. > Could it be that what you really need is a templating system? > > Thanks for bringing some much-needed questions to this list! > > Matt > > On Tue, Feb 22, 2011 at 8:31 PM, John Ricker wrote: > >> Received so much help with the last question (which seems to be >> functioning great, btw.. thanks all)... thought I'd try again. ;) >> >> Have a script that was recently migrated to Perl from shell/bash, and am >> wondering if there's an easy way in Perl to do the following- >> >> File(s) being modified are hex, and look something like: >> ---x--- >> ... >> 08 00 00 00 FF FF FF FF FF FF FF FF 1B 00 00 00 02 01 10 00 28 01 00 00 09 >> 00 00 00 FF FF FF FF FF FF FF FF 1C 00 00 00 14 00 14 00 0D 00 00 00 00 00 >> 0D 00 18 00 00 00 11 00 00 00 FF FF FF FF 08 00 00 05 02 0E 00 00 18 00 00 >> 00 0E 00 00 00 FF FF FF FF 08 00 00 1C 00 00 00 FF 18 00 00 00 09 00 00 00 >> FF FF FF FF 08 00 00 11 10 00 00 00 18 00 00 00 0F 00 00 00 FF FF FF FF 08 >> 00 00 01 11 02 02 01 18 00 00 00 03 00 00 00 FF FF FF FF 08 00 00 01 05 00 >> 08 01 18 00 00 00 10 00 00 00 FF FF FF FF 08 00 00 05 01 0F 00 00 18 00 00 >> 00 05 00 00 00 FF FF FF FF 08 00 00 05 01 0F 00 00 18 00 00 >> ... >> ---x--- >> File(s) contain several occurrences of "08 00 00 1c ## ## ## ##" or more >> appropriately for this list: "08\s00\s00\s1C\s\d\d\s\d\d\s\d\d\s\d\d" >> >> Now on to the good stuff... I would like to replace the ## ## ## ## with >> my chosen colors, that are being provided by vars... Example: >> colorChange1="FF 00 00 FF" >> colorChange2="FF FF 00 FF" >> colorChange3="FF 00 FF FF" >> ... >> >> I'm wondering how it might be possible, to replace the FIRST occurrence >> (in the file, NOT in a line) of "08 00 00 1C ## ## ## ##" with "08 00 00 1C >> $colorChange1", the second with "08 00 00 1C $colorChange2", etc. >> >> More info: >> --Script modifies files to theme them, in this case, I'm taking a BINARY >> (compiled XML) file, converting it to HEX via xxd, and then changing the >> text color for a theme via substitution. >> --I originally thought it could be done similar to what sed does... with >> s|(08\s00\s00\s1C)\s\d\d\s\d\d\s\d\d\s\d\d|$1$colorChange1|1 (note last >> digit...), but testing has shown that this doesn't work as expected. >> --Not really related, but in the binary, the colors are actually >> backwards, so color - FF AB CD EF becomes binary - EF CD AB FF, but that's >> an easy change. >> --CURRENTLY in my script, I'm doing this via a sub, passing an array of >> the colors, the file, and the file location (directory), and pulling the >> file, making the changes, and saving it back out via loop... BUT, because I >> couldn't figure out how to just do the multiple replaces... I cheated and >> made template files that contain "08 00 00 1C 11 11 11 11" and "08 00 00 1C >> 22 22 22 22", so when I iterate through the loop, I just change like so: >> for ( $i = 1 ; $i <= $COUNT ; $i++ ) { >> ... >> $line =~ s|08\s00\s00\s1c\s$i$i\s$i$i\s$i$i\s$i$i\s|08 00 00 1c >> $array_ref[$i]|; >> ... >> } >> >> Code works, but I'd much rather be able to pull native files straight out >> of the .zip, and change them that way... :P >> >> Anyway, again, if there are specifics I missed, happy to provide them. >> I keep thinking there should be an easy way to do this... but my google-fu >> is failing me.. >> >> Thanks again for the help with the previous problem, and in advance for >> any assistance on this one. :) >> At the very least, I guess I can provide some chatter to the group. >> >> -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: From msouth at gmail.com Tue Feb 22 21:49:13 2011 From: msouth at gmail.com (Mike South) Date: Tue, 22 Feb 2011 23:49:13 -0600 Subject: [Raleigh-talk] [Perl help] - Multiple substitutions in a file In-Reply-To: References: Message-ID: Also those \d's aren't going to match A-F are they? Are you familiar with qr// to let you store a regex bit in a variable? That might make some of this more readable. mike On Tue, Feb 22, 2011 at 10:58 PM, Matt Nash wrote: > Of course, you want actual spaces (not '\s') in the replace string, but you > knew that. > > > On Tue, Feb 22, 2011 at 9:56 PM, Matt Nash wrote: > >> Hi John, >> >> You are reading the entire file into an array of lines, then processing >> that array using a C-style for loop and interpolating the index var into the >> search string... and that is the LEAST crazy thing. :) It works, of course, >> because it turns out that there are infinitely many ways to do it. >> >> For your immediate problem of the search-and-replace, I recommend reading >> the whole file into a single string, newlines and all, then using the /gc >> modifiers in the regex. Maybe put your colorChange stuff into an array that >> you loop through, checking the regex for the nth match: >> >> @changes = ($change1, $change2, $change3); >> >> foreach $change (@changes) { >> $wholefile =~ >> s|08\s00\s00\s1C\s\d\d\s\d\d\s\d\d\s\d\d|08\s00\s00\s1C\s$change|gc; >> } >> >> see http://perldoc.perl.org/perlre.html and >> http://perldoc.perl.org/perlretut.htm for >> lots of details, but the upshot of the modifiers is: g makes it keep looking >> for matches; c tells it to remember where it last matched, and start from >> there on the next match. You don't have to care what n is, because you are >> matching your search string exactly as many times as you have changes to >> make. >> >> ...but this project sounds ripe for refactoring, if I may be so bold. >> Could it be that what you really need is a templating system? >> >> Thanks for bringing some much-needed questions to this list! >> >> Matt >> >> On Tue, Feb 22, 2011 at 8:31 PM, John Ricker wrote: >> >>> Received so much help with the last question (which seems to be >>> functioning great, btw.. thanks all)... thought I'd try again. ;) >>> >>> Have a script that was recently migrated to Perl from shell/bash, and am >>> wondering if there's an easy way in Perl to do the following- >>> >>> File(s) being modified are hex, and look something like: >>> ---x--- >>> ... >>> 08 00 00 00 FF FF FF FF FF FF FF FF 1B 00 00 00 02 01 10 00 28 01 00 00 >>> 09 00 00 00 FF FF FF FF FF FF FF FF 1C 00 00 00 14 00 14 00 0D 00 00 00 00 >>> 00 0D 00 18 00 00 00 11 00 00 00 FF FF FF FF 08 00 00 05 02 0E 00 00 18 00 >>> 00 00 0E 00 00 00 FF FF FF FF 08 00 00 1C 00 00 00 FF 18 00 00 00 09 00 00 >>> 00 FF FF FF FF 08 00 00 11 10 00 00 00 18 00 00 00 0F 00 00 00 FF FF FF FF >>> 08 00 00 01 11 02 02 01 18 00 00 00 03 00 00 00 FF FF FF FF 08 00 00 01 05 >>> 00 08 01 18 00 00 00 10 00 00 00 FF FF FF FF 08 00 00 05 01 0F 00 00 18 00 >>> 00 00 05 00 00 00 FF FF FF FF 08 00 00 05 01 0F 00 00 18 00 00 >>> ... >>> ---x--- >>> File(s) contain several occurrences of "08 00 00 1c ## ## ## ##" or more >>> appropriately for this list: "08\s00\s00\s1C\s\d\d\s\d\d\s\d\d\s\d\d" >>> >>> Now on to the good stuff... I would like to replace the ## ## ## ## >>> with my chosen colors, that are being provided by vars... Example: >>> colorChange1="FF 00 00 FF" >>> colorChange2="FF FF 00 FF" >>> colorChange3="FF 00 FF FF" >>> ... >>> >>> I'm wondering how it might be possible, to replace the FIRST occurrence >>> (in the file, NOT in a line) of "08 00 00 1C ## ## ## ##" with "08 00 00 1C >>> $colorChange1", the second with "08 00 00 1C $colorChange2", etc. >>> >>> More info: >>> --Script modifies files to theme them, in this case, I'm taking a BINARY >>> (compiled XML) file, converting it to HEX via xxd, and then changing the >>> text color for a theme via substitution. >>> --I originally thought it could be done similar to what sed does... with >>> s|(08\s00\s00\s1C)\s\d\d\s\d\d\s\d\d\s\d\d|$1$colorChange1|1 (note last >>> digit...), but testing has shown that this doesn't work as expected. >>> --Not really related, but in the binary, the colors are actually >>> backwards, so color - FF AB CD EF becomes binary - EF CD AB FF, but that's >>> an easy change. >>> --CURRENTLY in my script, I'm doing this via a sub, passing an array of >>> the colors, the file, and the file location (directory), and pulling the >>> file, making the changes, and saving it back out via loop... BUT, because I >>> couldn't figure out how to just do the multiple replaces... I cheated and >>> made template files that contain "08 00 00 1C 11 11 11 11" and "08 00 00 1C >>> 22 22 22 22", so when I iterate through the loop, I just change like so: >>> for ( $i = 1 ; $i <= $COUNT ; $i++ ) { >>> ... >>> $line =~ s|08\s00\s00\s1c\s$i$i\s$i$i\s$i$i\s$i$i\s|08 00 00 1c >>> $array_ref[$i]|; >>> ... >>> } >>> >>> Code works, but I'd much rather be able to pull native files straight out >>> of the .zip, and change them that way... :P >>> >>> Anyway, again, if there are specifics I missed, happy to provide them. >>> I keep thinking there should be an easy way to do this... but my >>> google-fu is failing me.. >>> >>> Thanks again for the help with the previous problem, and in advance for >>> any assistance on this one. :) >>> At the very least, I guess I can provide some chatter to the group. >>> >>> -John (sephtin @gmail) >>> >>> _______________________________________________ >>> Raleigh-talk mailing list >>> Raleigh-talk at pm.org >>> http://mail.pm.org/mailman/listinfo/raleigh-talk >>> >>> >> > > _______________________________________________ > 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: From sephtin+pm-talk at gmail.com Tue Feb 22 22:45:01 2011 From: sephtin+pm-talk at gmail.com (John Ricker) Date: Wed, 23 Feb 2011 01:45:01 -0500 Subject: [Raleigh-talk] [Perl help] - Multiple substitutions in a file In-Reply-To: References: Message-ID: Yea, that's what I get for trying to be creative when I try to simplify an example down for an email, currently using "..", not "\d\d" I've run across qr// before, not quite sure exactly what use I might have for it, but I'll take a look... @Matt, c modifyer looks like what I need.... I'm already using arrays and a loop, will check it out.. In other news, for a completely different problem in the script, I recently came across File::Slurp... I could probably make more use of it in this case. (It let me reduce some other code from 21 lines to 5, and I assume the overhead isn't bad, as I've read it's being included natively with Perl 6...) *shrug* -John On Feb 23, 2011 12:49 AM, "Mike South" wrote: > Also those \d's aren't going to match A-F are they? > > Are you familiar with qr// to let you store a regex bit in a variable? That > might make some of this more readable. > > mike > > On Tue, Feb 22, 2011 at 10:58 PM, Matt Nash wrote: > >> Of course, you want actual spaces (not '\s') in the replace string, but you >> knew that. >> >> >> On Tue, Feb 22, 2011 at 9:56 PM, Matt Nash wrote: >> >>> Hi John, >>> >>> You are reading the entire file into an array of lines, then processing >>> that array using a C-style for loop and interpolating the index var into the >>> search string... and that is the LEAST crazy thing. :) It works, of course, >>> because it turns out that there are infinitely many ways to do it. >>> >>> For your immediate problem of the search-and-replace, I recommend reading >>> the whole file into a single string, newlines and all, then using the /gc >>> modifiers in the regex. Maybe put your colorChange stuff into an array that >>> you loop through, checking the regex for the nth match: >>> >>> @changes = ($change1, $change2, $change3); >>> >>> foreach $change (@changes) { >>> $wholefile =~ >>> s|08\s00\s00\s1C\s\d\d\s\d\d\s\d\d\s\d\d|08\s00\s00\s1C\s$change|gc; >>> } >>> >>> see http://perldoc.perl.org/perlre.html and >>> http://perldoc.perl.org/perlretut.htm< http://perldoc.perl.org/perlretut.html#Using-regular-expressions-in-Perl> for >>> lots of details, but the upshot of the modifiers is: g makes it keep looking >>> for matches; c tells it to remember where it last matched, and start from >>> there on the next match. You don't have to care what n is, because you are >>> matching your search string exactly as many times as you have changes to >>> make. >>> >>> ...but this project sounds ripe for refactoring, if I may be so bold. >>> Could it be that what you really need is a templating system? >>> >>> Thanks for bringing some much-needed questions to this list! >>> >>> Matt >>> >>> On Tue, Feb 22, 2011 at 8:31 PM, John Ricker wrote: >>> >>>> Received so much help with the last question (which seems to be >>>> functioning great, btw.. thanks all)... thought I'd try again. ;) >>>> >>>> Have a script that was recently migrated to Perl from shell/bash, and am >>>> wondering if there's an easy way in Perl to do the following- >>>> >>>> File(s) being modified are hex, and look something like: >>>> ---x--- >>>> ... >>>> 08 00 00 00 FF FF FF FF FF FF FF FF 1B 00 00 00 02 01 10 00 28 01 00 00 >>>> 09 00 00 00 FF FF FF FF FF FF FF FF 1C 00 00 00 14 00 14 00 0D 00 00 00 00 >>>> 00 0D 00 18 00 00 00 11 00 00 00 FF FF FF FF 08 00 00 05 02 0E 00 00 18 00 >>>> 00 00 0E 00 00 00 FF FF FF FF 08 00 00 1C 00 00 00 FF 18 00 00 00 09 00 00 >>>> 00 FF FF FF FF 08 00 00 11 10 00 00 00 18 00 00 00 0F 00 00 00 FF FF FF FF >>>> 08 00 00 01 11 02 02 01 18 00 00 00 03 00 00 00 FF FF FF FF 08 00 00 01 05 >>>> 00 08 01 18 00 00 00 10 00 00 00 FF FF FF FF 08 00 00 05 01 0F 00 00 18 00 >>>> 00 00 05 00 00 00 FF FF FF FF 08 00 00 05 01 0F 00 00 18 00 00 >>>> ... >>>> ---x--- >>>> File(s) contain several occurrences of "08 00 00 1c ## ## ## ##" or more >>>> appropriately for this list: "08\s00\s00\s1C\s\d\d\s\d\d\s\d\d\s\d\d" >>>> >>>> Now on to the good stuff... I would like to replace the ## ## ## ## >>>> with my chosen colors, that are being provided by vars... Example: >>>> colorChange1="FF 00 00 FF" >>>> colorChange2="FF FF 00 FF" >>>> colorChange3="FF 00 FF FF" >>>> ... >>>> >>>> I'm wondering how it might be possible, to replace the FIRST occurrence >>>> (in the file, NOT in a line) of "08 00 00 1C ## ## ## ##" with "08 00 00 1C >>>> $colorChange1", the second with "08 00 00 1C $colorChange2", etc. >>>> >>>> More info: >>>> --Script modifies files to theme them, in this case, I'm taking a BINARY >>>> (compiled XML) file, converting it to HEX via xxd, and then changing the >>>> text color for a theme via substitution. >>>> --I originally thought it could be done similar to what sed does... with >>>> s|(08\s00\s00\s1C)\s\d\d\s\d\d\s\d\d\s\d\d|$1$colorChange1|1 (note last >>>> digit...), but testing has shown that this doesn't work as expected. >>>> --Not really related, but in the binary, the colors are actually >>>> backwards, so color - FF AB CD EF becomes binary - EF CD AB FF, but that's >>>> an easy change. >>>> --CURRENTLY in my script, I'm doing this via a sub, passing an array of >>>> the colors, the file, and the file location (directory), and pulling the >>>> file, making the changes, and saving it back out via loop... BUT, because I >>>> couldn't figure out how to just do the multiple replaces... I cheated and >>>> made template files that contain "08 00 00 1C 11 11 11 11" and "08 00 00 1C >>>> 22 22 22 22", so when I iterate through the loop, I just change like so: >>>> for ( $i = 1 ; $i <= $COUNT ; $i++ ) { >>>> ... >>>> $line =~ s|08\s00\s00\s1c\s$i$i\s$i$i\s$i$i\s$i$i\s|08 00 00 1c >>>> $array_ref[$i]|; >>>> ... >>>> } >>>> >>>> Code works, but I'd much rather be able to pull native files straight out >>>> of the .zip, and change them that way... :P >>>> >>>> Anyway, again, if there are specifics I missed, happy to provide them. >>>> I keep thinking there should be an easy way to do this... but my >>>> google-fu is failing me.. >>>> >>>> Thanks again for the help with the previous problem, and in advance for >>>> any assistance on this one. :) >>>> At the very least, I guess I can provide some chatter to the group. >>>> >>>> -John (sephtin @gmail) >>>> >>>> _______________________________________________ >>>> Raleigh-talk mailing list >>>> Raleigh-talk at pm.org >>>> http://mail.pm.org/mailman/listinfo/raleigh-talk >>>> >>>> >>> >> >> _______________________________________________ >> 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: From msouth at gmail.com Tue Feb 22 23:05:18 2011 From: msouth at gmail.com (Mike South) Date: Wed, 23 Feb 2011 01:05:18 -0600 Subject: [Raleigh-talk] [Perl help] - Multiple substitutions in a file In-Reply-To: References: Message-ID: If you have the whole file in a string and the replacements in an array and the 'marker' doesn't change, I think the following will work. The /e modifier executes code, so s/foo/ do something here /eg will replace each instance of foo with whatever 'do something here', executed as perl code, returns. So I just made it grab the next color off the list and put that in place. In case you have more of the 'markers' than you have replacements, I just put $1 back in right where we found it. The stuff that's printing out the length of the string is a rough check to make sure I'm not accidentally clobbering part of the string. mike use strict; use warnings; my $file = ' 08 00 00 00 FF FF FF FF FF FF FF FF 1B 00 00 00 02 01 10 00 28 01 00 00 09 00 00 00 FF FF FF FF FF FF FF FF 1C 00 00 00 14 00 14 00 0D 00 00 00 00 00 0D 00 18 00 00 00 11 00 00 00 FF FF FF FF 08 00 00 05 02 0E 00 00 18 00 00 00 0E 00 00 00 FF FF FF FF 08 00 00 1C 00 00 00 FF 18 00 00 00 09 00 00 00 FF FF FF FF 08 00 00 11 10 00 00 00 18 00 00 00 0F 00 00 00 FF FF FF FF 08 00 00 01 11 02 02 01 18 00 00 00 03 00 00 00 08 00 00 1C FF FF FF FF 08 00 00 01 05 00 08 01 18 00 00 00 10 00 00 00 FF FF FF FF 08 00 00 05 01 0F 00 00 18 00 00 00 05 00 08 00 00 1C 00 00 FF FF FF FF 08 00 00 05 01 0F 00 08 00 00 1C 00 18 00 00 '; my $length = length $file; my $color1 = '11#AA#BB#CC'; my $color2 = '22#DD#EE#FF'; my $color3 = '33#XX#XX#XX'; my @replacements = ($color1, $color2, $color3); #my @replacements = ($color1, $color2); my $marker = '08 00 00 1C '; my $xx = qr/[[:xdigit:]]{2}/; # two hex digits $file =~ s/$marker(($xx ){3}$xx)/$marker . (shift(@replacements) || $1)/ge; print $file,$/; print "length was $length, now it's ", length($file), $/; On Tue, Feb 22, 2011 at 11:49 PM, Mike South wrote: > Also those \d's aren't going to match A-F are they? > > Are you familiar with qr// to let you store a regex bit in a variable? > That might make some of this more readable. > > mike > > On Tue, Feb 22, 2011 at 10:58 PM, Matt Nash wrote: > >> Of course, you want actual spaces (not '\s') in the replace string, but >> you knew that. >> >> >> On Tue, Feb 22, 2011 at 9:56 PM, Matt Nash wrote: >> >>> Hi John, >>> >>> You are reading the entire file into an array of lines, then processing >>> that array using a C-style for loop and interpolating the index var into the >>> search string... and that is the LEAST crazy thing. :) It works, of course, >>> because it turns out that there are infinitely many ways to do it. >>> >>> For your immediate problem of the search-and-replace, I recommend reading >>> the whole file into a single string, newlines and all, then using the /gc >>> modifiers in the regex. Maybe put your colorChange stuff into an array that >>> you loop through, checking the regex for the nth match: >>> >>> @changes = ($change1, $change2, $change3); >>> >>> foreach $change (@changes) { >>> $wholefile =~ >>> s|08\s00\s00\s1C\s\d\d\s\d\d\s\d\d\s\d\d|08\s00\s00\s1C\s$change|gc; >>> } >>> >>> see http://perldoc.perl.org/perlre.html and >>> http://perldoc.perl.org/perlretut.htm for >>> lots of details, but the upshot of the modifiers is: g makes it keep looking >>> for matches; c tells it to remember where it last matched, and start from >>> there on the next match. You don't have to care what n is, because you are >>> matching your search string exactly as many times as you have changes to >>> make. >>> >>> ...but this project sounds ripe for refactoring, if I may be so bold. >>> Could it be that what you really need is a templating system? >>> >>> Thanks for bringing some much-needed questions to this list! >>> >>> Matt >>> >>> On Tue, Feb 22, 2011 at 8:31 PM, John Ricker wrote: >>> >>>> Received so much help with the last question (which seems to be >>>> functioning great, btw.. thanks all)... thought I'd try again. ;) >>>> >>>> Have a script that was recently migrated to Perl from shell/bash, and am >>>> wondering if there's an easy way in Perl to do the following- >>>> >>>> File(s) being modified are hex, and look something like: >>>> ---x--- >>>> ... >>>> 08 00 00 00 FF FF FF FF FF FF FF FF 1B 00 00 00 02 01 10 00 28 01 00 00 >>>> 09 00 00 00 FF FF FF FF FF FF FF FF 1C 00 00 00 14 00 14 00 0D 00 00 00 00 >>>> 00 0D 00 18 00 00 00 11 00 00 00 FF FF FF FF 08 00 00 05 02 0E 00 00 18 00 >>>> 00 00 0E 00 00 00 FF FF FF FF 08 00 00 1C 00 00 00 FF 18 00 00 00 09 00 00 >>>> 00 FF FF FF FF 08 00 00 11 10 00 00 00 18 00 00 00 0F 00 00 00 FF FF FF FF >>>> 08 00 00 01 11 02 02 01 18 00 00 00 03 00 00 00 FF FF FF FF 08 00 00 01 05 >>>> 00 08 01 18 00 00 00 10 00 00 00 FF FF FF FF 08 00 00 05 01 0F 00 00 18 00 >>>> 00 00 05 00 00 00 FF FF FF FF 08 00 00 05 01 0F 00 00 18 00 00 >>>> ... >>>> ---x--- >>>> File(s) contain several occurrences of "08 00 00 1c ## ## ## ##" or more >>>> appropriately for this list: "08\s00\s00\s1C\s\d\d\s\d\d\s\d\d\s\d\d" >>>> >>>> Now on to the good stuff... I would like to replace the ## ## ## ## >>>> with my chosen colors, that are being provided by vars... Example: >>>> colorChange1="FF 00 00 FF" >>>> colorChange2="FF FF 00 FF" >>>> colorChange3="FF 00 FF FF" >>>> ... >>>> >>>> I'm wondering how it might be possible, to replace the FIRST occurrence >>>> (in the file, NOT in a line) of "08 00 00 1C ## ## ## ##" with "08 00 00 1C >>>> $colorChange1", the second with "08 00 00 1C $colorChange2", etc. >>>> >>>> More info: >>>> --Script modifies files to theme them, in this case, I'm taking a BINARY >>>> (compiled XML) file, converting it to HEX via xxd, and then changing the >>>> text color for a theme via substitution. >>>> --I originally thought it could be done similar to what sed does... with >>>> s|(08\s00\s00\s1C)\s\d\d\s\d\d\s\d\d\s\d\d|$1$colorChange1|1 (note last >>>> digit...), but testing has shown that this doesn't work as expected. >>>> --Not really related, but in the binary, the colors are actually >>>> backwards, so color - FF AB CD EF becomes binary - EF CD AB FF, but that's >>>> an easy change. >>>> --CURRENTLY in my script, I'm doing this via a sub, passing an array of >>>> the colors, the file, and the file location (directory), and pulling the >>>> file, making the changes, and saving it back out via loop... BUT, because I >>>> couldn't figure out how to just do the multiple replaces... I cheated and >>>> made template files that contain "08 00 00 1C 11 11 11 11" and "08 00 00 1C >>>> 22 22 22 22", so when I iterate through the loop, I just change like so: >>>> for ( $i = 1 ; $i <= $COUNT ; $i++ ) { >>>> ... >>>> $line =~ s|08\s00\s00\s1c\s$i$i\s$i$i\s$i$i\s$i$i\s|08 00 00 1c >>>> $array_ref[$i]|; >>>> ... >>>> } >>>> >>>> Code works, but I'd much rather be able to pull native files straight >>>> out of the .zip, and change them that way... :P >>>> >>>> Anyway, again, if there are specifics I missed, happy to provide them. >>>> I keep thinking there should be an easy way to do this... but my >>>> google-fu is failing me.. >>>> >>>> Thanks again for the help with the previous problem, and in advance for >>>> any assistance on this one. :) >>>> At the very least, I guess I can provide some chatter to the group. >>>> >>>> -John (sephtin @gmail) >>>> >>>> _______________________________________________ >>>> Raleigh-talk mailing list >>>> Raleigh-talk at pm.org >>>> http://mail.pm.org/mailman/listinfo/raleigh-talk >>>> >>>> >>> >> >> _______________________________________________ >> 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: From mattnashbrowns at gmail.com Tue Feb 22 23:23:19 2011 From: mattnashbrowns at gmail.com (Matt Nash) Date: Wed, 23 Feb 2011 02:23:19 -0500 Subject: [Raleigh-talk] [Perl help] - Multiple substitutions in a file In-Reply-To: References: Message-ID: Ooh, shiny! Nicely done. On Wed, Feb 23, 2011 at 2:05 AM, Mike South wrote: > If you have the whole file in a string and the replacements in an array and > the 'marker' doesn't change, I think the following will work. > > The /e modifier executes code, so s/foo/ do something here /eg will replace > each instance of foo with whatever 'do something here', executed as perl > code, returns. So I just made it grab the next color off the list and put > that in place. > > In case you have more of the 'markers' than you have replacements, I just > put $1 back in right where we found it. The stuff that's printing out the > length of the string is a rough check to make sure I'm not accidentally > clobbering part of the string. > > mike > > > use strict; > use warnings; > > my $file = ' > 08 00 00 00 FF FF FF FF FF FF FF FF 1B 00 00 00 02 01 10 00 28 01 00 00 09 > 00 00 00 FF FF FF FF FF FF FF FF 1C 00 00 00 14 00 14 00 0D 00 00 00 00 00 > 0D 00 18 00 00 00 11 00 00 00 FF FF FF FF 08 00 00 05 02 0E 00 00 18 00 00 > 00 0E 00 00 00 FF FF FF FF 08 00 00 1C 00 00 00 FF 18 00 00 00 09 00 00 00 > FF FF FF FF 08 00 00 11 10 00 00 00 18 00 00 00 0F 00 00 00 FF FF FF FF 08 > 00 00 01 11 02 02 01 18 00 00 00 03 00 00 00 08 00 00 1C FF FF FF FF 08 00 > 00 01 05 00 08 01 18 00 00 00 10 00 00 00 FF FF FF FF 08 00 00 05 01 0F 00 > 00 18 00 00 00 05 00 08 00 00 1C 00 00 FF FF FF FF 08 00 00 05 01 0F 00 08 > 00 00 1C 00 18 00 00 '; > > my $length = length $file; > > my $color1 = '11#AA#BB#CC'; > my $color2 = '22#DD#EE#FF'; > my $color3 = '33#XX#XX#XX'; > my @replacements = ($color1, $color2, $color3); > #my @replacements = ($color1, $color2); > > my $marker = '08 00 00 1C '; > my $xx = qr/[[:xdigit:]]{2}/; # two hex digits > $file =~ s/$marker(($xx ){3}$xx)/$marker . (shift(@replacements) || $1)/ge; > > > print $file,$/; > print "length was $length, now it's ", length($file), $/; > > > On Tue, Feb 22, 2011 at 11:49 PM, Mike South wrote: > >> Also those \d's aren't going to match A-F are they? >> >> Are you familiar with qr// to let you store a regex bit in a variable? >> That might make some of this more readable. >> >> mike >> >> On Tue, Feb 22, 2011 at 10:58 PM, Matt Nash wrote: >> >>> Of course, you want actual spaces (not '\s') in the replace string, but >>> you knew that. >>> >>> >>> On Tue, Feb 22, 2011 at 9:56 PM, Matt Nash wrote: >>> >>>> Hi John, >>>> >>>> You are reading the entire file into an array of lines, then processing >>>> that array using a C-style for loop and interpolating the index var into the >>>> search string... and that is the LEAST crazy thing. :) It works, of course, >>>> because it turns out that there are infinitely many ways to do it. >>>> >>>> For your immediate problem of the search-and-replace, I recommend >>>> reading the whole file into a single string, newlines and all, then using >>>> the /gc modifiers in the regex. Maybe put your colorChange stuff into an >>>> array that you loop through, checking the regex for the nth match: >>>> >>>> @changes = ($change1, $change2, $change3); >>>> >>>> foreach $change (@changes) { >>>> $wholefile =~ >>>> s|08\s00\s00\s1C\s\d\d\s\d\d\s\d\d\s\d\d|08\s00\s00\s1C\s$change|gc; >>>> } >>>> >>>> see http://perldoc.perl.org/perlre.html and >>>> http://perldoc.perl.org/perlretut.htm for >>>> lots of details, but the upshot of the modifiers is: g makes it keep looking >>>> for matches; c tells it to remember where it last matched, and start from >>>> there on the next match. You don't have to care what n is, because you are >>>> matching your search string exactly as many times as you have changes to >>>> make. >>>> >>>> ...but this project sounds ripe for refactoring, if I may be so bold. >>>> Could it be that what you really need is a templating system? >>>> >>>> Thanks for bringing some much-needed questions to this list! >>>> >>>> Matt >>>> >>>> On Tue, Feb 22, 2011 at 8:31 PM, John Ricker >>> > wrote: >>>> >>>>> Received so much help with the last question (which seems to be >>>>> functioning great, btw.. thanks all)... thought I'd try again. ;) >>>>> >>>>> Have a script that was recently migrated to Perl from shell/bash, and >>>>> am wondering if there's an easy way in Perl to do the following- >>>>> >>>>> File(s) being modified are hex, and look something like: >>>>> ---x--- >>>>> ... >>>>> 08 00 00 00 FF FF FF FF FF FF FF FF 1B 00 00 00 02 01 10 00 28 01 00 00 >>>>> 09 00 00 00 FF FF FF FF FF FF FF FF 1C 00 00 00 14 00 14 00 0D 00 00 00 00 >>>>> 00 0D 00 18 00 00 00 11 00 00 00 FF FF FF FF 08 00 00 05 02 0E 00 00 18 00 >>>>> 00 00 0E 00 00 00 FF FF FF FF 08 00 00 1C 00 00 00 FF 18 00 00 00 09 00 00 >>>>> 00 FF FF FF FF 08 00 00 11 10 00 00 00 18 00 00 00 0F 00 00 00 FF FF FF FF >>>>> 08 00 00 01 11 02 02 01 18 00 00 00 03 00 00 00 FF FF FF FF 08 00 00 01 05 >>>>> 00 08 01 18 00 00 00 10 00 00 00 FF FF FF FF 08 00 00 05 01 0F 00 00 18 00 >>>>> 00 00 05 00 00 00 FF FF FF FF 08 00 00 05 01 0F 00 00 18 00 00 >>>>> ... >>>>> ---x--- >>>>> File(s) contain several occurrences of "08 00 00 1c ## ## ## ##" or >>>>> more appropriately for this list: "08\s00\s00\s1C\s\d\d\s\d\d\s\d\d\s\d\d" >>>>> >>>>> Now on to the good stuff... I would like to replace the ## ## ## ## >>>>> with my chosen colors, that are being provided by vars... Example: >>>>> colorChange1="FF 00 00 FF" >>>>> colorChange2="FF FF 00 FF" >>>>> colorChange3="FF 00 FF FF" >>>>> ... >>>>> >>>>> I'm wondering how it might be possible, to replace the FIRST occurrence >>>>> (in the file, NOT in a line) of "08 00 00 1C ## ## ## ##" with "08 00 00 1C >>>>> $colorChange1", the second with "08 00 00 1C $colorChange2", etc. >>>>> >>>>> More info: >>>>> --Script modifies files to theme them, in this case, I'm taking a >>>>> BINARY (compiled XML) file, converting it to HEX via xxd, and then changing >>>>> the text color for a theme via substitution. >>>>> --I originally thought it could be done similar to what sed does... >>>>> with s|(08\s00\s00\s1C)\s\d\d\s\d\d\s\d\d\s\d\d|$1$colorChange1|1 (note >>>>> last digit...), but testing has shown that this doesn't work as expected. >>>>> --Not really related, but in the binary, the colors are actually >>>>> backwards, so color - FF AB CD EF becomes binary - EF CD AB FF, but that's >>>>> an easy change. >>>>> --CURRENTLY in my script, I'm doing this via a sub, passing an array of >>>>> the colors, the file, and the file location (directory), and pulling the >>>>> file, making the changes, and saving it back out via loop... BUT, because I >>>>> couldn't figure out how to just do the multiple replaces... I cheated and >>>>> made template files that contain "08 00 00 1C 11 11 11 11" and "08 00 00 1C >>>>> 22 22 22 22", so when I iterate through the loop, I just change like so: >>>>> for ( $i = 1 ; $i <= $COUNT ; $i++ ) { >>>>> ... >>>>> $line =~ s|08\s00\s00\s1c\s$i$i\s$i$i\s$i$i\s$i$i\s|08 00 00 1c >>>>> $array_ref[$i]|; >>>>> ... >>>>> } >>>>> >>>>> Code works, but I'd much rather be able to pull native files straight >>>>> out of the .zip, and change them that way... :P >>>>> >>>>> Anyway, again, if there are specifics I missed, happy to provide them. >>>>> >>>>> I keep thinking there should be an easy way to do this... but my >>>>> google-fu is failing me.. >>>>> >>>>> Thanks again for the help with the previous problem, and in advance for >>>>> any assistance on this one. :) >>>>> At the very least, I guess I can provide some chatter to the group. >>>>> >>>>> -John (sephtin @gmail) >>>>> >>>>> _______________________________________________ >>>>> Raleigh-talk mailing list >>>>> Raleigh-talk at pm.org >>>>> http://mail.pm.org/mailman/listinfo/raleigh-talk >>>>> >>>>> >>>> >>> >>> _______________________________________________ >>> Raleigh-talk mailing list >>> Raleigh-talk at pm.org >>> http://mail.pm.org/mailman/listinfo/raleigh-talk >>> >>> >> > > _______________________________________________ > 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: From sephtin+pm-talk at gmail.com Tue Feb 22 23:51:38 2011 From: sephtin+pm-talk at gmail.com (John Ricker) Date: Wed, 23 Feb 2011 02:51:38 -0500 Subject: [Raleigh-talk] [Perl help] - Multiple substitutions in a file In-Reply-To: References: Message-ID: Very nice... I assumed that it'd have to be done via some sort of loop structure.. never crossed my mind that I could shift through the array to accomplish this. :) Sitting in front of the code, I agree that it would work. The marker does not change, the replacements are already in an array, and the file could easily be grabbed in a string. Thanks again for the assistance! This is too easy. :( I'll see if I can find something more fun and difficult.. like maybe instead of having to call system ("xxd...") to convert the binary to hex, see if I can find a way to do it in Perl as well.. if so, then I think I've eliminated the last "system" call in my script that can possibly be eliminated... ;) Anyway, thanks again! -John On Wed, Feb 23, 2011 at 2:05 AM, Mike South wrote: > If you have the whole file in a string and the replacements in an array and > the 'marker' doesn't change, I think the following will work. > > The /e modifier executes code, so s/foo/ do something here /eg will replace > each instance of foo with whatever 'do something here', executed as perl > code, returns. So I just made it grab the next color off the list and put > that in place. > > In case you have more of the 'markers' than you have replacements, I just > put $1 back in right where we found it. The stuff that's printing out the > length of the string is a rough check to make sure I'm not accidentally > clobbering part of the string. > > mike > > > use strict; > use warnings; > > my $file = ' > 08 00 00 00 FF FF FF FF FF FF FF FF 1B 00 00 00 02 01 10 00 28 01 00 00 09 > 00 00 00 FF FF FF FF FF FF FF FF 1C 00 00 00 14 00 14 00 0D 00 00 00 00 00 > 0D 00 18 00 00 00 11 00 00 00 FF FF FF FF 08 00 00 05 02 0E 00 00 18 00 00 > 00 0E 00 00 00 FF FF FF FF 08 00 00 1C 00 00 00 FF 18 00 00 00 09 00 00 00 > FF FF FF FF 08 00 00 11 10 00 00 00 18 00 00 00 0F 00 00 00 FF FF FF FF 08 > 00 00 01 11 02 02 01 18 00 00 00 03 00 00 00 08 00 00 1C FF FF FF FF 08 00 > 00 01 05 00 08 01 18 00 00 00 10 00 00 00 FF FF FF FF 08 00 00 05 01 0F 00 > 00 18 00 00 00 05 00 08 00 00 1C 00 00 FF FF FF FF 08 00 00 05 01 0F 00 08 > 00 00 1C 00 18 00 00 '; > > my $length = length $file; > > my $color1 = '11#AA#BB#CC'; > my $color2 = '22#DD#EE#FF'; > my $color3 = '33#XX#XX#XX'; > my @replacements = ($color1, $color2, $color3); > #my @replacements = ($color1, $color2); > > my $marker = '08 00 00 1C '; > my $xx = qr/[[:xdigit:]]{2}/; # two hex digits > $file =~ s/$marker(($xx ){3}$xx)/$marker . (shift(@replacements) || $1)/ge; > > > print $file,$/; > print "length was $length, now it's ", length($file), $/; > > > On Tue, Feb 22, 2011 at 11:49 PM, Mike South wrote: > >> Also those \d's aren't going to match A-F are they? >> >> Are you familiar with qr// to let you store a regex bit in a variable? >> That might make some of this more readable. >> >> mike >> >> On Tue, Feb 22, 2011 at 10:58 PM, Matt Nash wrote: >> >>> Of course, you want actual spaces (not '\s') in the replace string, but >>> you knew that. >>> >>> >>> On Tue, Feb 22, 2011 at 9:56 PM, Matt Nash wrote: >>> >>>> Hi John, >>>> >>>> You are reading the entire file into an array of lines, then processing >>>> that array using a C-style for loop and interpolating the index var into the >>>> search string... and that is the LEAST crazy thing. :) It works, of course, >>>> because it turns out that there are infinitely many ways to do it. >>>> >>>> For your immediate problem of the search-and-replace, I recommend >>>> reading the whole file into a single string, newlines and all, then using >>>> the /gc modifiers in the regex. Maybe put your colorChange stuff into an >>>> array that you loop through, checking the regex for the nth match: >>>> >>>> @changes = ($change1, $change2, $change3); >>>> >>>> foreach $change (@changes) { >>>> $wholefile =~ >>>> s|08\s00\s00\s1C\s\d\d\s\d\d\s\d\d\s\d\d|08\s00\s00\s1C\s$change|gc; >>>> } >>>> >>>> see http://perldoc.perl.org/perlre.html and >>>> http://perldoc.perl.org/perlretut.htm for >>>> lots of details, but the upshot of the modifiers is: g makes it keep looking >>>> for matches; c tells it to remember where it last matched, and start from >>>> there on the next match. You don't have to care what n is, because you are >>>> matching your search string exactly as many times as you have changes to >>>> make. >>>> >>>> ...but this project sounds ripe for refactoring, if I may be so bold. >>>> Could it be that what you really need is a templating system? >>>> >>>> Thanks for bringing some much-needed questions to this list! >>>> >>>> Matt >>>> >>>> On Tue, Feb 22, 2011 at 8:31 PM, John Ricker >>> > wrote: >>>> >>>>> Received so much help with the last question (which seems to be >>>>> functioning great, btw.. thanks all)... thought I'd try again. ;) >>>>> >>>>> Have a script that was recently migrated to Perl from shell/bash, and >>>>> am wondering if there's an easy way in Perl to do the following- >>>>> >>>>> File(s) being modified are hex, and look something like: >>>>> ---x--- >>>>> ... >>>>> 08 00 00 00 FF FF FF FF FF FF FF FF 1B 00 00 00 02 01 10 00 28 01 00 00 >>>>> 09 00 00 00 FF FF FF FF FF FF FF FF 1C 00 00 00 14 00 14 00 0D 00 00 00 00 >>>>> 00 0D 00 18 00 00 00 11 00 00 00 FF FF FF FF 08 00 00 05 02 0E 00 00 18 00 >>>>> 00 00 0E 00 00 00 FF FF FF FF 08 00 00 1C 00 00 00 FF 18 00 00 00 09 00 00 >>>>> 00 FF FF FF FF 08 00 00 11 10 00 00 00 18 00 00 00 0F 00 00 00 FF FF FF FF >>>>> 08 00 00 01 11 02 02 01 18 00 00 00 03 00 00 00 FF FF FF FF 08 00 00 01 05 >>>>> 00 08 01 18 00 00 00 10 00 00 00 FF FF FF FF 08 00 00 05 01 0F 00 00 18 00 >>>>> 00 00 05 00 00 00 FF FF FF FF 08 00 00 05 01 0F 00 00 18 00 00 >>>>> ... >>>>> ---x--- >>>>> File(s) contain several occurrences of "08 00 00 1c ## ## ## ##" or >>>>> more appropriately for this list: "08\s00\s00\s1C\s\d\d\s\d\d\s\d\d\s\d\d" >>>>> >>>>> Now on to the good stuff... I would like to replace the ## ## ## ## >>>>> with my chosen colors, that are being provided by vars... Example: >>>>> colorChange1="FF 00 00 FF" >>>>> colorChange2="FF FF 00 FF" >>>>> colorChange3="FF 00 FF FF" >>>>> ... >>>>> >>>>> I'm wondering how it might be possible, to replace the FIRST occurrence >>>>> (in the file, NOT in a line) of "08 00 00 1C ## ## ## ##" with "08 00 00 1C >>>>> $colorChange1", the second with "08 00 00 1C $colorChange2", etc. >>>>> >>>>> More info: >>>>> --Script modifies files to theme them, in this case, I'm taking a >>>>> BINARY (compiled XML) file, converting it to HEX via xxd, and then changing >>>>> the text color for a theme via substitution. >>>>> --I originally thought it could be done similar to what sed does... >>>>> with s|(08\s00\s00\s1C)\s\d\d\s\d\d\s\d\d\s\d\d|$1$colorChange1|1 (note >>>>> last digit...), but testing has shown that this doesn't work as expected. >>>>> --Not really related, but in the binary, the colors are actually >>>>> backwards, so color - FF AB CD EF becomes binary - EF CD AB FF, but that's >>>>> an easy change. >>>>> --CURRENTLY in my script, I'm doing this via a sub, passing an array of >>>>> the colors, the file, and the file location (directory), and pulling the >>>>> file, making the changes, and saving it back out via loop... BUT, because I >>>>> couldn't figure out how to just do the multiple replaces... I cheated and >>>>> made template files that contain "08 00 00 1C 11 11 11 11" and "08 00 00 1C >>>>> 22 22 22 22", so when I iterate through the loop, I just change like so: >>>>> for ( $i = 1 ; $i <= $COUNT ; $i++ ) { >>>>> ... >>>>> $line =~ s|08\s00\s00\s1c\s$i$i\s$i$i\s$i$i\s$i$i\s|08 00 00 1c >>>>> $array_ref[$i]|; >>>>> ... >>>>> } >>>>> >>>>> Code works, but I'd much rather be able to pull native files straight >>>>> out of the .zip, and change them that way... :P >>>>> >>>>> Anyway, again, if there are specifics I missed, happy to provide them. >>>>> >>>>> I keep thinking there should be an easy way to do this... but my >>>>> google-fu is failing me.. >>>>> >>>>> Thanks again for the help with the previous problem, and in advance for >>>>> any assistance on this one. :) >>>>> At the very least, I guess I can provide some chatter to the group. >>>>> >>>>> -John (sephtin @gmail) >>>>> >>>>> _______________________________________________ >>>>> Raleigh-talk mailing list >>>>> Raleigh-talk at pm.org >>>>> http://mail.pm.org/mailman/listinfo/raleigh-talk >>>>> >>>>> >>>> >>> >>> _______________________________________________ >>> Raleigh-talk mailing list >>> Raleigh-talk at pm.org >>> http://mail.pm.org/mailman/listinfo/raleigh-talk >>> >>> >> > > _______________________________________________ > 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: From msouth at gmail.com Wed Feb 23 05:07:55 2011 From: msouth at gmail.com (Mike South) Date: Wed, 23 Feb 2011 07:07:55 -0600 Subject: [Raleigh-talk] [Perl help] - Multiple substitutions in a file In-Reply-To: References: Message-ID: On Wed, Feb 23, 2011 at 1:51 AM, John Ricker wrote: > Very nice... > I assumed that it'd have to be done via some sort of loop structure.. never > crossed my mind that I could shift through the array to accomplish this. :) > > Sitting in front of the code, I agree that it would work. The marker does > not change, the replacements are already in an array, and the file could > easily be grabbed in a string. > Thanks again for the assistance! > > This is too easy. :( > s/too easy/perl/ :) mike > I'll see if I can find something more fun and difficult.. like maybe > instead of having to call system ("xxd...") to convert the binary to hex, > see if I can find a way to do it in Perl as well.. if so, then I think I've > eliminated the last "system" call in my script that can possibly > be eliminated... ;) > > Anyway, thanks again! > -John > > On Wed, Feb 23, 2011 at 2:05 AM, Mike South wrote: > >> If you have the whole file in a string and the replacements in an array >> and the 'marker' doesn't change, I think the following will work. >> >> The /e modifier executes code, so s/foo/ do something here /eg will >> replace each instance of foo with whatever 'do something here', executed as >> perl code, returns. So I just made it grab the next color off the list and >> put that in place. >> >> In case you have more of the 'markers' than you have replacements, I just >> put $1 back in right where we found it. The stuff that's printing out the >> length of the string is a rough check to make sure I'm not accidentally >> clobbering part of the string. >> >> mike >> >> >> use strict; >> use warnings; >> >> my $file = ' >> 08 00 00 00 FF FF FF FF FF FF FF FF 1B 00 00 00 02 01 10 00 28 01 00 00 09 >> 00 00 00 FF FF FF FF FF FF FF FF 1C 00 00 00 14 00 14 00 0D 00 00 00 00 00 >> 0D 00 18 00 00 00 11 00 00 00 FF FF FF FF 08 00 00 05 02 0E 00 00 18 00 00 >> 00 0E 00 00 00 FF FF FF FF 08 00 00 1C 00 00 00 FF 18 00 00 00 09 00 00 00 >> FF FF FF FF 08 00 00 11 10 00 00 00 18 00 00 00 0F 00 00 00 FF FF FF FF 08 >> 00 00 01 11 02 02 01 18 00 00 00 03 00 00 00 08 00 00 1C FF FF FF FF 08 00 >> 00 01 05 00 08 01 18 00 00 00 10 00 00 00 FF FF FF FF 08 00 00 05 01 0F 00 >> 00 18 00 00 00 05 00 08 00 00 1C 00 00 FF FF FF FF 08 00 00 05 01 0F 00 08 >> 00 00 1C 00 18 00 00 '; >> >> my $length = length $file; >> >> my $color1 = '11#AA#BB#CC'; >> my $color2 = '22#DD#EE#FF'; >> my $color3 = '33#XX#XX#XX'; >> my @replacements = ($color1, $color2, $color3); >> #my @replacements = ($color1, $color2); >> >> my $marker = '08 00 00 1C '; >> my $xx = qr/[[:xdigit:]]{2}/; # two hex digits >> $file =~ s/$marker(($xx ){3}$xx)/$marker . (shift(@replacements) || >> $1)/ge; >> >> >> print $file,$/; >> print "length was $length, now it's ", length($file), $/; >> >> >> On Tue, Feb 22, 2011 at 11:49 PM, Mike South wrote: >> >>> Also those \d's aren't going to match A-F are they? >>> >>> Are you familiar with qr// to let you store a regex bit in a variable? >>> That might make some of this more readable. >>> >>> mike >>> >>> On Tue, Feb 22, 2011 at 10:58 PM, Matt Nash wrote: >>> >>>> Of course, you want actual spaces (not '\s') in the replace string, but >>>> you knew that. >>>> >>>> >>>> On Tue, Feb 22, 2011 at 9:56 PM, Matt Nash wrote: >>>> >>>>> Hi John, >>>>> >>>>> You are reading the entire file into an array of lines, then processing >>>>> that array using a C-style for loop and interpolating the index var into the >>>>> search string... and that is the LEAST crazy thing. :) It works, of course, >>>>> because it turns out that there are infinitely many ways to do it. >>>>> >>>>> For your immediate problem of the search-and-replace, I recommend >>>>> reading the whole file into a single string, newlines and all, then using >>>>> the /gc modifiers in the regex. Maybe put your colorChange stuff into an >>>>> array that you loop through, checking the regex for the nth match: >>>>> >>>>> @changes = ($change1, $change2, $change3); >>>>> >>>>> foreach $change (@changes) { >>>>> $wholefile =~ >>>>> s|08\s00\s00\s1C\s\d\d\s\d\d\s\d\d\s\d\d|08\s00\s00\s1C\s$change|gc; >>>>> } >>>>> >>>>> see http://perldoc.perl.org/perlre.html and >>>>> http://perldoc.perl.org/perlretut.htm for >>>>> lots of details, but the upshot of the modifiers is: g makes it keep looking >>>>> for matches; c tells it to remember where it last matched, and start from >>>>> there on the next match. You don't have to care what n is, because you are >>>>> matching your search string exactly as many times as you have changes to >>>>> make. >>>>> >>>>> ...but this project sounds ripe for refactoring, if I may be so bold. >>>>> Could it be that what you really need is a templating system? >>>>> >>>>> Thanks for bringing some much-needed questions to this list! >>>>> >>>>> Matt >>>>> >>>>> On Tue, Feb 22, 2011 at 8:31 PM, John Ricker < >>>>> sephtin+pm-talk at gmail.com> wrote: >>>>> >>>>>> Received so much help with the last question (which seems to be >>>>>> functioning great, btw.. thanks all)... thought I'd try again. ;) >>>>>> >>>>>> Have a script that was recently migrated to Perl from shell/bash, and >>>>>> am wondering if there's an easy way in Perl to do the following- >>>>>> >>>>>> File(s) being modified are hex, and look something like: >>>>>> ---x--- >>>>>> ... >>>>>> 08 00 00 00 FF FF FF FF FF FF FF FF 1B 00 00 00 02 01 10 00 28 01 00 >>>>>> 00 09 00 00 00 FF FF FF FF FF FF FF FF 1C 00 00 00 14 00 14 00 0D 00 00 00 >>>>>> 00 00 0D 00 18 00 00 00 11 00 00 00 FF FF FF FF 08 00 00 05 02 0E 00 00 18 >>>>>> 00 00 00 0E 00 00 00 FF FF FF FF 08 00 00 1C 00 00 00 FF 18 00 00 00 09 00 >>>>>> 00 00 FF FF FF FF 08 00 00 11 10 00 00 00 18 00 00 00 0F 00 00 00 FF FF FF >>>>>> FF 08 00 00 01 11 02 02 01 18 00 00 00 03 00 00 00 FF FF FF FF 08 00 00 01 >>>>>> 05 00 08 01 18 00 00 00 10 00 00 00 FF FF FF FF 08 00 00 05 01 0F 00 00 18 >>>>>> 00 00 00 05 00 00 00 FF FF FF FF 08 00 00 05 01 0F 00 00 18 00 00 >>>>>> ... >>>>>> ---x--- >>>>>> File(s) contain several occurrences of "08 00 00 1c ## ## ## ##" or >>>>>> more appropriately for this list: "08\s00\s00\s1C\s\d\d\s\d\d\s\d\d\s\d\d" >>>>>> >>>>>> Now on to the good stuff... I would like to replace the ## ## ## ## >>>>>> with my chosen colors, that are being provided by vars... Example: >>>>>> colorChange1="FF 00 00 FF" >>>>>> colorChange2="FF FF 00 FF" >>>>>> colorChange3="FF 00 FF FF" >>>>>> ... >>>>>> >>>>>> I'm wondering how it might be possible, to replace the >>>>>> FIRST occurrence (in the file, NOT in a line) of "08 00 00 1C ## ## ## ##" >>>>>> with "08 00 00 1C $colorChange1", the second with "08 00 00 1C >>>>>> $colorChange2", etc. >>>>>> >>>>>> More info: >>>>>> --Script modifies files to theme them, in this case, I'm taking a >>>>>> BINARY (compiled XML) file, converting it to HEX via xxd, and then changing >>>>>> the text color for a theme via substitution. >>>>>> --I originally thought it could be done similar to what sed does... >>>>>> with s|(08\s00\s00\s1C)\s\d\d\s\d\d\s\d\d\s\d\d|$1$colorChange1|1 (note >>>>>> last digit...), but testing has shown that this doesn't work as expected. >>>>>> --Not really related, but in the binary, the colors are actually >>>>>> backwards, so color - FF AB CD EF becomes binary - EF CD AB FF, but that's >>>>>> an easy change. >>>>>> --CURRENTLY in my script, I'm doing this via a sub, passing an array >>>>>> of the colors, the file, and the file location (directory), and pulling the >>>>>> file, making the changes, and saving it back out via loop... BUT, because I >>>>>> couldn't figure out how to just do the multiple replaces... I cheated and >>>>>> made template files that contain "08 00 00 1C 11 11 11 11" and "08 00 00 1C >>>>>> 22 22 22 22", so when I iterate through the loop, I just change like so: >>>>>> for ( $i = 1 ; $i <= $COUNT ; $i++ ) { >>>>>> ... >>>>>> $line =~ s|08\s00\s00\s1c\s$i$i\s$i$i\s$i$i\s$i$i\s|08 00 00 1c >>>>>> $array_ref[$i]|; >>>>>> ... >>>>>> } >>>>>> >>>>>> Code works, but I'd much rather be able to pull native files straight >>>>>> out of the .zip, and change them that way... :P >>>>>> >>>>>> Anyway, again, if there are specifics I missed, happy to provide them. >>>>>> >>>>>> I keep thinking there should be an easy way to do this... but my >>>>>> google-fu is failing me.. >>>>>> >>>>>> Thanks again for the help with the previous problem, and in advance >>>>>> for any assistance on this one. :) >>>>>> At the very least, I guess I can provide some chatter to the group. >>>>>> >>>>>> -John (sephtin @gmail) >>>>>> >>>>>> _______________________________________________ >>>>>> Raleigh-talk mailing list >>>>>> Raleigh-talk at pm.org >>>>>> http://mail.pm.org/mailman/listinfo/raleigh-talk >>>>>> >>>>>> >>>>> >>>> >>>> _______________________________________________ >>>> Raleigh-talk mailing list >>>> Raleigh-talk at pm.org >>>> http://mail.pm.org/mailman/listinfo/raleigh-talk >>>> >>>> >>> >> >> _______________________________________________ >> Raleigh-talk mailing list >> Raleigh-talk at pm.org >> http://mail.pm.org/mailman/listinfo/raleigh-talk >> >> > > _______________________________________________ > 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: