On Thu, Jul 14, 2016 at 1:24 PM, Darryl Philip Baker <
darryl.ba...@northwestern.edu> wrote:

> On Thu, Jul 14, 2016 at 10:50 AM, Darryl Philip Baker <
> darryl.ba...@northwestern.edu> wrote:
>
> While not truly a beginner it feels that way after not doing anything
> substantial in Perl in many years.
>
>
>
> I currently need a program to take Apache HTTPD configuration files in
> HTTPD 2.2 syntax used in current production and convert them to HTTPD 2.4
> syntax in future production. I will need to do this many times weekly until
> we cut over to the new systems. My first challenge is converting the
> permissions syntax:
>
>         Order deny,allow
>
>         Deny from all
>
> To
>
>         Require all denied
>
>
>
> Why not go line by line? Not 100% certain on the Apache syntax, but in
> your while loop where you are processing line by line:
>
>
>
> next if /Order\s+deny,\s*allow/i;  # Skip it in the output
>
>
>
> if( /(\s*)Deny\s+from\s+all/i) {
>
>
>
>     my $leading_whitespace = $1;  # Preserve whatever whitespace is in the
> file now, could be multiple tabs and or spaces
>
>
>
>     print $leading_whitespace . "Require all denied";
>
>
>
> }
>
> Learning from that example I came up with:
>
> $prevline = $_;                                 # Save current line for
> later if needed
>
> # Change:
>
> #       Order deny,all
>
> #       Deny from all
>
> # To:
>
> #       Require all denied
>
> # No matter what case the first letters are
>
> if ( /Order\s+Deny,\s*Allow/i ) {
>
>         next;
>
>         chomp $_;
>
>         if ( /(\s)Deny\s+from\s+All/i) {
>
>                 $whitespace = $1;
>
>                 print $whitespace, "Require all denied \# New Syntax\n";
>
>                 next;
>
>                 chomp $_;
>
>         } else {
>
>                 print $prevline, " \# Old Syntax\n";
>
>         }
>
> }
>
> It is not doing the expected. The “Order deny,allow” line is never output
> and where I want “Require all denied” the “Deny from all” is still being
> output.
>
>
>
You never get to the nested if. (Not sure why they're nested). When the
outside if statement is true you immediately call next, that will skip
everything after it and go back to the nearest loop.

By the way chomp $_ is not necessary as chomp does that when another
variable is not specified.

In /(\s)Deny\s+from\s+All/ That first \s will match only *one* white space
character, probably not what you want.

I believe for the Apache syntax to be correct you don't want to output a
comment on the same line as a statement. Comments in Apache need to be on
their own line.

<http://rock13.com>

Reply via email to