CGI/Perl Guide | Learning Center | Forums | Advertise | Login
Site Search: in

  Main Index MAIN
INDEX
Search Posts SEARCH
POSTS
Who's Online WHO'S
ONLINE
Log in LOG
IN

Home: Perl Programming Help: Beginner:
Strip off comments in a file

 



iphone
User

Feb 12, 2011, 11:06 PM

Post #1 of 2 (386 views)
Strip off comments in a file Can't Post

I have some .c,.h etc files which have some text in-between comments of the form /* */.I am trying to write a subroutine that takes each file as input and strips off these comments.Note that there are multiple comments in a line

/*===========================================================================

FUNCTION apps_proc

DESCRIPTION
This function initializes the PAM for PMIC.

It does the following:
* Initializes the Apps proc

INPUT PARAMETERS
None.

RETURN VALUE
None.

DEPENDENCIES
None

SIDE EFFECTS
NONE.

===========================================================================*/
void
apps_proc (void)
{
} /* apps_proc() */


Code
 
sub strip_file_comments
{
$file=shift;

open my $fh, '<',$file or die "could not open '$file' $!";
while (my $line = <$fh>)
{
if ($line =~ /*){
Replace everything with spaces until it reaches "*/"//How do i write this logic
}
}
}



(This post was edited by iphone on Feb 12, 2011, 11:11 PM)


Karazam
User

Feb 13, 2011, 1:19 AM

Post #2 of 2 (383 views)
Re: [iphone] Strip off comments in a file [In reply to] Can't Post

This is actually pretty tricky. Luckily, it is also a perlfaq, and can be viewed at http://perldoc.perl.org/perlfaq6.html#How-do-I-use-a-regular-expression-to-strip-C-style-comments-from-a-file?.

Hope this helps. Smile

 
 


Search for (options) Powered by Gossamer Forum v.1.2.0

Web Applications & Managed Hosting Powered by Gossamer Threads
Visit our Mailing List Archives