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

I have a problem in replacing code.
i look for a method and if found i comment it out
but i dont want to comment it if it is already commented
my problem is
/** * public void method1(); * is deprecated */ public void method1();
in this case my code replaces to
/*/** * public void method1()*/ */ public void method1();
it had to actually replace it
/** * public void method1(); */ /*public void method1();*/
I dont want to replace anything in the comments and my code reads the entire java file as a single line.
can u give a solution for this

Replies are listed 'Best First'.
Re: replace java code when not commented
by zer (Deacon) on Mar 28, 2006 at 05:30 UTC
    This works on your test code.
    #!/usr/bin/perl use strict; use warnings; while (<DATA>){ print /^public/?$_ = "/*".$_."*/":$_; } __DATA__ /** * public void method1(); * is deprecated */ public void method1();
      Heres an update. This allows for tabbing and private methods. I am not sure how many method types there are in java but this is the direction to go.
      use strict; use warnings; while (<DATA>){ print /^\s*(public)|^\s*(private)/?$_ = "/*".$_."*/\n":"$_\n"; } __DATA__ /** * public void method1(); * is deprecated */ public void method1(); rgrprivate void method2(); private void method3();

      Output:
      /**
      * public void method1();
      * is deprecated
      */
      /*public void method1();*/
           rgrprivate void method2();
      /*    private void method3();*/
Re: replace java code when not commented
by gube (Parson) on Mar 28, 2006 at 05:41 UTC
    #!/usr/local/bin/perl while(<DATA>) { $_ =~ s/^(public[^\n]+)/\/\*$1\*\//si; print $_; } __DATA__ /** * public void method1(); * is deprecated */ public void method1();
      this is good! Use gubes method. heres my extention applied to it.
      while(<DATA>) { $_ =~ s/^\s*((public|private)[^\n]+)/\/\*$1\*\//si; print $_; }