in reply to Search replace with a block

Then I am able to get into the block. Would like to know what you guys think.

Hi

Have you heard of Config::General or Config::ApacheFormat and Apache::Admin::Config?

#!/usr/bin/perl -- use strict; use warnings; use Data::Dump qw/ dd /; use Config::General qw(ParseConfig); my $rawconfig = q{ <IfModule> ## workaround of some kind <IfModule mod_limitipconn.c> <Location /> MaxConnPerIP 12 NoIPLimit image/* </Location> <Location /location2> MaxConnPerIP 0 </Location> <Location /location3> MaxConnPerIP 0 </Location> </IfModule> }; my %config = ParseConfig(-String => $rawconfig, -AutoTrue => 1); dd( \%config ); __END__ { IfModule => { IfModule => { "mod_limitipconn.c" => { Location => { "" => {} }, M +axConnPerIP => 12, NoIPLimit => "image/*" }, }, Location => { "/location2" => { MaxConnPerIP => 0 }, "/location3" => { MaxConnPerIP => 0 }, }, }, }

Some of these modules are worth a try or two :)

Replies are listed 'Best First'.
Re^2: Search replace with a block
by santoshrao99 (Initiate) on May 11, 2016 at 08:29 UTC
    Hi, Yes I did play around wit those. There are two problems - 1. I would have a very hard time getting to install those libraries, read red-tapism. 2. I tested admin::config, admin::config was unable to parse this file as my file is a sub file which is included in the global confi. The library goes looking for some standard stuff which would be present ideally in a httpd.conf file. Config::General I did play around but wasn't to get far.
      ok, maybe something like this, filter every location block
      $rawconfig =~ s{ <location \s+ (\S+?)> (.+?) </location> }{ ReformulateLocation("$1", "$2"); }gimsex; print $rawconfig; sub ReformulateLocation { my( $loc, $con ) = @_; my @pairs = $con =~ m{ ^ \s* (\S+) \s+ ([^\r\n]+?) \s* $}gmix; dd( { $loc, \@pairs } ); return "<location $loc> $con </location>"; ## no change } __END__ { "/" => ["MaxConnPerIP", 12, "NoIPLimit", "image/*"] } { "/location2" => ["MaxConnPerIP", 0] } { "/location3" => ["MaxConnPerIP", 0] } <IfModule> ## workaround of some kind <IfModule mod_limitipconn.c> <location /> MaxConnPerIP 12 NoIPLimit image/* </location> <location /location2> MaxConnPerIP 0 </location> <location /location3> MaxConnPerIP 0 </location> </IfModule>
      Also perlfaq6 has some tips
        Based on both the feedback I did the following -
        #!/usr/bin/perl -w use strict; my $location ="/newlocation"; my $file_name ='ipconn.conf'; $/ = undef; open (FILE, "< $file_name") or die $!; my $file = <FILE>; close FILE; my $value; # check for the block we want if ($file =~ m/\<Location\ $location.+?\<\/Location\>/s){ my $block = $&; if($block =~ s/MaxConnPerIP\s*(.+?)*/MaxConnPerIP 10/g) { print $block; } }
        Now the issue I have is $block has the updated text and $file has the entire file. How do I merge those , by merge meaning either remove the matching block and add this new one. Or change my value into the original variable which has the file. Which is part of the reason I was leaning on using tie::file, updating the file was so much more easier. Let me know what you guys think. Thank you for your help.