Search code examples
regexperltagscommentsdepth

Perl Regex to be used in order comment out irrelevant code with many level of nesting


I am trying to comment out all the preprocessing instructions of the type:

if LABEL
  do something
else
  do something else
end 

You have to provide a list of LABELS to the script so that it only comments out the line containing that "if" statements of interest and all the code under the "else" part of the statement (if any - There may or may not be an "else").
Note that there could be other type of preprocessing instructions (all ending with the keyword "end"), which increases the difficulty when there are multiple level of nesting. So, we have to make sure to remove the correct else/end associated to each "if LABEL_OF_INTEREST".
In order to comment out a line, use #.

In order to make things clearer, let's take an example, where only the code from within the "if" statements have to be preserved, the rest need to be commented out. Here is the input:

if STATEMENT_TO_KEEP  
   do somthing useful  
   do somthing else useful  
   while A < 1
      if STATEMENT_TO_IGNORE  
          do somthing useful  
          do somthing else useful  
      else  
          do nothing  
          do nothing  
      end  
   end
else  
   do nothing  
   do nothing        
end  
if STATEMENT_TO_KEEP  
   do somthing useful  
   do somthing else useful
end     

The expected result would be:

 #if STATEMENT_TO_KEEP  
   do somthing useful  
   do somthing else useful  
   while A < 1
      if STATEMENT_TO_IGNORE  
          do somthing useful  
          do somthing else useful  
      else  
          do nothing  
          do nothing  
      end  
   end
  #else  
  #   do nothing  
  #   do nothing        
  #end 
#if STATEMENT_TO_KEEP  
   do somthing useful  
   do somthing else useful
#end 

Solution

  • use strict;
    use warnings;
    
    my $state = 0;                       # print out when this is 0
    while (<>) {
      if (!s/^(\s*if)/#$1/) {            # when you find and if, comment out
        if (!$state and /^\s*else/) {    # turn off printing from else blocks
          $state = 1;
          s/^/#/;                        # and comment out 'else'
        }   
        if ($state) {
          s/^/#/;                        # check flag, and comment out if set
          if (/^\s*end\s*$/) {
            $state = 0;                  # clear flag upon an end
          }   
        }   
      }
      print;                             # actual print
    }