in reply to Search replace with a block

Also I not sure why but the match operator does work if I [slurp the whole file into a scalar]

Your example with the tied file loops over each line at a time. One line will never match both the start and the end of a Location block because they are on different lines. Conversely, when you slurp the whole file into a scalar, then it can match.

Replies are listed 'Best First'.
Re^2: Search replace with a block
by santoshrao99 (Initiate) on May 12, 2016 at 02:20 UTC
    I understand the difference now. Thank you. Is it more simple if I slurp the entire thing in and match the start end and replace the value as compared to doing a tie::file ? Also I am sorry for the noobish question, but is there a way to get into the match's block and do the replace in a single regex.

      There are many, many different ways to approach this and identifying which is simplest is a very subjective measure. Try some of the approaches and see which you think appears simplest to you and just go with that one. TIMTOWTDI.

      You could construct a single regex to do a multiline match and replace but that will certainly be far from the simplest approach. I would recommend Apache::Admin::Config over such a complicated approach any day. Note that this is a pure-perl module so you can just use the source if necessary.

      However, here's the general all-in-one method:

      #!/usr/bin/env perl use strict; use warnings; my $in = <<EOT; <Location /> MaxConnPerIP 12 NoIPLimit image/* </Location> EOT my $max = 16; my $out = $in; $out =~ s/(<Location .+?MaxConnPerIP\s+)(?:\d+)(.+?<\/Location>)/$1$ma +x$2/s; print $out;