dhudnall has asked for the wisdom of the Perl Monks concerning the following question:

Hi, my question I have is that I am reading in a file from the command line and storing the file in the perl script as argv[0]; what I would then like to do with the file is remove all COMMENTS in the file. The following is something like what I did...again I am very new to perl....please excuse.
$TheFile = argv[0]; #Then I want to remove comments $TheFile =~ s/^'//g;
I figure that would take $TheFile and remove any lines that begin with a (') or "tick-mark" representing a comment in ASP and replace that line with NOTHING..basically erasing it. If you have any thoughts on this please leave your comments. Thank you for your time.

Replies are listed 'Best First'.
Re: removing comments
by ikegami (Patriarch) on Jun 21, 2007 at 15:45 UTC

    ^ means normally means "beginning of string", not "beginning of line". The m modifier changes that. (See perlre.) That gives you two options:

    $TheFile =~ s/^'//mg;

    and

    while (my $TheLine = <>) { $TheLine =~ s/^'//; ... }

    Update: You probably want to use '.* instead of just ' unless you only want to delete the '.

Re: removing comments
by jettero (Monsignor) on Jun 21, 2007 at 15:20 UTC

    That would work if you ran the program like so:

    perl -i -pe 's/^\s*\'.*//' targetfilename

    On the perlrun page you can see why this works (along with examples of how -p and -i expand).

    You are running your substitution on the filename rather than on any of the contents of it.

    -Paul

      that would work if I am running the command from the command line but I would like to be able to do it inside my program instead of having to run a command from the command line but I don't know how. Would I do something like:
      $TheFile = $ARGV[0]; open (INFILE, $TheFile) or die while(<INFILE>) { perform the removal of the comments here...or what?!? and if so what w +ould the syntax be. }
      I would just like to immediately remove all comments before going any further into my massive program.
        Do you want to have the entire file in memory? What do you plan to do with it, apart from removing comments? For any one line of the file, s/^'.*// will clear the comment text, leaving a blank line in its place.

        Caution: Contents may have been coded under pressure.
        Your question is a bit ambigious. Do you want to
        1. create a new file with no comments
        2. load the file into memory and strip the comments before further processing
        ?

        variant 1:
        open (my $in, "<", $ARGV[0]) or die "in: $@"; open (my $out, ">", "$ARGV[0].new") or die "out: $@"; while( my $line = <$in>) { print $out $line unless $line =~ /^#/; } close $in; close $out;
        variant 2:
        open (my $in, "<", $ARGV[0]) or die "in: $@"; my @lines = grep { not /^#/ } <$in>; #<> here in list context, therefo +re no explicit loop
        Note that you cannot erase a line from the middle of a file, you will always have to write a new one. But you already knew that, didn't you? ;-)


        holli, /regexed monk/

        You'd have to open an input and an output file — generally they're not the same one, unless you're very clever about it.

        There is actually an example on the page I linked but it may have been unobvious to locate.

        open my $input, "<", $ARGV[0] or die "couldn't open $ARGV[0]: $!"; open my $output, ">", "output.txt" or die "coudln't open output: $!"; while(<$input>) { if( m/something/ ) { # do stuff } s/stuff//g; # etc. print {$output} $_; } close $output; close $input;

        -Paul

Re: removing comments
by graff (Chancellor) on Jun 22, 2007 at 02:27 UTC
    Have you ever heard of the unix command line tool called "grep"? The perl "grep" function is sort of based on that. The tool is used to filter a text stream (a text file, or text data being fed through a "piped" command line) by selecting (or, with the "-v" option, rejecting) lines that match a given regex pattern:
    grep -v "^'" file.asp > uncommented_file.asp
    To do the same thing with perl, it's still the case that all you need is a command line:
    perl -ne 'print unless (/^\x27/)' file.asp > uncommented_file.asp
    (In that case, an ms-windows "dos" shell would rather have double quotes instead of single quotes around the "script". Note that I use "\x27" instead of an actual apostrophe character, to avoid quoting problems in the shell.)

    If you really need this to be done as part of a larger script that is doing other things with the non-comment lines, you can read all the lines into an array like this:

    open( INPUT, "<", $ARGV[0] ) or die "$ARGV[0]: $!\n"; my @code_lines = grep !/^\x27/, <INPUT>;