Removing comments with Perl

Something I am doing is removing comments from the file while processing it. I was wondering if there is a module out there for this.

Sorting code I keep writing time and again

    while(<>) {
        s/#.*// ;
        next if /^ \s+ $/x ;

        **** do something useful here ****
    }

      

Edit . To clarify, the input is not Perl. This is a text file of my own creation that may have data that I want to process one way or another. I want to be able to post comments that my programs ignore.

+2


source to share


2 answers


The question does not indicate what type of file it is. Are we dealing with perl source files? If so, your approach is not entirely correct - see gbacon's comment. Perl file sources are notoriously difficult (impossible?) To parse with regex. In this case, or if you need to deal with multiple file types, use Regexp :: Common :: comment as suggested by Niffle. Otherwise, if you think your regex logic is correct for your scenario, then I personally prefer to write it explicitly, it's just a couple of strighforward lines, little is gained from using the module (and you inject the dependency).



+3


source


If this is not a learning experience, I suggest using Regexp :: Common :: comment instead of writing your own regular expressions.



It supports quite a few languages.

+5


source







All Articles