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

I am writing a script to insert method headers into C# files for the development group, since there are hundreds of files without headers and we need it done FAST.

I have gotten to the point where for sample data consisting of a method with no arguments, the method header will be properly inserted, but a second pair of parentheses is inserted after the first on the method declaration line. If the method HAS arguments, though, nothing happens. I have examined perlre and Chapter 5 of Camel numerous times. UPDATE: Sample data at end of READMORE.

#!c:/perl/bin/perl.exe use strict; use warnings; use File::Find; our $copyright = <<'HERE'; #region "Usage Rights" // Redacted to protect the guilty // All rights reserved. This document was developed under U.S. // Government Contract No. pi, and therefore the U.S. // Government is granted a copyright license to this document for U.S +. // Government purposes. #endregion HERE our $access_modifier = "public|private|protected|internal|protected in +ternal"; our $method_modifier = "virtual|sealed|override|abstract|extern"; our $comment = <<HERE; \\s*///\\s*<summary> \\s*///\\s*\\w+ \\s*///\\s*</summary> \\s*///\\s*<param name = ".+">\\w+</param>\\s* HERE our $not_method = "as|basebreakcase|catch|checked|class|const|continue| default|delegate|do|else|enum|event|explicit|false|finally|fixed| for|foreach|goto|if|implicit|in|interface|is|lock|namespace| null|object|operator|out|params|readonly|ref|return|sizeof| stackalloc|struct|switch|this|throw|true|try|typeof|unchecked| unsafe|using|volatile|while"; =comment put stuff to glob the files here. =cut # Gets input and output filenames while getting Wanted to work my ($filin, $filout) = (@ARGV); Wanted($filin, $filout); exit; sub Wanted { my ($filin, $filout) = (@_); my %methods; open(FILIN, "<", "$filin") or die $^E; my $autogen = 0; my $line; while ($line = <FILIN>) { chomp $line; if ($line =~ /^\W*$not_method/) { next; } if ($autogen < 48) { # This block looks for auto-generated fi +les and skips to the next with no alterations at all. if ($line =~ /<auto-generated>/) { close FILIN; return 0; } ++$autogen; } if ( $line =~ m`(new)? # new is an optional + element \s+ ($access_modifier) # public, private, + protected etc. \s* ($method_modifier)? # static, override +, extern, etc. Also optional \s* \w+ # return type, not + optional \s+ \w+ # method name \s* \( .* \) # parameter list i +n parentheses `xo ) { # 'x' allows comme +nts and internal whitespace, o says compile the pattern once only my $method = $line; my $summary = $method; $summary =~ s/^\s+//; my $params = $summary; $params =~ s`.*\(([^\(]*)\)`$1`; my @params = split ',', $params; my $header = <<HERE; /// <summary> /// $summary /// </summary> HERE for (@params) { my ($type, $name) = split ' ', $_; $header .= <<HERE; /// <param name="$name">$type</param> HERE } $methods{$method} = $header; } } close FILIN; open FILIN, "<", "$filin" or die $!; my $file; {local $/; $file = <FILIN>; close FILIN; } METHOD: for (sort keys %methods) { my $method = $_; if ($file =~ m`$comment$method`s) { next METHOD; } my $header = $methods{$method}; my @file = split /$method/, $file; $file[0] .= $header; $file = join '', @file; } $file =~ s/#region "Usage Rights".+Government purposes.\n#endregion//g +s; $file = $copyright . $file; open FILOUT, ">", "$filout"; print FILOUT $file; close FILOUT; }
Sample data:
public virtual void Destroy(object O) { try { m_container.Mockery.VerifyAllExpectationsHaveBeenMet() +; } finally { m_container = null; } }

Replies are listed 'Best First'.
Re: regex subst not DWIM
by dreadpiratepeter (Priest) on Dec 19, 2007 at 22:03 UTC
    Well for starters, i think there is a bug in the substitution for $params. I think you wanted [^\)]*


    -pete
    "Worry is like a rocking chair. It gives you something to do, but it doesn't get you anywhere."
      Thanks! .*\(([^\)]*)\).* worked (needed the last bit to lose the brace at the end of the line).
Re: regex subst not DWIM
by suaveant (Parson) on Dec 19, 2007 at 22:14 UTC
    With a program this size with a vague dataset it would be helpful to have an input file with an example of what output you want to get, then we could actually run it. Just a minimal dataset will do.

                    - Ant
                    - Some of my best work - (1 2 3)

      Here you go:
      public virtual void Destroy(object O) { try { m_container.Mockery.VerifyAllExpectationsHaveBeenMet() +; } finally { m_container = null; } }
        Ok... first problem I see (besides the params regexp already pointed out... is your not_method regexp piece...

        you didn't put it in a container, so your pipes aren't doing what you expect...

        if ($line =~ /^\W*(?:$not_method)/) {
        That'll work better.

        Update a \b at the end would also help make sure you don't match a string starting with as or if etc...

        if ($line =~ /^\W*(?:$not_method)\b/) {

                        - Ant
                        - Some of my best work - (1 2 3)

        And what should it look like once converted? When I run it I see it getting the copyright and nothing else changes.

                        - Ant
                        - Some of my best work - (1 2 3)