Reputation: 9057
I have a file with the following content:
sub is_top_level0 { # with comment
print '\nFollowed by comment \n'; # The comment
# line from begin
}
I'm, basically, using the following code where $sFilename
is set to the right file name:
use PPR;
open(DATAIN, $sFilename);
my @aFileData = map({ s/\r$//g; $_; } <DATAIN>);
close (DATAIN);
my $aRawFileData= \@aFileData;
printf("Read:\n@{$aRawFileData}===============\n");
my $aUncommentFileData = PPR::decomment($aRawFileData);
printf("Uncomment:\n@{$aUncommentFileData}===============\n");
The output is:
Read:
sub is_top_level0 { # with comment
print '\nFollowed by comment \n'; # The comment
# line from begin
}
===============
Uncomment:
sub is_top_level0 { # with comment
print '\nFollowed by comment \n'; # The comment
# line from begin
}
===============
as can be seen the part named "Uncomment" still contains the comments.
How to handle this?
(Note problem might sound a bit silly, but I'm not a perl programmer, just trying to modify some existing code)
Edit: small clarification as I wrote in the comment on the answer from @Dada (and that has been added in the answer as well):
Probably not clear enough in the question, but I would like to have the data available in an array
$aRawFileData
and$aUncommentFileData
(with the line terminators\n
) so I can iterate over the arrays (as it is done at the moment).
Upvotes: 1
Views: 191
Reputation: 6626
PPR::decomment
takes a string as argument, cf the documentation:
It takes a single argument: a string containing the course code
Here, you are calling it on $aRawFileData
, which is an arrayref (used as a string, it looks like ARRAY(0x.....)
; which doesn't contain any #
and is thus left as is by PPR::decomment
)
Instead of reading your source code line by line, you should read it as a single string, store that in a variable, and pass it to PPR::decomment
:
use File::Slurp;
my $sFilename = "test.pl";
my $source_code = read_file($sFilename);
my $without_comments = PPR::decomment($source_code);
print $without_comments;
You might want to still remove the \r
after reading the file. In that case add
$source_code =~ s/\r$//g;
after reading your input.
I've used File::Slurp
to read the whole file at once. If you'd like another method to do that, have a look at the question What is the best way to slurp a file into a string in Perl?.
If you need the lines of your file in an array, you can then split your string:
my @lines = split /\n/, $without_comments;
If you want the newlines (\n
) to be kept, then use:
my @lines = split /(?<=\n)/, $without_comments;
Upvotes: 3