tags:

views:

93

answers:

2

Something I keep doing is removing comments from a file as I process it. I was was wondering if there a module to do this.

Sort of code I keep writing time and again is

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

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

Edit Just to clarify, the input is not Perl. It is a text file of my own making that might have data I want to process in some way or other. I want to beable to place comments that are ignored by my programs

+5  A: 

Unless this is a learning experience I suggest you use Regexp::Common::comment instead of writing your own regular expressions.

It supports quite a few languages.

Nifle
+3  A: 

The question does not make clear 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 source files are notoriously difficult (impossible?) to parse with regex. In that case, or if you need to deal with several types of files, 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 pair of strighforward lines, there is little to be gained by using a module (and you introduce a dependency).

leonbloy
@leonbloy You are probably right that it is hardly worth wrapping it up in a module. I have a reflex reaction not to repeat code, maybe I am taking this too far.
justintime