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

hello,

Need some guidence on a problem:
I got several files in directories. I wrote a script to parse all files in a directory. Looking at the extention I know what script it is and what is used for comments.

eg. .pl is a perl-script with # this as comment... for c it.s // or /* */

I need to remove the head comments and replace it with a special cvs header...

But only the head(top of file) comments and not the comments from there on!

What logic can i use for this?


--
My opinions may have changed,
but not the fact that I am right

Replies are listed 'Best First'.
Re: Head comments parsing
by ChOas (Curate) on May 18, 2001 at 14:00 UTC
    Hey ;))

    Something like this ?:
    while (<INPUTFILE>) { last if $HeaderLines==$.; ProcessHeader($_); };

    If you know how many lines there are in the header
    you can just set $HeaderLines to that...($. will be set to the current
    line number during each read (I`m assuming you`re using \n as a delimiter))

    Since you didn`t mention any other header-delimeter or tags, and said `top of the file`...
    that would be my guess

    GreetZ!,
      ChOas

    print "profeth still\n" if /bird|devil/;
      Ok... See i know how to parse te header... But i only want the top of the file being parsed!!!

      eg for perl:
      #!/usr/bin/perl -w # # Comment's about the file ... # ########################################### # Need this ... use strict;
      IMHO opninion it can't be safely done because there is now way every comment structure is the same...



      --
      My opinions may have changed,
      but not the fact that I am right

        # Assume that the header is all the first block of commented # lines and that the first non-comment line ends the # header block while (<OLDFILE>) { last unless is_comment($filetype, $_); } # Print new header to new file print NEWFILE new_header($filetype); # Print the rest of the file while (<OLDFILE>) { print NEWFILE $_; }
        --
        <http://www.dave.org.uk>

        "Perl makes the fun jobs fun
        and the boring jobs bearable" - me

Re: Head comments parsing
by andye (Curate) on May 18, 2001 at 14:20 UTC
    Maybe something like this for Perl
    my @input = <FILE> ; my @body = grep {! (1 ... /^[^#]/) } @input; print "$special_cvs_header @body";
    update:  grep {! (1 ... /^(?!#)/) } @input; is better though it looks like swearing
Re: Head comments parsing
by bobione (Pilgrim) on May 18, 2001 at 14:20 UTC
    You probably have to define a structure of head-comments like :
    For Global comment like C
    match one /* and stop when another */ (this his your header.
    or for Line-comment like Perl
    #
    # Here is your header until you've got a \n in your comment
    #
    <- (here \n)
    # Here another comment

    I hope I really undestand your question, because I am not sure...

    BobiOne KenoBi ;)