http://www.perlmonks.org?node_id=495532


in reply to Newbie question, advice appreciated

Greetings all,
One other suggestion is to use HTML::Template, if you have access to CPAN that is. Im not sure how you want to handle the lines but here is a quick mockup that may or may not work for your purposes.
#!/usr/bin/perl -w use strict; use HTML::Template; my $output_template = qq* <table border="0" width="100%" cellspacing="1" bgcolor="#000000"> <tr bgcolor="#cccccc"> <td>Function Name:</td><td>Description</td> </tr> <TMPL_LOOP NAME="functions"> <tr bgcolor="#ffffff"> <td valign="top"><TMPL_VAR NAME="name"></td><td valign="top">< +TMPL_VAR NAME="desc"></td> </tr> </TMPL_LOOP> </table> *; my @output = do{ local @_; #you will need to replace the DATA handle with #one to your file of interest. my $string = do{ local undef $/; <DATA> }; while($string =~ /\s?STANDARD\s+(.*?)\nfunction\s(\S+)\n?/mg){ push @_, {name=>$2, desc=>$1}; } @_; }; my $tmplt = HTML::Template->new(scalarref=>\$output_template,die_on_ba +d_params=>0); $tmplt->param({functions=>\@output}); print $tmplt->output(); exit; __DATA__ blah blah blahblah blah blah blah blah blahblah blah blah # STANDARD This is some text that describes function seventhdoesnotgetenoughsleep # STANDARD This is some describing text function seventhgetsomesleep blah blah blahblah blah blah # STANDARD This is some description function seventhgethomesafe blah blah blahblah blah blah # STANDARD This is some text ...nuff said function seventhwhyiraq blah blah blahblah blah blah

the output is
Function Name:Description
seventhdoesnotgetenoughsleepThis is some text that describes
seventhgetsomesleepThis is some describing text
seventhgethomesafeThis is some description
seventhwhyiraqThis is some text ...nuff said


Now in your code you will need to change the <DATA> part to read in your file and as dragonchild illustrated, TEST IF OPEN WORKED! Sorry for the caps but its a necessary step. Also you will need to open another handle (and check if it opened) to your output file for writing. Is that what you were thinking?
I hope that helps


Update(s)!
This approach assumes that the file size is not enormous since it reads the entire file into $string. If the files are rather large you probably want to go with a line by line approach.
I updated the @output do block code to match across newlines.
Removed obsolete code from do block my @data = split /\#/, $string; .

-InjunJoel
"I do not feel obliged to believe that the same God who endowed us with sense, reason and intellect has intended us to forego their use." -Galileo