Perl - How to crawl a directory, parse every file in the directory and extract all comments to html file
I need some serious help, I'm new to perl and need help on how to create a perl script that would prompt the user for a directory containing perl files, parse every file in that directory and then extract all comments from each file to individual html files.
code examples or existing modules that alread开发者_JAVA百科y does this would be great.
Thank you!
PPI can be used to parse Perl code files. This should get you started on getting Perl files in a directory (assuming they have .pl
extensions) and grabbing the comments. I'm not sure what you mean about the HTML piece:
use warnings;
use strict;
use PPI;
my $dir = shift;
for my $file (glob "$dir/*.pl") {
my $doc = PPI::Document->new($file);
for my $com (@{ $doc->find('PPI::Token::Comment') }) {
print $com->{content};
}
}
Update: Look at HTML::Template (but it may be overkill).
A simple cpan search with keyword "dir" turned up a whole slew of helpful modules. One of the ones I use a lot is:
IO::Dir
If you have a choice, here's a Ruby script
#!/usr/bin/env ruby
print "Enter directory: "
directory=File.join(gets.chomp,"*.pl")
directory="/home/yhlee/test/ruby/*.pl"
c=0
Dir[directory].each do |file|
c+=1
o = File.open("file_#{c}.html","w")
File.open(file).each do |line|
if line[/#/]
o.write ( line.scan(/;*\s+(#.*)$/)[0].first + "\n" ) if line[/;*\s+#/]
o.write ( line.scan(/^\s+(#.*)$/)[0].first + "\n") if line[/^\s+#/]
end
end
o.close
end
精彩评论