in reply to Re: Search replace with a block
in thread Search replace with a block

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.

Replies are listed 'Best First'.
Re^3: Search replace with a block
by beech (Parson) on May 11, 2016 at 09:05 UTC
    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.

        Maybe use ReformulateLocation to perform the location based substitution?

        :D