?
Solved

Removing comments from perl source code file.

Posted on 2004-04-12
10
Medium Priority
?
162 Views
Last Modified: 2013-12-25
Hello everyone,

    How to remove all of the # starting lines in a perl source code file? It's way too big to do it by hand. Thanks for any help.
0
Comment
Question by:intoxicated
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
  • 2
  • 2
  • +3
10 Comments
 

Expert Comment

by:mannie
ID: 10805890
Something like this perhaps

#!/bin/perl

open FILE,"filetostrip.pl" or die("Failed to open");
@lines=<FILE>;
close FILE;

open FILEAGAIN, ">filetostrip.pl" or die("Failed to reopen");
foreach (@lines) {
 print $_ unless (/^[#](.*)$/);
}
close FILEAGAIN;

/Linus
0
 
LVL 28

Expert Comment

by:FishMonger
ID: 10807600
perl -pi -e 's/^#.*$//' script.pl
0
 
LVL 28

Expert Comment

by:FishMonger
ID: 10807643
Oops, that will also remove the shebang line; this one won't

perl -pi -e 's/^#.*$// unless $.==1' script.pl
0
Docker-Compose to Simplify Multi-Container Builds

Our veteran DevOps Author takes you through how to build a multi-container environment, managed with a single utility in order to simplify your deployments.

 
LVL 84

Expert Comment

by:ozo
ID: 10808200
If you have
#
inside of quoted strings it will remove them too
0
 
LVL 2

Expert Comment

by:Fataqui
ID: 10815163
Hi


I did not add put a line empty line clean up, but you can add on before saving the new code...

$file = '';

open FILE, "<" . $file or die ( "Can not open file" );
while ( <FILE> )
{
      tr/\r//d;

      if ( !m/^#!/ )
      {

            s/(^|\n|\s)*#[^\n]*//g;

      }

      $out .= $add . $_;

}

close FILE;

open NEW, ">" . $file;
print NEW $out;
close NEW;




Fataqui
0
 
LVL 2

Expert Comment

by:Fataqui
ID: 10815229
Hi

This line....

     $out .= $add . $_;

should be....

     $out .= $_;

I put the $add there to insert new lines, but seeing I did not know if you wanted to strip \r?\n I took that part out and forgot to remove $add, before posting, sorry!


Fataqui
0
 
LVL 1

Expert Comment

by:Glauron
ID: 10830022
all very good and all..... but WHY would you want to remove the comments!?!?  Especially if the file is so big!!
Hope you can find your way around it again  =)

..... unless ur just formatting for some other use...
.. oh... nm
0
 

Author Comment

by:intoxicated
ID: 10830833
Thenks alot lads. FishMonger, you're a star ;). Is there a way for your single-liner to remove the whole comment line (now it leaves an empty line, making wide areas of empty space)?? Why do I need it? To check how much of the file is actual code.
0
 
LVL 28

Accepted Solution

by:
FishMonger earned 500 total points
ID: 10833724
Simple, just add \n to the end of the seach pattern.

perl -pi -e 's/^#.*\n$// unless $.==1' script.pl
0
 

Author Comment

by:intoxicated
ID: 10833838
That worked like charm. Thanks again.
0

Featured Post

Quick Cloud Training

Looking for some quick training on the cloud in 2 hours or less? Check out these how-to guides in AWS, Linux, OpenStack, Azure, and more!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

It is a general practice to get rid of old user profiles on a computer  in a LAN environment. As I have been working with a company in a LAN environment where users move from one place to some other place at times. This will make many user profil…
Active Directory replication delay is the cause to many problems.  Here is a super easy script to force Active Directory replication to all sites with by using an elevated PowerShell command prompt, and a tool to verify your changes.
Learn the basics of modules and packages in Python. Every Python file is a module, ending in the suffix: .py: Modules are a collection of functions and variables.: Packages are a collection of modules.: Module functions and variables are accessed us…
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.
Suggested Courses

765 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question